# How to shift graphs in Matlab?

36 views (last 30 days)
Robert Flores on 5 May 2018
Commented: Jan on 15 May 2018
Hello,
I am trying to figure out how to start my graph correctly. Time is starting at zero, however my statements are starting at 2 and just putting a zero for my first element, not 1,2. They should be starting from the same y-intercept and then vary after time equals 7.45ish. The first figure is without the correction, and the second image is if I try and fix the shift with changing line 114 with 1/10 - 1/10. If you can help me with this issue that will be great.
CODE:
close all
clear all, clc
g = 9.81; %acceleration due to gravity m/s^2
l = 20; % length of rope from top of platform
L = l+9;
h = 91-l; % the total displacement of the spring
d = l +h; % the height of the platform
m = 100; % mass in kg
k = 2*m*g*d/h^2; % spring constant
c =10; % damping constant
x0 = -m*g/k; % position at time = 0, when the spring engages
v0 = sqrt(2*g*l); % velocity at time =0, when the spring engages
wn = sqrt(k/m); % omega_n, natural frequency, radians/sec
z = c/(2*m*wn); % zeta, damping ratio,
wd = wn*sqrt(1-z^2); % damping frequency, radians/sec
A = sqrt(((v0+z*wn*x0)^2+(wd*x0)^2)/(wd)^2); % amplitude
phi = atan(wd*x0/(v0+z*wn*x0)); % phase angle
syms t
x = A.*exp(-z.*wn.*t).*sin(wd.*t+phi); % position function
v = diff(x,t); % velocity function
a = diff(v,t); % accleration function
j = diff(a,t); % jerk function
% x is maximum when velocity is zero
t_x_max = double(solve(v==0,t)); % solves for time when v is zero
t_x_max = t_x_max(t_x_max>=0); % selects the positive time values
t = t_x_max;
x_max = double(subs(x)); % maximum displacement for time when v is zero
x_total = x_max+abs(x0) % x is measured from the equilibrium position. This adds
%the initial position to the displacement from the equilibrium postion.
if x_total > h
disp('Splat!')
else
disp('Jumper is safe')
end
% Velocity is maximum when acceleration is zero.
syms t
t_v_max = double(solve(a==0,t)); % time when a is zero.
t_v_max = min(t_v_max);
t = t_v_max;
v_max = double(subs(v)); % maximum velocity
% acceleration is maximum when jerk is zero.
syms t
t_a_max = double(solve(j==0,t));
t_a_max = t_a_max(t_a_max >=0);
t = t_a_max;
a_max = double(subs(a)); % maximum acceleration
gs = abs(a_max/g) % accleration/acceleration due to gravity gives the number of gs.
if gs > 2
disp('Black out')
else
disp('Jumper is awake')
end
Td = 2*pi/wd; % damped period
n = 0:20;
time = Td*n/2+t_x_max; % time at which x peaks occur
t = time;
x_peaks = double(subs(x)); % x peak values
for i = 1:1:numel(x_peaks)
if abs(x_peaks(i))<4
t_over = time(i);
fprintf('Ride ends at %g \n', t_over)
break
end
end
% subplot(3,1,1)
% scatter(time,x_peaks)
% hold on
% plot(time,4)
% axis([0 60 -60 60])
% syms t
% subplot(3,1,2)
% fplot(x)
% hold on
% fplot(4)
% axis([0 60 -60 60])
% subplot(3,1,3)
% fplot(v)
% axis([0 60 -60 60])
% hold off
t_x0 = vpasolve(x==x0, 8)
C = m*g*l/(.5*int(v^2,0,t_x0))
clc
syms t
free_fall = x0+v0.*t+1/2.*g.*t.^2;
ff_vel = diff(free_fall);
%X- True position
X = zeros(1,601);
for i = 1:1:601
t = i/10;
if subs(x)>x0
X(i) = subs(x);
else
v0= subs(v);
end
if subs(free_fall)<x0
X(i) = subs(free_fall);
else
v0 = subs(ff_vel);
end
end
t= [0:.1:60];
plot(t,X,'R--')
hold on
fplot(x)
xlim([0 10])
hold off
% t_x0 = vpasolve(x==x0, 7)
%
% t = t_x0
% v_x0 = subs(v)
%
% x = free_fall;
% t_x0 = vpasolve(x==x0,

Krishna Bindumadhavan on 15 May 2018
"Time is starting at zero, however my statements are starting at 2 and just putting a zero for my first element, not 1,2. They should be starting from the same y-intercept and then vary after time equals 7.45ish."
^ Could you please elaborate this with respect to the code? I was not able to understand what you are trying to say here.
Jan on 15 May 2018
Inserting a blank line after each line of code decreases the readability. Please don't do this. Remember, that the outcommented sections are confusing in general, most of all, if you post code in the forum.