How to make graph that plot between Analytical Solution (Exact Solution) and Numerical Method Solution?

6 ビュー (過去 30 日間)
Greetings!
Can someone help to guide me how to make graph that plot between Analytical Solution (Exact Solution) and Adams-Moulton Solution? Here I attached my script.
%% 2nd-order Adams-Moulton Solution
fun = @(t,y) ((1+4*t)*((y)^1/2));
y0 = 1;
tspan = [0,1];
N = 4;
%% Initial Values
h = (tspan(2) - tspan(1))/N;
exactY = @(t) (((3*t)/2)+3*t.^2).^(2/3);
t1 = tspan(1) + h; y1 = exactY(t1);
t2 = tspan(1) + 2*h; y2 = exactY(t2);
[t2,Y2] = AM2(fun,tspan,y0,y1,N);
%% Display Solution
Y = exactY(t2);
disp('-----------------------------');
disp('t_i y(t_i) AM2 Error')
disp('-----------------------------');
formatSpec = '%.2f %.5f %.5f %.5f\n';
fprintf(formatSpec,[t2';Y';Y2';abs(Y'-Y2')])
I really appreciate any help you can provide. Thank you.
  4 件のコメント
Torsten
Torsten 2022 年 3 月 5 日
fun = @(t,y) (1+4*t)*sqrt(y);
y0 = 1;
tspan = [0,1];
N = 4;
%% Initial Values
h = (tspan(2) - tspan(1))/N;
exactY = @(t)(t/2 + t.^2 + 1).^2;
t1 = tspan(1) + h; y1 = exactY(t1);
[T,Ynum] = AM2(fun,tspan,y0,y1,N);
%% Display Solution
Yexact = exactY(T);
plot(T,[Ynum,Yexact])
function [t,y] = AM2(fun,tspan,y0,y1,N)
a = tspan(1);
b = tspan(2);
h = (b-a)/N;
t = zeros(N+1,1);
y = zeros(N+1,1);
t(1) = a; y(1) = y0;
t(2) = a+h; y(2) = y1;
for i = 2:N
t(i+1) = t(i) + h;
Fi = fun(t(i),y(i));
Fi1 = fun(t(i-1),y(i-1));
funh = @(yh) yh - y(i) - h/12*(5*fun(t(i+1),yh) + 8*Fi - Fi1);
y(i+1) = fsolve(funh,y(i));
end
end
Yusuf Arya Yudanto
Yusuf Arya Yudanto 2022 年 3 月 5 日
Many thanks! I really appreciate your help.

サインインしてコメントする。

採用された回答

Alan Stevens
Alan Stevens 2022 年 3 月 5 日
After Y = exactY(t2) you probably need something like:
plot(t2,Y2,t2,Y)
legend('AM','Exact')
However, you haven't supplied the code for your AM function, so I can't check!
  6 件のコメント
Yusuf Arya Yudanto
Yusuf Arya Yudanto 2022 年 3 月 5 日
Oh. Thank you for the correction. I really appreciate it!
Yusuf Arya Yudanto
Yusuf Arya Yudanto 2022 年 3 月 6 日
Hello, Sir Alan. Can you help me make a loglog scale plot of the multiple solutions? I've posted the question, please kindly check it. How to make convergence plot (error VS time step) in a log-log scale among four numerical methods and exact solution?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by