differential equation huen plotting problem

조회 수: 11 (최근 30일)
Sneha
Sneha 2021년 7월 3일
답변: Walter Roberson 2021년 7월 3일
x_a=linspace(0,2,n)
h=x_a(n)-x_a(n-1)
y_a=((x_a.^2+1/2.*x_a+1)).^2
for i=1:n-1 ;
x_h=linspace(0,2,n)
h_h=x_h(n)-x_h(n-1)
A(i)=x_h(i)+h_h
B(i)=y_h(i)+h_h*(1+4.*x_h(i)).*sqrt(y_h(i))
L(i)=(1+4.*x_h(i)).*sqrt(y_h(i))
R(i)=(1+4.*A(i)).*sqrt(B(i))
y_h(1)=1;
y_h(i+1)=y_h(i)+1/2.*(h_h.*L(i).*R(i))
i=i+1;
end
plot(x_a,y_a,'--kd')
hold on
plot(x_h,y_h,'--rx')
Above code is to comapre the value of diffrenetial equation by analytic method and huen method by plotting it on graph.
But,Dont know why its not plotting the graph correctly.
If I am plotting the analytic solution graph individually its plotting the correct graph.But when I am plotting the two graphs together,none of the two graphs are correct.
Please help!!!

채택된 답변

Walter Roberson
Walter Roberson 2021년 7월 3일
You did not initialize y_h(1) before you used it.
Your plots are very different scales: your analytic goes up to about 40 but your heun goes to about 10^10 .
This suggests that you have programmed your heun incorrectly.
format long g
n = 127;
x = linspace(0,2,n);
h = x(n)-x(n-1);
y_a =((x.^2+1/2.*x+1)).^2;
y_h(1) = 1;
for i=1:n-1
A(i) = x(i) + h;
B(i) = y_h(i) +h*(1+4.*x(i)).*sqrt(y_h(i));
L(i) = (1+4.*x(i)).*sqrt(y_h(i));
R(i) = (1+4.*A(i)).*sqrt(B(i));
y_h(i+1) = y_h(i) + 1/2.*(h.*L(i).*R(i));
end
plot(x,y_a,'--kd')
figure
plot(x,y_h,'--rx')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by