How to solve a second order non-homogeneous equation using Euler's approximation

2 ビュー (過去 30 日間)
I'm able to approximate a the following homogenous differential equation n''+n'+2n=0, n(0)=5, n'(0)=1 using:
%Defining functions
first=@(n,x,t) x;
second=@(n,x,t) -x-2*n;
%step size
T=.05;
%max t value
tf=10;
%Initial conditions
t(1)=0;
n(1)=5;
n2(1)=1;
%euler approximation
for i=1:(tf/T)
t(i+1)=t(i)+T;
n(i+1)=n(i)+T*first(n(i),n2(i)+t(i));
n2(i+1)=n2(i)+T*second(n(i),n2(i)+t(i));
end
plot(t,n)
However, how should I edit the code above to solve a non-homogenous variation n''+n'+2n=cos(t), with the same initial conditions? Thank you.

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 6 月 7 日
編集済み: Ameer Hamza 2020 年 6 月 7 日
First, there is a mistake in your equation of Euler method
n(i+1)=n(i)+T*first(n(i),n2(i),t(i)); % there should be a comma between n2(i) and t(i)
n2(i+1)=n2(i)+T*second(n(i),n2(i),t(i)); % there should be a comma between n2(i) and t(i)
You can use cos(t) as input by changing the function 'second'
second=@(n,x,t) -x-2*n+cos(t);

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by