How to write ode45 function correctly

1 visualización (últimos 30 días)
Teb Keb
Teb Keb el 10 de Oct. de 2020
Respondida: Ameer Hamza el 10 de Oct. de 2020
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.

Respuestas (1)

Ameer Hamza
Ameer Hamza el 10 de Oct. de 2020
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

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by