solving ordinary differential equation
显示 更早的评论
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
采纳的回答
更多回答(1 个)
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
