having problem plotting ramp function
古いコメントを表示

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 件のコメント
Shivam
2024 年 12 月 2 日
Hi Yin,
Could you attach the data dt for me to debug the issue? You can use the paperclick icon to attach the data.
Tien Yin
2024 年 12 月 2 日
Tien Yin
2024 年 12 月 2 日
@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
2024 年 12 月 3 日
採用された回答
その他の回答 (1 件)
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).
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)')
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


