Index exceeds number of array elements (1) Heuns method

1 次查看(过去 30 天)
% 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)

回答(2 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021-2-24
编辑:KALYAN ACHARJYA 2021-2-24
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 2021-11-20
% 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,

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by