How to write ode45 function correctly

1 回表示 (過去 30 日間)
Teb Keb
Teb Keb 2020 年 10 月 10 日
回答済み: Ameer Hamza 2020 年 10 月 10 日
I am trying to solve 2nd order ODE using matlab and I am having a problem with my function file.
My original equation is as follows:
I wrote my 2nd order in terms of 1st order and have 2 sets of equation as the following:
x1=x
x2=dx
And my 1st order ode’s are:
dx=x2
dxdt=b1*x1-a*x2+b2*x1^4+4t
The function I wrote in matlab is:
function dxdt= model(t,x,m,a,b1,b2)
x=x(1);
dx=x(2);
dxdt(1)=dx;
dxdt(2)=b1*x-a*dx+b2*x^4+4*t;
end
But matlab kept saying there is an error at x=x(1). I know x1 and x2 are vectors but I am not sure how to write it in this function.

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 10 月 10 日
You are oberwriting variable 'x' inside model(). Try this
m = 1;
a = 0.5;
b1 = 0.1;
b2 = 0.2;
tspan = [0 10];
ic = [1; 0];
ode45(@(t,x) model(t,x,m,a,b1,b2), tspan, ic)
function dxdt= model(t,x,m,a,b1,b2)
x_=x(1);
dx=x(2);
dxdt(1)=dx;
dxdt(2)=b1*x_-a*dx+b2*x_^4+4*t;
dxdt = dxdt(:); % it must return a column matrix
end

カテゴリ

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

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by