Matlab for loop indexing confusion

Click For Summary
SUMMARY

The forum discussion clarifies the indexing behavior of MATLAB in the context of a for loop within an Euler method implementation. The code provided initializes the vector "y" with a boundary condition "y0" and iterates from 1 to the length of "t" minus one. The confusion arises from the expectation that indexing could start at zero, which is incorrect in MATLAB as it requires one-based indexing. The correct implementation ensures that "y(1)" corresponds to the initial condition "y0".

PREREQUISITES
  • Understanding of MATLAB programming language
  • Familiarity with numerical methods, specifically the Euler method
  • Knowledge of vector indexing in MATLAB
  • Basic concepts of ordinary differential equations (ODEs)
NEXT STEPS
  • Study MATLAB's one-based indexing system
  • Learn about the Euler method for solving ordinary differential equations
  • Explore MATLAB's vector operations and their implications on performance
  • Investigate error handling in MATLAB for out-of-bounds indexing
USEFUL FOR

Mathematics students, MATLAB programmers, and anyone implementing numerical methods for solving differential equations will benefit from this discussion.

cookiemnstr510510
Messages
162
Reaction score
14
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
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   Reactions: sumairaanwar and hdp12

Similar threads

Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K