MATLAB Answers

Index exceeds number of array elements (1) Heuns method

22 views (last 30 days)
Darren Tharby
Darren Tharby on 24 Feb 2021
Commented: Darren Tharby on 24 Feb 2021
% step size %
h = 0.1;
% number of steps %
N = 10;
x(1) = 0.1;
y(1) = 1.1;
f = 2*sin(x)+y;
for i = 1:N
y(i+1) = y(i) + (h/2) * f(x(i),y(i)) + f(x(i+1),y(i+1)),
y(i) + h * f(x(i),y(i));
x(i+1) = x(i)+h;
end
plot(x,y)

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 24 Feb 2021
Edited: KALYAN ACHARJYA on 24 Feb 2021
Please verify (Read): Heuns Method
h = 0.1;
% number of steps %
N = 10;
y=zeros(1,N);
x=zeros(1,N);
x(1) = 0.1;
y(1) = 1.1;
f =@(x,y) 2*sin(x)+y;
for i = 1:N
c1=f(x(i),y(i));
c2=f(x(i)+h,y(i)+c1*h);
y(i+1)=y(i)+(h/2)*(c1+c2);
x(i+1)=x(i)+h;
end
plot(x,y)

Community Treasure Hunt

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

Start Hunting!

Translated by