STuck at coding second order differential equation (mx"+cx'+kx=f0coswt)

2 views (last 30 days)
Error at line 6. Could you help me, please.
function Xdot=num_for(t,X)
m=100; k=1000; c=160;
ze=c/(2*sqrt(k*m));
wn=sqrt(k/m);
w=5; F=160; f=F/m;
f=[0; f*cos(w*t)];
A=[0 1;-wn*wn -2*ze*wn];
Xdot=A*X+f;
Tspan=[0 10];
y0=[0.01;0.1];
[t,y]=ode45('num_for',Tspan,Y0);
plot(t,y(:,1))

Answers (1)

Star Strider
Star Strider on 24 Apr 2022
Refer to the ‘num_for’ function as a function handle. Quoted character arays ar not longer recognised.
Tspan=[0 10];
y0=[0.01;0.1];
[t,y]=ode45(@num_for,Tspan,y0);
figure
plot(t,y(:,1))
function Xdot=num_for(t,X)
m=100; k=1000; c=160;
ze=c/(2*sqrt(k*m));
wn=sqrt(k/m);
w=5; F=160; f=F/m;
f=[0; f*cos(w*t)];
A=[0 1;-wn*wn -2*ze*wn];
Xdot=A*X+f;
end
If you are unfamiliar with function handles, see What Is a Function Handle? for details.
.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!