Euler's approximation for ODE

4 ビュー (過去 30 日間)
MC
MC 2019 年 9 月 23 日
コメント済み: darova 2019 年 9 月 23 日
I want want to use MATLAB to implement Euler’s method with each step size h= 0.2,0.1,0.05 for 0≤t≤1 and plot the results in a single figure. I would like to also solve the ODE and plot the exact solution in the same in compare the two.
How do i fix this error:
Error: File: M1.m Line: 5 Column: 7
Invalid use of operator.
euler is a function i wrote.
f = @(t,y) t*y-y;
y0 = 0.5;
i = 0.2;
for i >= 0.05
t = 0:i:25;
[t,y1] = euler(f,t,y0);
i=i/2
plot(t,y1)
end
y2 = -1 ./ (t.^2/2 + 10);
plot(t,y2)
legend('Euler','Exact solution')
save(M1.fig)
  1 件のコメント
darova
darova 2019 年 9 月 23 日
It's not Euler's method
[t,y1] = euler(f,t,y0);

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

回答 (1 件)

Lucademicus
Lucademicus 2019 年 9 月 23 日
編集済み: Lucademicus 2019 年 9 月 23 日
for i >= 0.05
This is not the correct way to define a for-loop.
Also, with the error message MATLAB tries to point you in the right direction, by telling you 1) what is not working, 2) where it is not working. I would want to advise you to take a look at what line number (and column number) MATLAB mentions and then looking in the documentation regarding the code you're using on that line.

カテゴリ

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