having problem plotting ramp function

조회 수: 65 (최근 30일)
Tien Yin
Tien Yin 2024년 12월 2일 7:41
댓글: Tien Yin 2024년 12월 3일 15:50
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
VBBV
VBBV 2024년 12월 2일 14:22
편집: VBBV 2024년 12월 3일 2:45
@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])
Tien Yin
Tien Yin 2024년 12월 3일 15:50
Thanks! So far I think using heaviside is a clearer way to present function too

댓글을 달려면 로그인하십시오.

채택된 답변

Shivam
Shivam 2024년 12월 2일 8:41
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.
  댓글 수: 1
Tien Yin
Tien Yin 2024년 12월 2일 9:11
THANKS ALOT
Appreciate your hard work

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sam Chak
Sam Chak 2024년 12월 2일 9:56
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).
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)')
  댓글 수: 1
Tien Yin
Tien Yin 2024년 12월 2일 16:47
thanks alot!
i'm new to MATLAB now I learnt a new way to display piecewise function!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear Prediction에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by