# How to get range of values from a plot?

29 views (last 30 days)
Susmita Panda on 4 Apr 2021
Commented: Susmita Panda on 20 Apr 2021
Hi,
Firstly, I am doubful about getting velocity and acceleration using 'gradient' function instead of 'diff' to get velocity and acceleration from displacement 'y2' in time domain.Is it the correct way to do? secondly, I am trying to get specific range of values from my graph but have no idea how to do?
For example, I just want values in time domain with 0 to 2.5 in x-axis or in between 2.5 to 10, and interpolate to get the perfect nearest one in time axis? I want such portion because I want to add another section of graph from free vibration analysis, this the the response using forced vibration only.
Thanks
Here is my code:
clear all
close all
clc
a=0.5;beta=0.025;wb=14.25;gamma = 1e-10; % introduce gamma
% Define time and frequency axis variables
fs =400; % samples/s
N = 1e6; % number of points
dt = 1 / fs; % s, time step
t = (0:N-1)*dt; % s, time axis
df = 1 / N / dt; % Hz, frequency step
f = (-N/2:N/2-1)*df; % Hz, frequency axis
eta_dom=0.15:0.1:1;
len_eta=length(eta_dom);
len_f=length(f);
X_store=zeros(len_eta,len_f);
% Define function first mode
for j=1:len_eta
eta=eta_dom(j)
y3=sin(pi.*a).*eta.*(1+exp(-1i.*pi.*f./eta))./((-f.^2+(2.*1i).*f.*beta+1).*(eta.^2-i*f*gamma-f.^2));
y4=sin(pi.*a).*eta.*(1+exp(-1i.*pi.*f./eta))./((-f.^2+(2.*1i).*f.*beta+1).*(eta.^2+i*f*gamma-f.^2));
yy = (y3+y4)/2;
%plot original signal
figure(1)
% plot initial time domain signal
plot(f,yy),title('Frequency response');ylabel('Displacement Response of beam');xlabel('Frequency');
grid on
%conversion to time domain
y2 = ifft(ifftshift(yy));
X_store(j,:)=y2;
figure(2)
plot(t,(y2)),title('Time response');ylabel(' Displacement Response of beam');xlabel('Time');hold on;
axis([0 2.5 -0.03 0.03]); % time domain signal after IFFT
grid on
%% To calculate velocity and acceleration
figure(3),plot(t,(v)),title('Time response');ylabel('velocity Response of beam');xlabel('Time');hold on;
axis([0 2.5 -0.001 0.001]);
figure(4),plot(t,(acc)),title('Time response');ylabel('acceleration Response of beam');xlabel('Time');%hold on;
axis([0 2.5 -0.00002 0.00002]);
end
figure(5)
cmap=[jet(9)];
for k=1:9
plot(t,X_store(k,:),'color',cmap(k,:),'linewidth',1.5);axis([0 0.7 -0.03 0.03]); hold on;colorbar
end
hold off;
Susmita Panda on 5 Apr 2021
I want to add the following reults to figure(5):
function dy=testode2(~,y)
global m k c
dy=[-k*y(2)/m-c*y(1)/m;y(1)];
end
clc;
clear all;
close all;
m=1;k=1;c=1;
global m k c
dt=0.005;
t=0:0.0001:20;
y0=[4.619*10^(-5) 0.021];
[tsol,ysol]=ode45('testode2',t,y0);
damping_ratio=c/(2*sqrt(k*m))
%%plot (displ vs time)
figure(1),plot(t,ysol(:,2));title('Displacement vs Time');
grid on
%%plot (velocity vs time)
figure(2),plot(t,ysol(:,1));title('Velocity vs Time')
grid on

The difference between gradient and diff is that the first calculates central differences and the second forward differences, both should give you an approximated right differentiation, although their numerical stabilities are different. In your case it shouldn't matter too much. Specifically for the case of displacement, velocity and acceleration, what is often done is a differentiation/integration in frequency domain. This can be done by multiplying/dividing your spectrum by 1j*w, where w = 2*pi*f.
If you only want the values between 0 and 2.5 s or any other interval you can find the respectively values by using logic indexing like this:
a=0.5;beta=0.025;wb=14.25;gamma = 1e-10; % introduce gamma
% Define time and frequency axis variables
fs =400; % samples/s
N = 1e6; % number of points
dt = 1 / fs; % s, time step
t = (0:N-1)*dt; % s, time axis
startTime = 0;
endTime = 2.5;
validIndices = find(t>startTime & t<endTime);
tTruncated = t(validIndices);
y2Truncated = t(validIndices);
validIndices
validIndices = 1×999
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
To interpolate values in between you can take a look at the interp1 function.
Susmita Panda on 20 Apr 2021
How to add this portion of graph (0 to 0.7 axis values) to the other section ? Help is highly appreciated.

### Categories

Find more on Vibration Analysis in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by