Solving a System of 2nd Order Nonlinear ODEs
Show older comments
Hi,
I am having some issues plotting this two equations. For some reason MATLAB is taking forever to solve. Is my syntax incorrect? See attached equation

function dydt = odefcn(t, y)
% y1 = x
% y2 = theta
% y3 = xdot
% y4 = thetadot
dydt = zeros(4,1);
dydt(1) = y(3); % linear speed
dydt(2) = y(4); % angular speed
dydt(3) = ((6*sin(y(2)))/((-3*(sin(y(2)))^2)+8))*((-1.5/2)*sin(y(2)) - (4/(3*sin(y(2))))*(-(cos(y(2))/4)*(y(4))^2 + 0.1*y(3) + y(1))); % linear acceleration
dydt(4) = ((((-6*sin(y(2)))/(-3*(sin(y(2)))^2))*((-1.5/2)*sin(y(2)) - (4/(3*sin(y(2))))*(-(cos(y(2))/4)*(y(4))^2 + 0.1*y(3) + y(1)))) - ((cos(y(2)))/4)*(y(4))^2 + 0.1*y(3) + y(1))*(4/sin(y(2))); % angular acceleration
end
function mpz
close all
clc
tspan = [0 2]; % time span of simulation
y0 = [0.5 pi/3 0 0]; % initial values
[t, y] = ode45(@(t, y) odefcn(t, y), tspan, y0);
plot(t, y(1), 'linewidth', 1.5)
grid on
xlabel('Time, t [sec]')
ylabel({'$x,\; \theta,\; \dot{x},\; \dot{\theta}$'}, 'Interpreter', 'latex')
title('Time responses of the system states')
legend({'x', '$\theta$', '$\dot{x}$', '$\dot{\theta}$'}, 'Interpreter', 'latex', 'location', 'best')
end
Accepted Answer
More Answers (1)
William Rose
on 8 Apr 2022
1 vote
@mpz,
Please show how you converted the two differential equations, labeleled "7" and "8" in your figure, to a set of first order O.D.E.s. I think that this set of ODEs is implicit, i.e. you will have to use a mass matrix in the solution. You can still use ode45() with an implicit set of equations. Specify the mass matrix using the Mass option of odeset. See the help for details.
6 Comments
mpz
on 8 Apr 2022
mpz
on 8 Apr 2022
William Rose
on 8 Apr 2022
Edited: William Rose
on 8 Apr 2022
[edit: fix error in eq. 10, eq.12, M]
Define:

Then equation 7 becomes
Equation 8 becomes
Rewrite 9:
Rewrite 10:
Now we have
where
and f(y) are column vectors, and
,
See https://www.mathworks.com/help/matlab/math/solve-equations-of-motion-for-baton.html for how to code this. Figure out the equations yourself and see if you agree with my results above.
William Rose
on 8 Apr 2022
@mpz,
Maybe you have already inverted the mass matrix and multiplied
. If so, your way should work, and you don't have to use the mass matix option.
. If so, your way should work, and you don't have to use the mass matix option.I see I made a mistake in equation 10 above. That mistake propagates into equation 12 and the equation for matrix M.
mpz
on 8 Apr 2022
William Rose
on 8 Apr 2022
My mass matrix equation had an error in M(4,4), which I have now fixed in my comment above. I omitted a factor of 0.25 from M(4,4). If you update your code to include the corrected M(4,4), does it run better?
Try setting the Mass state dependence to 'strong' - see help for odeset. Example:
opts = odeset('Mass',@M,'MStateDependence','strong');
If you still get errors or have problems, you could try a different ODE solver. Review the recommendations here. Maybe ode15s or ode15i.
Categories
Find more on Programming 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!






