Matlab for loop indexing confusion

In summary, the code provided is an implementation of the Euler method for solving ordinary differential equations. The first element of the vector y is denoted as y(1) and is given as a boundary condition. The for loop begins at i=1 and runs until i=length(t)-1, with each iteration calculating the next element of y using the Euler method formula. The code is correct and any confusion may arise from not understanding the indexing and conventions used in MATLAB.
  • #1
Homework Statement
Code euler method for solving ODE in matlab
Relevant Equations
test case given: y0=10, f=@(t,y) -0.5*y
The test case for the block of code below is:
y0=10,
f=@(t,y) -0.5*y,
[t,y]=euler_method_attempt(f,0,5,y0,10)

This code below works and is the correct answer, but I am confused on some parts of it. When indexing the for loop it seems as if the first output for "y" would be y(2), not y(1). And as far as the math goes I think y(2)=7.5.
I also don't understand why we wouldn't write something like:
for i=0:length(t)...
So how does MATLAB access the first element of "y"?

Thanks so much!

Matlab:
function [t,y]=euler_method_attempt(f,t0,tf,y0,n)
t=linspace(t0,tf,n+1);
dt=t(2)-t(1);
y=[y0,zeros(1,n)];

for i=1:length(t)-1
    y(i+1) = f(t(i),y(i)).*dt +y(i);
end

end
 
Physics news on Phys.org
  • #2
y(1) is what in the code is called 'y0' (line 4) and it is the boundary condition that you have to supply in order to solve the ODE, so you don't calculate it: it's given.

In MATLAB you access vectors with y(i) with i running from 1 to the total number of elements.
y(0) is not accepted, so
cookiemnstr510510 said:
why we wouldn't write something like:
for i=0:length(t)...
is wrong because you have to start from 1.

Moreover in your for loop you are using the variable y(i+1) so you cannot stop at i = length(t) because in the loop you will have y(n+1+1) = y(n+2) which is outside the range of the vector you earlier defined.
 
  • Like
Likes sumairaanwar and hdp12

1. What is Matlab for loop indexing?

Matlab for loop indexing is a way to access and manipulate specific elements or subsets of data in a loop. It allows you to perform operations on a large amount of data without having to write repetitive code.

2. How does for loop indexing work in Matlab?

In Matlab, for loop indexing works by specifying a range of indices within square brackets, separated by a colon. For example, if you want to access elements 1 to 5 of a vector, you would use the syntax: vector(1:5). This will create a new vector containing only those elements.

3. Why is there confusion surrounding for loop indexing in Matlab?

There can be confusion surrounding for loop indexing in Matlab because it is a powerful tool with many different ways to use it. This can lead to different approaches and techniques, which can be confusing for beginners.

4. How can I avoid confusion when using for loop indexing in Matlab?

To avoid confusion when using for loop indexing in Matlab, it is important to fully understand the syntax and how it works. It can also be helpful to practice and experiment with different examples to gain a better understanding of how to use it effectively.

5. Are there any tips for using for loop indexing in Matlab?

Yes, there are a few tips for using for loop indexing in Matlab. Firstly, it is important to use descriptive variable names to make your code more readable. Additionally, it can be helpful to use comments to explain your code and make it easier to understand. Lastly, try to break down your code into smaller, manageable chunks to make it easier to debug and troubleshoot any issues.

Suggested for: Matlab for loop indexing confusion

Replies
5
Views
979
Replies
6
Views
2K
Replies
8
Views
757
Replies
3
Views
710
Replies
2
Views
830
Replies
59
Views
3K
Replies
1
Views
886
Replies
3
Views
1K
Back
Top