Index exceeds number of array elements (1) Heuns method

1 Ansicht (letzte 30 Tage)
Darren Tharby
Darren Tharby am 24 Feb. 2021
Beantwortet: VBBV am 20 Nov. 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)

Antworten (2)

KALYAN ACHARJYA
KALYAN ACHARJYA am 24 Feb. 2021
Bearbeitet: KALYAN ACHARJYA am 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)

VBBV
VBBV am 20 Nov. 2021
% step size %
h = 0.1;
% number of steps %
N = 10;
x = zeros(1,N);
y = zeros(1,N);
yt = zeros(1,N);
x(1) = 0.1;
y(1) = 1.1;
f = @(x,y) 2*sin(x)+y;
for i = 2:N
yt(i) = y(i-1) + h * f(x(i-1),y(i-1)); % intermediate approximation
y(i) = y(i-1) + (h/2) * (f(x(i-1),y(i-1)) + f(x(i-1),yt(i)));
x(i) = x(i-1)+h;
end
plot(x,y,'linewidth',2);
hold on;
plot(x,yt,'ro','MarkerSize',6,'MarkerFaceColor','red')
legend('Final approx','Intermediate approx')
You have to modify the intermediate values step to get final approximation,

Kategorien

Mehr zu Programming 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