how to repeat a function after fixed interval?

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.

Answers (2)

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

Thanks, Dyuman Joshi for answering. Since I am new to MATLAB, your answer is a learning for me.
I may be wrong in interpreting your answer.
As it appears, the second part code you wrote is just for plotting purposes.
However, the function y is just a part of a bigger code that will be used further in the calculation with the same time interval.
Please go through the picture posted for illustration. The right side of the plot is y plotted in one cycle. On the left side, the same function is plotted with time (showing more than one cycles).
"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])
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])

Sign in to comment.

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)

Products

Release

R2019b

Asked:

on 6 Sep 2023

Commented:

on 6 Sep 2023

Community Treasure Hunt

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

Start Hunting!