solving ordinary differential equation

13 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

James Tursa
James Tursa il 19 Feb 2021
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( ).
  13 Commenti
James Tursa
James Tursa il 20 Feb 2021
@Jasneet Singh You seem to have ignored all of the code I suggested. Why not give it a try?

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 19 Feb 2021
[t,x]=ode45(@(t,x)EQ(tspan,x0,a,k,m,F,omega);
That line tells us that ode45 is to call an anonymous function passing in two parameters, and that it is to throw away the parameters and call EQ passing in 7 variables whose values had to exist at the time the @ function handle was constructed
But ode45 must be passed at least 3 parameters not just one. The second passed to ode45 must be the time span and the third must be the initial conditions. So ode45 would fail.
If you were to pass the time span and initial conditions to ode45 then it would try to call the anonymous function, which would require recalling those 7 variables. However only tspan and x0 exist, and it would fail trying to find the other ones.
function sol= EQ(t,x)
If the variables did all exist then matlab would try to call EQ but would fail because EQ only expects two parameters.
  1 Commento
Jasneet Singh
Jasneet Singh il 19 Feb 2021
I got your concern, I do have values for all parameters except x .. Then is it correct? I'm facing issue while combining scripts for function and ode when I try to run it. And also if I place any other variable in place of x and bring some changes to script, it says undefined variable x , t etc, Even though they are my outputs.

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by