Euler's method

1 回表示 (過去 30 日間)
Paul Jackson
Paul Jackson 2021 年 9 月 23 日
コメント済み: Paul 2021 年 9 月 24 日
I was given a semi-finished chunk of code and asked to implement euler's method to solve it.
I've looked at countless examples of implementations but i think the finished chunk confuse me
I have acces to the function myode(t,y) that should give me a specific solution based on t and y
This is the code I was given.
dt=.1;
t_values=[0:dt:500];
numsteps=length(t_values);
y_initial=[50;1];
y_values=zeros(2,numsteps);
y_values(:,1)=y_initial;
for i1=2:numsteps;
y_values(:,i1)=
The issue I want help with is understanding how to utilize the pre-written code, my current attempt is
y_values(:,i1)= y_values(i1) + dt * myode(t_values(i1),y_values(i1))
Any help is greatly appreciated. I've tried rewriting the code back and forth for a day now and i'm not making any progress.
Best regards

採用された回答

Geoff Hayes
Geoff Hayes 2021 年 9 月 23 日
Paul (Adam?) - I think all that you are missing is in your assignment
y_values(:,i1)= y_values(i1) + dt * myode(t_values(i1),y_values(i1))
Remember that y_values is a 2D array..so you will need to access both rows for every column. The assignment does this with
y_values(:,i1)=
and so you will need to do the same (else you will get assignment errors or maybe even errors with myode since it might be expecting a 2x1 array as the second input to this function.
Secondly, each step of the algorithm uses the results of the previous iteration. Currently, your code is using i1 on both sides of the assignment. Since the minimum value of i1 is 2, then you can access the previous value with i1-1. Try the following
y_values(:,i1)= y_values(:,i1-1) + dt * myode(t_values(i1-1),y_values(i1-1));
  1 件のコメント
Paul
Paul 2021 年 9 月 24 日
Should be:
y_values(:,i1)= y_values(:,i1-1) + dt * myode(t_values(i1-1),y_values(:,i1-1));

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by