MATLAB: Solving a System of 2nd Order Nonlinear ODEs

differential equationsengineeringmathematics

Hello I am a beginner at MATLAB. i am trying to solve the system of ODEs shown below, where I am trying to plot u=u(t) and theta=theta(t), and m, L, and g are just constants. Any help would be immensly appreciated. Thanks.

Best Answer

It is usually easier to let the Symbolic Math Toolbox do the ‘heavy lifting’:
syms g k L m th(t) t u(t) Y
th1 = diff(th); % dtheta/dt
th2 = diff(th1);
u1 = diff(u,1); % du/dt
u2 = diff(u1);
Eqs = [m*u2-m*L*sin(th)*th2+k*u-m*L*th1^2*cos(th); m*L^2*th2-m*u2*L*sin(th)-m*u1*L*cos(th)*th1+m*g*L*sin(th)];
[VF,Subs] = odeToVectorField(Eqs)
Eqns = matlabFunction(VF, 'Vars',{t,Y,g,k,L,m})
You can then use the numerical ODE solvers, such as ode45, to integrate the ‘Eqns’ anonymous function.
I leave the rest to you.