how to repeat a function after fixed interval?

조회 수: 6 (최근 30일)
Pulak Kumar Ray
Pulak Kumar Ray 2023년 9월 6일
댓글: Dyuman Joshi 2023년 9월 6일
Below is the code.
clc
clear all
t=0:0.01:1;
for i=1:length(t)
y(i)=2*sqrt((6*(t(i))));
end
plot(t,y)
#########################################
The above code runs one time and plots in one time interval od 1s (t=0:0.01:1 - as defined in fourth line of code).
I want it to be repeated for several times in a larger set of t (say t in 0:100) with same time interval (as defined in fourth line of code). Please help me do this.

답변 (2개)

Dyuman Joshi
Dyuman Joshi 2023년 9월 6일
Like this?
incr = 0.01;
t = (0:incr:1)';
y = 2*(6*t).^(1/2);
%For 100, the plot looks quite cramped
val=10;
t0 = (0:val-1);
T = t+t0;
plot(T,y)
  댓글 수: 3
Dyuman Joshi
Dyuman Joshi 2023년 9월 6일
"As it appears, the second part code you wrote is just for plotting purposes."
t0 was added to t to repeat the given part multiple times.
T is a mxn array, whereas y is mx1 array. (m and n are placeholders)
Refer to this particular part of documentation of plot() for more info - Input Arguements to plot()
I understand now that you want to make a continuous plot.
Method 1 -
incr1 = 0.01;
t = (0:incr1:1)';
y = 2*(6*t).^(1/2);
val=10;
incr2 = max(t)-min(t);
t0 = (0:incr2:val-1);
T = t+t0;
%To plot as a single line
%reshape T as a vector, and repeat values in y according to the size
T = reshape(T,[],1);
y = repmat(y,val,1);
plot(T,y,'r-')
xlim([-1 11])
As you can see, In this method the plot ends at an upper value. You can do slight modifications -
T = [T;T(end)];
y = [y;0];
figure
plot(T,y,'b-')
xlim([-1 11])
Dyuman Joshi
Dyuman Joshi 2023년 9월 6일
Method 2
You can incorporate the above modification in the first step, so that the curve that repeats itself end on a lower value -
incr1 = 0.01;
t = (0:incr1:1)';
y = 2*(6*t).^(1/2);
t = [t;t(end)];
y = [y;0];
figure
plot(t,y)
xlim([-1 2])
val=10;
incr2 = max(t)-min(t);
t0 = (0:incr2:val-1);
T = t+t0;
figure
plot(T,y,'g-')
xlim([-1 11])

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


Bruno Luong
Bruno Luong 2023년 9월 6일
편집: Bruno Luong 2023년 9월 6일
f_on_1period = @(t) 2*sqrt((6*(t)));
period = pi;
f_periodic = @(t) f_on_1period(mod(t,period));
t=0:0.01:20;
y = f_periodic(t);
plot(t,y)

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by