How to write ode45 function correctly

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by