How to use the "subplot" and "arrayfun" command correctly?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hi, I'm wondering if someone can assist me with debugging this Error I received when I ran this .m file program. The code is below:
Ts = [0.05 0.01 0.001]; % Define sampling time, Ts = 0.05 seconds.
TT = 1; % Define total time, TT = 1 second.
f = 2; % Define frequency, f = 2 Hz.
t = 0:Ts:1; % Generate the time vector.
S = arrayfun(t, Ts); % Applies the function, t, to the elements
% of S, one element at a time.
% arrayfun concatenates the outputs from t into the output array S, so that
% for the ith element of Ts, S(i)=t(Ts(i)).
x = sin(2*pi*f*t); % Generate the desired 2 Hz sine wave.
plot (S,x,'.k') % Plot the sine wave as discrete points.
xlabel 'Time (s)' % and label the (x) and (y) axes.
ylabel 'x(t)'
I'm receiving 1 warning in Line 4 that states: "Variable 'Ts' might be set by a nonscalar operator." And 1 Error that states:
"Error using arrayfun"
First input must be a function handle.
Does anyone know how I can possibly fix this Error?
All I'm trying to do is create a subplot of 3 different plots of a discrete 2-Hz sine wave (one for each sampling time [0.05, 0.01, 0.001]) on 1 single plot. I need to create the subplot so I plot with lines connecting the points, and on the same plot, the individual points should be superimposed on the line curves. I need to plot the sine waves using the 3 sample intervals separately but I need to use 'subplot' to keep the 3 plots together in 1 Figure.
I hope that doesn't sound too confusing. If you need some clarification, please let me know. I'm not sure how to use the 'subplot' command correctly. Thanks!
2 comentarios
Rik
el 1 de Feb. de 2018
Have you read the documentation? That is always a good start. Here, the error is also illuminating: t must be a function handle (or an anonymous function), but it isn't. I can't cell what function you would want to use here, but this isn't a function.
Respuestas (1)
Rik
el 2 de Feb. de 2018
Do you mean something like this? (note that I changed the input to plot, as well as the creation of t)
Ts = [0.05 0.01 0.001]; % Define sampling time, Ts = 0.05 seconds.
TT = 1; % Define total time, TT = 1 second.
f = 2; % Define frequency, f = 2 Hz.
for Ts_i=1:numel(Ts)
t = 0:Ts(Ts_i):TT; % Generate the time vector.
x = sin(2*pi*f*t); % Generate the desired 2 Hz sine wave.
subplot(3,1,Ts_i)
plot (t,x,'.k') % Plot the sine wave as discrete points.
xlabel 'Time (s)' % and label the (x) and (y) axes.
ylabel 'x(t)'
end
0 comentarios
Ver también
Categorías
Más información sobre Analyze Data en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!