How to Implement Iterations in MATLAB for a Given Formula?

  • Context: MATLAB 
  • Thread starter Thread starter sandy.bridge
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around implementing iterations in MATLAB for the formula v(t)=10(1-e^{-t}) over the interval 0<=t<=5. Participants explore different approaches to achieve this, including the use of loops and vectorized operations.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant expresses confusion about using a "for loop" in MATLAB and seeks guidance on implementing iterations for the given formula.
  • Another participant suggests that a for loop may not be necessary and provides a vectorized approach to apply the function to values of t from 1 to 5.
  • A different participant recommends using the linspace function to generate 1000 data points for t and demonstrates the vectorized implementation of the formula.
  • One participant emphasizes the efficiency of vector operations over for loops in MATLAB, providing a comparison of execution times for both methods.
  • A participant inquires about beginner resources for learning MATLAB and mentions an interest in implementing Euler's method for differentials.
  • Another participant suggests the MathWorks documentation and MATLAB Answers as valuable resources for beginners.

Areas of Agreement / Disagreement

There is no consensus on the necessity of using a for loop, as some participants advocate for vectorized operations while others initially consider the loop approach. The discussion remains unresolved regarding the best method for implementation.

Contextual Notes

Participants have varying levels of experience with MATLAB, which may influence their suggestions and preferences for coding practices. The discussion does not resolve the best approach to implementing iterations for the formula.

Who May Find This Useful

Beginners in MATLAB programming, individuals interested in numerical methods, and those looking for efficient coding practices in MATLAB may find this discussion beneficial.

sandy.bridge
Messages
797
Reaction score
1
Hello all,
I am trying to play around with MATLAB so I can become familiar with it. I have absolutely no programming experience, si it's a bit confusing for me. I have this formula:
v(t)=10(1-e^{-t})
and I want to implement iterations on MATLAB for 0<=t<=5.

I understand that I need to start "for loop", but I'm not entirely sure how to go about it. I know to start it,
"for i=0:n"

Any suggestions and explanations?
 
Last edited by a moderator:
Physics news on Phys.org


Do you mean that you want to apply that function to each value of t from 1 to 5? There's no need to use a for loop.

t = 1:5;
v = 10*(1 - e.^(-t))
 


Try this:
Code:
t = linspace(0, 5, 1000); % Produces 1000 data points linearly spaced on [0,5].
v = 10*(1 - exp(-t));
v will then be in your workspace and you can do anything with it. For plotting, the more points you use the better the plot will be.

In MATLAB, you should always prefer using vectors to for loops, e.g. use this:
Code:
x = 0:.01:100;
y = x .^ 2;
instead of:
Code:
% BAD !
x = 0:.01:100;
for i = 1:length(x);
    y(i) = x(i) .^ 2;
end

Here is a quick program that tests the speed of a for loop versus a vector operation:
Code:
t = cputime;
e = tic;
x = 0:.01:1000000;
for i = 1:length(x);
    y(i) = x(i) .^ 2;
end
fprintf('for: cputime: %f elapsed: %f \n', [cputime - t, toc(e)]);
t = cputime;
e = tic;
x = 0:.01:1000000;
y = x .^ 2;
fprintf('vec: cputime: %f elapsed: %f \n', [cputime - t, toc(e)]);

On my computer it outputs:
Code:
for: cputime: 12.850000 elapsed: 12.723039 
vec: cputime: 1.270000 elapsed: 1.395663
As you can see, even with the small example, MATLAB is more efficient using vectors than loops.
 
Last edited:


Can you guys recommend a good book for beginners? I was also attempting to implement Euler's method for differentials.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K