Hello all, This was a homework assignment for a class on thermodynamics. The MATLAB code I will post below gives the correct answer while my sagemath code that was in a worksheet gives completely different and ludicrous answers and I can't figure out what the difference is between the two. I ran the sage worksheet on cloud.sagemath.com, which is awesome by the way. I am pretty new to Sage but can definitely see the power and ease of use that it brings. The MATLAB code is split into two files, one of which is a function called by the second file.
function [ F ] = ODE( t , y )
% This function is to be used in conjunction with file Project1.m
% It uses a reduction of order to turn a 2nd order ODE into a system
% of first order ODE's. All y(1) and y(2) are the slopes of the velocity
% the acceleration curves respectively. Z is the height of the object,
% wn is the natural frequency of the system. These two items are
% specifically noted here because they are functions of time in this
% system. F(1) stores values of the position, and F(2) stores the velocity.
global g m zeta V
z = V*t;
wn = sqrt((600000/(24-z)/m));
Forcing = 2*zeta*wn*(V-y(2)) + wn^2*(z-y(1));
if Forcing < 0 % check point for forcing function (no pushing of rope)
L = 0;
else
L = Forcing;
end
F = zeros(2,1);
F(1) = y(2);
F(2) = L - g;
%F(2) = g + 2*zeta*wn*V + wn^2*z - 2*zeta*wn*y(2) - wn^2*y(1);
end
End of first MATLAB file, ODE.m Sorry having trouble with the formatting.
% Define Constants
global g m zeta V
g = 32.2; % ft/s^2
%m = 170; % lbm
m = 5.2795; % slugs
zeta = 0.1; % damping ratio
V = 2; % ft/s
%% Drum Calculations
GR = 75; % Gear Ratio
RPSin = 1275/60; % Input RPM from Motor (rev/sec with /60)
RPSout = RPSin/GR; % Output rev/sec for drum
w_motor = RPSin*2*pi; % Omega of Motor (Rad/sec)
f_drum = RPSout; % Natural frequency of drum (Hz)
w_drum = RPSout*2*pi; % Natural rotational freq (rad/s)
drad = V/w_drum; % Drum radius (ft)
ddiam = drad*2; % Drum diameter (ft)
%% ODE Solution
% Example 1
y=[0 0]; % These are the initial conditions of the system. It begins at
% z=0 with velocity=0. The initial jerk of the cable sets the system in
% motion.
tspan=[0 1]; % This is the time span that the solution will be analyzed.
options=odeset('RelTol',1e-4,'AbsTol',[1e-4, 1e-4]);
% Options defines the error that the ode45 function uses to determine the
% time step.
[T,YV]=ode45(@ODE,tspan,y,options);
% ode45 is a 4th order Runge Kutta ode solver. It also uses a 5th order
% Runge Kutta to correct the time step and reduce error. T is the time
% Step of the solver and YV is an array indicating the displacement
% and velocity. Note that ode45 is calling an ODE that is in a different
% script.
Y=YV(:,1); % This is the displacements at time t of the solution.
v=YV(:,2); % This is the velocities at time t of the solution.
% Force Time Function
for i = 1:length(T)-1
a(i) = (v(i+1)-v(i))/(T(i+1)-T(i));
F(i) = m*a(i)+m*g;
Torque(i) = F(i)*drad;
Power(i) = Torque(i) * w_motor;
end
a(length(T)) = a(length(T)-1);
F(length(T)) = F(length(T)-1);
Torque(length(T)) = Torque(length(T)-1);
Power(length(T)) = Power(length(T)-1);
%% Power Calculations
Fmax = max(F);
Tmax = max(Torque);
Pmax = Tmax * w_drum;
Tmax_motor = Tmax/GR;
Pmax_motor = Tmax_motor*w_motor;
%% Plot results
figure(1);clf(1);
subplot(2,1,1); % Subplot has floating axes
plot(T,Y);
xlabel('Time (sec)');
ylabel('displacement (ft)');
subplot(2,1,2);
plot(T,v);
xlabel('Time (sec)');
ylabel('veolcity (ft/s)');
figure(2);clf(2);
subplot(2,1,1);
plot(T,a);
ylabel('Acceleration (ft/s^2)');
xlabel('Time (sec)');
subplot(2,1,2);
plot(T,F);
xlabel('Time (sec)');
ylabel('Force (lbf)');
figure(3);clf(3);
plot(T,Power)
xlabel('Time (sec)');
ylabel('Power (lbf*ft/sec)');
%% Print Values of Interest
fprintf('Max Force in The Rope (N) %4.2f\n',Fmax);
fprintf('Required motor power (HP) %4.2f\n',Pmax*0.001818);
fprintf('Required Drum Diameter (in) %4.2f\n',ddiam);
Here is the public access link to the sagemath worksheet on the cloud.sagemath.com website, which has embedded explanatory text as to what is happening:
I can't post links yet so add preview.tinyurl.com/ before the following: SageHWhelp
Thank you in advance for the help! This is driving me crazy!