I am fairly new to MatLab and currently trying to learn it as I go along. here is the code I have writen so far. Did I write the code correctly or is there another way to do this (more efficently) .
function New_Code1g = 9.81; % acceleration due to gravity (m/s^2)
m = 2; % mass of weight (kg)
L = .5; % Length of Bar AB (m)
%Initial Conditions (IC)
x0 = 0; % Initial starting point(m)
x_dot0 = 0; % inital velocity (m/s)
theta0 = 60; % initial starting angle (degrees)
theta_dot0 = 0; % intial angular velocity (rad/s)
%I_alpha = (115/48)*m*L^2; % mass moment of inertial as caculated
IC = [x0, x_dot0, theta0, theta_dot0];%Time Span
t0 = 0; %inital time (s)
tf =10; % Final time (s)
tspan = [t0,tf];%sdot = general_function_of(t,s)
sdot = @(t,s) ...[s(2);(5/6)*L*(((theta_dot0)^2)*sind(s(3))*tf + (24/23)*(g/L)*cosd(s(3))*sind(s(3)))*tf; s(4) ; -(24/23)*(g/L)*sind(s(3))*tf];%sdot = @(t,s) ...
%[(5/6)*L*(((s(4))^2)*sind(s(3)) + (24/23)*(g/L)*cosd(s(3))*sind(s(3))); 0; -(24/23)*(g/L)*sind(s(3)); 0];
% calling ode45
[time, state_values] = ode45(sdot, tspan,IC);x = state_values (:,1);v = state_values (:,2);theta = state_values(:,3);omega = state_values(:,4);%plot
figure(1), clfplot(time,x), xlabel('Time (seconds)'), ylabel('displacement (m)')title('X-Displacements VS Time')figure(2), clfplot(time,theta), xlabel('Time (seconds)'), ylabel('\theta (Degree)')title('\theta VS Time')figure(3), clfplot(time,v), xlabel('Time (seconds)'), ylabel('Velocity (m/s)')title('Velocity (X_dot) VS Time')figure(4), clfplot(time,omega), xlabel('Time (seconds)'), ylabel('\theta_dot (Degree)')title('Angular Veolocity VS Time')end
Best Answer