having problem plotting ramp function
    4 views (last 30 days)
  
       Show older comments
    

the line f(0001:3000)=((f0/3)*dt);
was supposed to maek the f(t) plot having a ramp function of f0/3 * time when time is at 0~3 
I tried f(0001:3000)=((f0/3)*time) and it got an error which says "Unable to perform assignment because the left and right sides have a different number of elements."
please help
5 Comments
  VBBV
      
      
 on 2 Dec 2024
				
      Edited: VBBV
      
      
 on 3 Dec 2024
  
			@Tien Yin  You can modify the present code as below.  A better way to do is to use piecewise function as mentioned by @Sam Chak 
clc
clearvars
m=5;
k=18;
c=1.2;
f0=100;
wn=sqrt(k/m);
cc=2*m*wn;
Dr=c/cc;
wd=wn*sqrt(1-Dr^2);    
time=linspace(0,10,5001);
idx1 = find(time==3); % 
idx2 = find(time==5); % use find     
f=zeros(1,length(time));
for kx = 1:numel(time)-1
if kx <= idx1
    f(kx+1)=f(kx) + (f0/3)*(time(kx+1)-time(kx));
elseif kx>idx1 & kx<=idx2
    f(kx+1) = 100;
else
    f(kx+1) = 0;
end 
end    
figure;
plot(time,f);xlabel('t(s)'); ylabel('f(t)') 
xticks(1:1:10);grid
axis([0 10 0 110])   
Accepted Answer
  Shivam
      
 on 2 Dec 2024
        Hi Yin, 
You can use the below script to get the desired figures.
m = 5;
k = 18;
c = 1.2;
f0 = 100;
wn = sqrt(k/m);
cc = 2*m*wn;
Dr = c/cc;
wd = wn*sqrt(1-Dr^2);
dt = 0.01; % assumed dt
time = 0:dt:10;
f = zeros(1, length(time));
f(1:round(3/dt)) = (f0/3) * (0:dt:3-dt); % Linear increase from 0 to 3 seconds
f(round(3/dt)+1:round(5/dt)) = f0; % Constant from 3 to 5 seconds
% f remains 0 after 5 seconds
g = (1/(m*wd))*exp(-Dr*wn*time).*sin(wd*time);
x = conv(f, g)*dt;
x = x(1:length(time)); % Ensure x is the same length as time
figure;
subplot(221); plot(time, f, 'r'); xlabel('t(s)'); ylabel('f(t)');
subplot(222); plot(time, g); xlabel('t(s)'); ylabel('g(t)');
subplot(223); plot(time, x); xlabel('t(s)'); ylabel('位移(m)');
Hope it helps.
More Answers (1)
  Sam Chak
      
      
 on 2 Dec 2024
        Hi @Tien Yin
The signal in the image is a piecewise function that consists of three sub-functions defined over different intervals:  ,
,  ,
,  . You can apply the piecewise() function from the Symbolic Math Toolbox, or you can use the MATLAB indexing approach (though this method may not be suitable for publication in a journal).
. You can apply the piecewise() function from the Symbolic Math Toolbox, or you can use the MATLAB indexing approach (though this method may not be suitable for publication in a journal). 
 ,
,  ,
,  . You can apply the piecewise() function from the Symbolic Math Toolbox, or you can use the MATLAB indexing approach (though this method may not be suitable for publication in a journal).
. You can apply the piecewise() function from the Symbolic Math Toolbox, or you can use the MATLAB indexing approach (though this method may not be suitable for publication in a journal). Alternatively, you can use a direct math formula to combine the sub-functions into a one-line equation, as shown below. Since the second and third sub-functions can be described using a Heaviside function, this effectively reduces the representation to "two" sub-functions.
If you are interested, you can find another example here:

t   = linspace(0, 10, 1001);
t1  = 3;
t2  = 5;
% Functions at subintervals
f1  = 100/3*t;                  % y1, Ramp function      at t < t1
f2  = 100*heaviside(t2 - t);    % y2, Heaviside function at t > t1
% Applying the Piecewise Function Put Together (PFPT) formula
f   = f1 + (f2 - f1).*heaviside(t - t1);
% Plot
plot(t, f, 'linewidth', 1.5), grid on, ylim([-50, 150])
title('Piecewise Function')
xlabel('t')
ylabel('f(t)')
See Also
Categories
				Find more on Creating and Concatenating Matrices 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!





