フィルターのクリア

Going from Euler's method to trapezoidal rule

1 回表示 (過去 30 日間)
Chloe Keller
Chloe Keller 2018 年 7 月 15 日
回答済み: Swaraj 2023 年 2 月 9 日
I used Euler's Method to solve y'= 1−t+4y with y(0) = 1 on the interval [0, 2], h = 0.01. I posted my code below. How would I edit this code to solve the same problem using the Trapezoidal Rule?
if true
syms t y
f=@(t,y) 1-t+4.*y;
t=0;
y=1;
h=.01;
n=(2-0)/h;
for i=1:n
m=f(t(i),(y(i)));
y=[y, y(i)+m*h]; %y(i+1)=y(i)+m*h
t=[t, t(i)+h]; %t(i+1)=t(i)+h
end
for i=1:length(t)
fprintf('%5.4f | %5.4f\n',t(i),y(i))
end
end
  2 件のコメント
DINU ANDREEA-EMILIA
DINU ANDREEA-EMILIA 2020 年 6 月 3 日
Did you find the answer to your question? Cause i have to do the exact same thing
James Tursa
James Tursa 2020 年 6 月 3 日

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

回答 (1 件)

Swaraj
Swaraj 2023 年 2 月 9 日
You would have to modify the calculation of the intermediate value m as follows:
if true
syms t y
f=@(t,y) 1-t+4.*y;
t=0;
y=1;
h=.01;
n=(2-0)/h;
%Changed Part Start
for i=1:n
m1 = f(t(i),y(i));
m2 = f(t(i) + h/2, y(i) + m1*h/2);
y = [y, y(i) + m2*h];
t = [t, t(i) + h];
end
%Changed Part End
for i=1:length(t)
fprintf('%5.4f | %5.4f\n',t(i),y(i))
end
end

カテゴリ

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

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by