In: Mechanical Engineering
Lets say I have a second order ODE: mx''+kx+bx'=kz+bz' ( an EOM where m=3000, b=2000, k=50000, and finally z=.01sin(3.5t)).
In Matlab, how would I use the Matlab command ode23 to solve this ode assuming zero initial conditions, range :0-20 seconds.
[t,x] = ode23(odefun,tspan,x0), where tspan = [t0 tf], integrates the system of differential equations x'=f(t,x) from t0 to tf with initial conditions x0. Each row in the solution array y corresponds to a value returned in column vector t.
Rewrite the second order equation as a system of first-order ODEs by making the substitution . The resulting system of first-order ODEs is
Function in Matlab:
function ypms = ypms(t,y)
ypms(1) = y(2);
ypms(2) = (50000/3000)*0.01*sin(3.5*t)
+(2000/3000)*0.01*3.5*cos(3.5*t) - (50000/3000)*y(1) -
(2000/3000)*y(2);
ypms = [ypms(1) ypms(2)]';
Matlab Code to run the function:
clear;
t0 = 0;
tf = 20;
x0 = [0 0];
[t x] = ode23('ypms' , [t0 tf],x0);
figure
plot(t,x(:,1))
title( 'Displacement vs Time' )
xlabel('t' )
ylabel('displacement' )
figure
plot(t,x(:,2))
title( 'Velocity vs Time' )
xlabel('t' )
ylabel('Velocity' )
[NOTE: First Save the function and then run the code]