MATLAB: Solving ordinary differential equation

MATLABode45please help me understand logic behind every syntax error i commited!!

Hello , I am trying to solve an ode in matlab
but i'm getting syntax errors. pls help!!
my actual ODE is : m*d2xdt2 + a*(dxdt)^2 + k*x= Fcos(omega*t)
and i need a solution for x which is displacement for an object.
i am trying this way please correct me where i'm wrong
tspan=[0:1800];
x0=0;
[t,x]=ode45(@(t,x)EQ(tspan,x0,a,k,m,F,omega);
plot(t,x);
function sol= EQ(t,x)
a=1;
k=20;
m=0.5;
F=0.01;
omega=2*pi;
x=[(F/k)*cos(omega*t)- (m/k)*d2xdt2- (a/k)*(dxdt)^2]
sol=x ;
end

Best Answer

Starting with this differential equation:
m*d2xdt2 + a*(dxdt)^2 + k*x= F*cos(omega*t)
The first step is to solve the equation for the highest order derivative appearing in the equation. This results in:
d2xdt2 = (F*cos(omega*t) - a*(dxdt)^2 - k*x)/m
Now rewrite this as two first order equations using a 2-element vector y, where y(1) is defined to be x and y(2) is defined to be dxdt:
dy(1)dt = dxdt = y(2)
dy(2)dt = d(dxdt)dt = d2xdt2 = (F*cos(omega*t) - a*(dxdt)^2 - k*x)/m = (F*cos(omega*t) - a*(y(2))^2 - k*y(1))/m
From that you can define a derivative function. E.g., expressed in a function handle:
a = 1;
k = 20;
m = 0.5;
F = 0.01;
omega = 2*pi;
dydt = @(t,y) [y(2);(F*cos(omega*t) - a*y(2)^2 - k*y(1))/m];
This function handle is what you can pass to ode45( ).
Related Question