how to repeat a function after fixed interval?
7 views (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
Dyuman Joshi
on 6 Sep 2023
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 Comments
Dyuman Joshi
on 6 Sep 2023
"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
on 6 Sep 2023
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
on 6 Sep 2023
Edited: Bruno Luong
on 6 Sep 2023
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)
See Also
Categories
Find more on Logical 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!





