How can I continue my Leapfrog method code ?

20 ビュー (過去 30 日間)
Hazel Can
Hazel Can 2022 年 5 月 23 日
編集済み: Hazel Can 2022 年 5 月 25 日
How can I continue my Leapfrog / midpoint two- step method code ?
clc; clear all;
t=[0 1];
h=0.01;
n=(t(2)-t(1))/h;
alpha=0.5;
%initials%
y_m(1)=2;
y_m(2)=exp(20.*h)+cos(h);
f_m(1)=20.*(y_m(1)-cos(t(1)))-sin(t(1));
f_m(2)=20.*(y_m(2)-cos(t(2)))-sin(t(2));
%Midpoint Two step method%
for i=3:n
y_m(i)=y_m(i-2)+2.*h.*f_m(i-1);
f_m(i)=20.*(y_m(i)-cos(t(i)))-sin(t(i));
end
  1 件のコメント
Torsten
Torsten 2022 年 5 月 23 日
編集済み: Torsten 2022 年 5 月 23 日
How can I continue my Leapfrog / midpoint two- step method code ?
What do you mean by "continue" ?
You forgot to set
t = 0:h:1
in your code.

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

採用された回答

Torsten
Torsten 2022 年 5 月 23 日
h=0.01;
t=0:h:1;
n=numel(t);
mu = 20;
f_m = @(t,y) mu*(y-cos(t))-sin(t);
exact = @(t) exp(mu*t)+cos(t);
%initials%
y_m(1)=exact(0);
y_m(2)=exact(h);
%Midpoint Two step method%
for i=3:n
y_m(i)=y_m(i-2)+2*h*f_m(t(i-1),y_m(i-1));
end
plot(t,[y_m;exact(t)])
  1 件のコメント
Hazel Can
Hazel Can 2022 年 5 月 24 日
Thank you very much for your answer!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by