Euler Method without using ODE solvers such as ode45
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I am trying to write a code that will solve a first order differential equation using Euler's method. I do not want to use an ode solver but rather would like to use numerical methods which allow me to calculate slope (k1, k2 values, etc). I am given an equation with two different step values. I am not sure how to begin to write this in MATLAB. I have solved the equation by hand and am now trying to write a code that solves that equation.
The equation to be used is y’ +2y = 2 – e -4t.
y(0) = 1, which has an exact solution:
y(t) = 1 + 0.5 e -4t - 0.5 e -2t .
I did the following to solve numerically: 1+(0.5e^-4t) - (0.5e^-2(0)) y_n+1=1+0.1 and y_n+1=1+1(.001)
The step sizes are 0.1 and 0.001. (so my h in this example). The t values range from 0.1 to 5.0.
Any help is appreciated even if its an example pseudocode. Thank you
採用された回答
Given the equation:y' + 2y = 2 - 1e-4t The approximation will be: y(t+h) = y(t) +h*(- 2*y(t)+ 2 - e(-4t))
To write this: EDIT
y = 1; % y at t = 0
h = 0.001;
t_final = 0.1;
t = 0;
while (t < t_final)
y = y +h*(- 2*y+ 2 - exp(-4*t));
t = t + h;
end
11 件のコメント
KayLynn
2014 年 2 月 8 日
My mistake the equation should be: y’ +2y = 2 – e^(-4t). Meaning that e is raised to the negative 4t.
y(t) = 1 + 0.5 e^(-4t) - 0.5 e^ (-2t)
Sorry for the confusion
KayLynn
2014 年 2 月 8 日
The following is the code that I am trying to implement. I am unsure of the total steps since t ranges from 0 to 5 in increments of 0.1. ANy help is again appreciated. Thank you %Rearrange your equation to represent the following: y'= 2-e^-4*t-2*y; %define your f(t,y) f(t,y)=2-e^-4*t-2*y; %define initial t and initial y; define step size(h) and number of steps(n) t0=0; y0=1; h=0.1; n= %Write for loop for i=1:numel(n)-1 m=f(t0,y0); y=y0 +h*m; t1=t0+ht0; Print t1,y1; t0=t1; y0=y1; end
Amit
2014 年 2 月 8 日
I have corrected the code accordingly. Your code will be similar to the one I wrote above.
Can you format your code with code attributes so that I can see it.
y'= 2-e^-4*t-2*y;
f(t,y)=2-e^-4*t-2*y;
t0=0; y0=1; h=0.1; n=
for i=1:numel(n)-1
m=f(t0,y0);
y=y0 +h*m;
t1=t0+ht0;
Print t1,y1;
t0=t1;
y0=y1;
end
I am not sure the number of total steps(n) since my t values go from 0 to 5.0 with a step size of 0.1
Was using this code I found online: define f(t,y)
input t0 and y0.
input step size, h and the number of steps, n.
for j from 1 to n do m = f (t0, y0) y1 = y0 + h*m t1 = t0 + h Print t1 and y1 t0 = t1 y0 = y1 end
Okay if you want to follow this way, then try this:
f = @(t,y) (2 - exp(-4*t) - 2*y);
h = 0.1; % Define Step Size
t_final = 5;
t = 0:h:t_final;
y = zeros(1,numel(t));
y(1) = 1; % y0
% You know the value a t = 0, thats why you'll state with t = h i.e. i = 2
for i = 2:numel(t)
y(i) = y(i-1) + h*f(t(i-1),y(i-1));
disp([t(i) y(i)]);
end
KayLynn
2014 年 2 月 8 日
Thank you so much. And then for my step size of 0.001 I would just change the h variable in said above code to 0.001
Amit
2014 年 2 月 8 日
Yup !
John D'Errico
2014 年 2 月 8 日
How can you be unsure of the total number of steps if you know the stepsize, and you know how far it must go? Surely division is the proper tool here. 5/0.1
Erin W
2016 年 9 月 20 日
Amit,
How did you determine the approximation for the equation?
I have a similar problem I am trying to solve only I have y' = ry(1-y/K) where r = 1, K = 10, and yo = 1.
I understand the code that you have written but I'm unsure of how to alter my function.
"Given the equation:y' + 2y = 2 - 1e-4t The approximation will be: y(t+h) = y(t) +h*(- 2*y(t)+ 2 - e(-4t))"
James Tursa
2016 年 9 月 20 日
@Erin: Open a new Question with your specific problem, what code you have written so far, and what specific things you need help with.
Erin W
2016 年 9 月 22 日
@James I did but I haven't been responded to yet :(
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Mathematics についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
