solving ordinary differential equation

9 次查看(过去 30 天)
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

采纳的回答

James Tursa
James Tursa 2021-2-19
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 个评论
James Tursa
James Tursa 2021-2-20
@Jasneet Singh You seem to have ignored all of the code I suggested. Why not give it a try?

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2021-2-19
[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 个评论
Jasneet Singh
Jasneet Singh 2021-2-19
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by