How to write ode45 function correctly
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/376811/image.png)
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.
0 commentaires
Réponses (1)
Ameer Hamza
le 10 Oct 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
0 commentaires
Voir également
Catégories
En savoir plus sur Ordinary Differential Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!