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 Center および File ExchangeMathematics についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by