Filter löschen
Filter löschen

Euler method for ODE

2 Ansichten (letzte 30 Tage)
PJS KUMAR
PJS KUMAR am 21 Sep. 2018
Beantwortet: James Tursa am 21 Sep. 2018
I wrote the following code for Euler method
function sol=Euler2(fn,a,b,y0,n)
h=(b-a)/n;
x=a:h:b;
y(1)=y0;
for k=1:n
y(k+1)=y(k)+h*feval(fn,x(k),y(k));
end
sol=[x',y'];
Suggest me to obtain 'y' vector without 'for' loop, i.e., vectorising statement which can replace the 'for' loop

Akzeptierte Antwort

James Tursa
James Tursa am 21 Sep. 2018
In general, you can't "vectorize" this process as you suggest. Since the calculation for y(k+1) depends on y(k), the calculation must be done in a loop as you are currently doing. An exception would be in cases where the fn function was of a form where the entire result could be obtained analytically (e.g., fn was a constant). But for your case, where fn is some arbitrary function being passed in, you cannot do that and as a result you will need the loop.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by