Not outputting all the values for Euler's formula

I want to find the y values at t=5,6,7,8,9 for step sizes h=1,0.5,0.25,0.1. The code I have written below shows me all the intermediate values how do I only show the values I want? (N.B the first part of code is the function and second part to calculate the values for my question.)
function y_out = Euler1(F,t0,h,tfinal,y0)
y = y0;
yout = y;
for t = t0 : h : tfinal-h
s = F(t,y);
y = y + h*s;
yout = [yout; y];
end
F = @(t,y) (y^2+t)^3;
y0 = 8;
t0 = 5;
tfinal = 9;
h1 = Euler1(F,t0,1,tfinal,y0);
disp(h1);
h0_5 = Euler1(F,t0,0.5,tfinal,y0);
disp(h0_5);
h0_25 = Euler1(F,t0,0.25,tfinal,y0);
disp(h0_25);
h0_1 = Euler1(F,t0,0.1,tfinal,y0);
disp(h0_1);
Many thanks for the help in advance :)

 Accepted Answer

Just use indexing to pick off the elements you want to display. E.g.,
disp(h0_5(1:2:end));
:
disp(h0_25(1:4:end));
:
disp(h0_1(1:10:end));

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!