How do I solve system of differentials using ode45 and eom functions

8 views (last 30 days)
Hello and thank you for taking the time to read my question!
I am trying to solve a system of differential equations for analytically and then numerically for and and plot the two for comparison. I also have to analytically find tension T in terms of theta then numerically find it in terms of time. The given equations for motion in normal-tangiential form are , and . The starting position is at rad with m=2 kg, g=9.81 m/s^2, and r=0.5 m.
Below is my code so far. I keep getting an error trying to run the ode45 function saying that inputs must be floats, namely a single or double. I have to use the ode45 function as a requirment for the assignment, but I'm not sure if I have to use the eom bit.
Error:
Error using superiorfloat (line 13)
Inputs must be floats,
namely single or double.
Error in odearguments (line 114)
dataType = superiorfloat(t0,y0,f0);
Error in ode45 (line 104)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Error in ESCI204_M1_Myers (line 74)
[T,S] = ode45(@eom,linspace(0,2,101),[0,0]);
Code:
%Givens:
theta0 = -pi/6;
m = 2;
g = 9.81;
r = 0.5;
syms Tens theta t thetaA
thetaA = linspace(-pi/6,pi/6,101);
% % DETERMINE ANALYTICALLY:------------------------------------------------
%Equation for angular velocity in terms of theta
thetadA = sqrt((-360*g/(pi*r))*(cos(theta0)-cos(thetaA)));
%Equation for Tension in terms of theta
TensA = 3*m*g*cos(thetaA)-2*m*g;
hold on
plot(thetaA,thetadA, 'black') %analytical solution for thetad(theta)
xlabel('Angular Position (rad) or time (t)')
ylabel('Angular Velocity (rad/sec)')
% % DETERMINE NUMERICALLY:
% 1) The angular velocity and angular position of the mass about point C
% as a function of time by integration of the equation of motion, thetadd
% 2) The tension in the string as a function of time.
function ds = eom(t,s) %S is current states - vector of current position and vel
%Givens:
theta0 = -pi/6;
m = 2;
g = 9.81;
r = 0.5;
syms Tens
ds(1,1) = sqrt((Tens-m*g*cos(s(1)))/(m*r)); % Derivative of position (s(1) = thetad
ds(2,1) = (-g*sin(s(1)))/r; % Derivative of velocity = thetadd
end
[T,S] = ode45(@eom,linspace(0,2,101),[0,0]);

Accepted Answer

Aquatris
Aquatris on 8 Aug 2024
Moved: Steven Lord on 8 Aug 2024
You defined a symbolic variable within your ode function, which is a nono if you wanna solve it numerically :D
function ds = eom(t,s) %S is current states - vector of current position and vel
%Givens:
theta0 = -pi/6;
m = 2;
g = 9.81;
r = 0.5;
syms Tens %======= THIS IS A NONO, plug in the values: Tens = 3*m*g*... =======%
ds(1,1) = sqrt((Tens-m*g*cos(s(1)))/(m*r)); % Derivative of position (s(1) = thetad
ds(2,1) = (-g*sin(s(1)))/r; % Derivative of velocity = thetadd
end
So plug in your values there
  4 Comments
Sam Chak
Sam Chak on 8 Aug 2024
The square in this differential equation suggests that there is another branch of solution.
The solution of is in the form of the Jacobi amplitude function.
Aquatris
Aquatris on 8 Aug 2024
Edited: Aquatris on 12 Aug 2024
The reason why you are getting 0s as solution is because you defined the initial condition as 0 in your ode with the [0,0] argument. It should be [theta0, 0] instead where theta0 is -pi/6. Also the second argument for ode45 function is the time vector so you should use a time vector with small enough step size, how small depends on the problem.
[t,x] = ode45('odeFunction here','time vec here','initial states here')
Second things is, I am not sure since I do not know the problem exactly, but from what I understand, the equation of motion is defined by the equation only. This completely defines your dynamics so you should solve this one analytically and numerically and compare. In analytical solution, I recommend you use small angle theorem to simplify the equation if it is a pendulum :)
The second equation, is there to calculate the T term after you solve your equation. So after you obtain your analytical and numerical solution, you should compare the resulting analytical T and numeric T by doing a simple .

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!