Index exceeds number of array elements (1) Heuns method

조회 수: 2 (최근 30일)
Darren Tharby
Darren Tharby 2021년 2월 24일
답변: VBBV 2021년 11월 20일
% 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