我正在尝试使用子图,但收到一条错误消息,我不知道如何纠正。
(此外,由于仅w的值发生变化,有没有比连续十次说“ cos(stuff * t”)更紧凑的方式来编写此代码了?)
我当前的代码是
clear all
clc
t = 0:0.001:40;
% there are 10 values of w to show, so make a 2x5 grid to show them all via
% subplot (2,5, blank)
x1=cos(pi/16*t);
x2=cos(pi/8*t);
x3=cos(pi/4*t);
x4=cos(pi/2*t);
x5=cos(pi*t);
x6=cos(15*pi/8*t);
x7=cos(2*pi*t);
x8=cos(5*pi/2*t);
x9=cos(3*pi*t);
x10=cos(4*pi*t);
subplot(2,5,x1), plot(t,x,1), subplot(2,5,x2), plot(t,x,2), subplot(2,5,x3),
plot(t,x,3), subplot(2,5,x4), plot(t,x,4), subplot(2,5,x5), plot(t,x,5),
subplot(2,5,x6), plot(t,x,6), subplot(2,5,x7), plot(t,x,7), subplot(2,5,x8),
plot(t,x,8), subplot(2,5,x9), plot(t,x,9), subplot(2,5,x10), plot(t,x,10);
%Error using subplot (line 330);
%Illegal plot number;
%Error in (line 19);
%subplot(2,5,x1), plot(t,x,1);
答案 0 :(得分:2)
您刚刚混合了services:
my-service:
image: ${PYTHON_IMAGE}:${PY_VERSION}
和plot
的参数。
阅读subplot文档。
这是您的代码的更正版本:
subplot
以下是使用for循环的方法:
clear all
clc
t = 0:0.001:40;
% there are 10 values of w to show, so make a 2x5 grid to show them all via
% subplot (2,5, blank)
x1=cos(pi/16*t);
x2=cos(pi/8*t);
x3=cos(pi/4*t);
x4=cos(pi/2*t);
x5=cos(pi*t);
x6=cos(15*pi/8*t);
x7=cos(2*pi*t);
x8=cos(5*pi/2*t);
x9=cos(3*pi*t);
x10=cos(4*pi*t);
subplot(2,5,1);
plot(t,x1);
subplot(2,5,2);
plot(t,x2);
subplot(2,5,3);
plot(t,x3);
subplot(2,5,4);
plot(t,x4);
subplot(2,5,5);
plot(t,x5);
subplot(2,5,6);
plot(t,x6);
subplot(2,5,7);
plot(t,x7);
subplot(2,5,8);
plot(t,x8);
subplot(2,5,9);
plot(t,x9);
subplot(2,5,10);
plot(t,x10);
您甚至可以在没有第一个clear all
clc
t = 0:0.001:40;
% there are 10 values of w to show, so make a 2x5 grid to show them all via
% subplot (2,5, blank)
W = [1/16, 1/8, 1/4, 1/2, 1, 15/8, 2, 5/2, 3, 4]*pi;
%X Replaces x1 to x10
X = zeros(10, length(t));
%Fill X with 10 rows (X(1, :) matches x1, X(2, :) matches x2...)
for i = 1:10
X(i, :) = cos(W(i)*t);
end
%Plot using a for loop
for i = 1:10
subplot(2,5,i);
plot(t, X(i, :));
end
循环的情况下进行操作:
for