Implement iterations on MATLAB

  • MATLAB
  • Thread starter sandy.bridge
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses using MATLAB for the first time, specifically implementing iterations for a given formula. The suggestion is made to use vectors instead of for loops, which is more efficient. The conversation also mentions resources for beginners, including the MathWorks documentation, MATLAB Answers, and MATLAB exercises.
  • #1
sandy.bridge
798
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:
[tex]v(t)=10(1-e^{-t})[/tex]
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
  • #2


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))
 
  • #3


Try this:
Code:
t = linspace(0, 5, 1000); [COLOR="SeaGreen"]% Produces 1000 data points linearly spaced on [0,5].[/COLOR]
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:
[COLOR="SeaGreen"]% BAD ![/COLOR]
x = 0:.01:100;
[COLOR="Blue"]for[/COLOR] i = 1:length(x);
    y(i) = x(i) .^ 2;
[COLOR="Blue"]end[/COLOR]

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;
[COLOR="Blue"]for[/COLOR] i = 1:length(x);
    y(i) = x(i) .^ 2;
[COLOR="Blue"]end[/COLOR]
fprintf([COLOR="DarkOrchid"]'for: cputime: %f elapsed: %f \n'[/COLOR], [cputime - t, toc(e)]);
t = cputime;
e = tic;
x = 0:.01:1000000;
y = x .^ 2;
fprintf([COLOR="DarkOrchid"]'vec: cputime: %f elapsed: %f \n'[/COLOR], [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:
  • #4


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

1. How do I implement iterations in MATLAB?

To implement iterations in MATLAB, you can use a for loop or a while loop. A for loop is useful when you know the number of iterations beforehand, while a while loop is helpful when the number of iterations depends on a condition. Both loops can be set up using the proper syntax and can be used to execute a specific code block multiple times.

2. What is the best way to optimize my iteration code in MATLAB?

To optimize your iteration code in MATLAB, you can consider vectorizing your code. This means using array operations rather than element-wise operations. By using vectorization, you can potentially reduce the number of iterations and improve the performance of your code.

3. Can I use nested iterations in MATLAB?

Yes, you can use nested iterations in MATLAB. This means having one iteration loop inside another. Nested iterations can be useful when working with multi-dimensional arrays or when you need to perform a specific task on each element of a matrix.

4. How do I control the flow of my iterations in MATLAB?

To control the flow of your iterations in MATLAB, you can use conditional statements such as if/else or switch/case. These statements can be used to determine when the iteration should continue or stop based on certain conditions. You can also use keywords like 'break' or 'continue' to terminate or skip iterations, respectively.

5. Can I implement iterations on a specific data type in MATLAB?

Yes, you can implement iterations on any data type in MATLAB, including numbers, strings, and arrays. The same iteration syntax can be used for different data types, but you may need to use specific functions or methods depending on the data type and the task you want to perform.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
998
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
984
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top