How can I plot a time vs x-Displacement graph?

9 views (last 30 days)
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_Code1
g = 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), clf
plot(time,x), xlabel('Time (seconds)'), ylabel('displacement (m)')
title('X-Displacements VS Time')
figure(2), clf
plot(time,theta), xlabel('Time (seconds)'), ylabel('\theta (Degree)')
title('\theta VS Time')
figure(3), clf
plot(time,v), xlabel('Time (seconds)'), ylabel('Velocity (m/s)')
title('Velocity (X_dot) VS Time')
figure(4), clf
plot(time,omega), xlabel('Time (seconds)'), ylabel('\theta_dot (Degree)')
title('Angular Veolocity VS Time')
end

Accepted Answer

Ameer Hamza
Ameer Hamza on 2 Apr 2020
Your code is well structured. It used ode45, so it is as fast as it can get in MATLAB. It is unlikely that you can improve the performance significantly as compared to the current code.

More Answers (0)

Categories

Find more on Mathematics 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!