Error using subplot (line 332) Index exceeds number of subplots.

4 ビュー (過去 30 日間)
Dan Iel
Dan Iel 2020 年 1 月 25 日
コメント済み: Allen 2020 年 1 月 27 日
x=[-3:0.01:3];
y=1/4*ones(size(x));
for n=1:40
ao=(1/6);
an=((-1)^n)/(n^2*pi^2);
bn=-(-1)^n/(n*pi)-(2)/(n^3*pi^3)
subplot(3,2,1)
stem(n,ao); xlabel('n-axis');ylabel('a_n');hold on; drawnow;
subplot(3,2,2)
stem(n,an); xlabel('n-axis'); ylabel('a_n'); hold on; drawnow;
subplot(3,2,3)
stem(n,bn); xlabel('n-axis'); ylabel('b_n'); hold on;
subplot(3,2,4)
plot(x,an*cos(n*pi*x)); xlabel('x-axis'); ylabel('a_n cos(n*pi*x/L)'); hold on
subplot(3,2,5)
plot(x,bn*sin(n*pi*x)); xlabel('x-axis'); ylabel('b_n sin(n*pi*x/L)'); hold on
subplot(3,2,[6 7])
y=y+an*cos(n*pi*x)+bn*sin(n*pi*x);
plot(x,y); xlabel('x-axis'); ylabel('f(x)');
end
  1 件のコメント
dpb
dpb 2020 年 1 月 25 日
編集済み: dpb 2020 年 1 月 26 日
subplot(3,2,[6 7])
There can't be 7 subplots in a 3x2 array of six...why did you write the vector [6 7] instead of just the single index 6 as before?
The actual fatal error aside, what is the intention here? Each pass through the loop on n will overwrite the previous plot for each subplot so that when the loop finishes all you'll be left with is the figure for n=40.
If the idea is a figure per n value, then you need a figure statement at the beginning of the loop to create a new one each pass. If the idea instead is to put a family of curves for different n values on the same axis; then you need to add a hold on after each plot is created in each subplot() axis the first time so will not overwrite the previous. If this is the idea, would be better to save the axes handles and use them to plot into subsequent times.

サインインしてコメントする。

回答 (1 件)

Allen
Allen 2020 年 1 月 26 日
x=[-3:0.01:3];
y=1/4*ones(size(x));
for n=1:40
ao=(1/6);
an=((-1)^n)/(n^2*pi^2);
bn=-(-1)^n/(n*pi)-(2)/(n^3*pi^3)
subplot(3,2,1)
stem(n,ao); xlabel('n-axis');ylabel('a_o');hold on; % Runs much faster when removing drawnow from loop and changed ylabel to 'a_o' from 'a_n'.
subplot(3,2,2)
stem(n,an); xlabel('n-axis'); ylabel('a_n'); hold on; % Runs much faster when removing drawnow from loop
subplot(3,2,3)
stem(n,bn); xlabel('n-axis'); ylabel('b_n'); hold on;
subplot(3,2,4)
plot(x,an*cos(n*pi*x)); xlabel('x-axis'); ylabel('a_n cos(n*pi*x/L)'); hold on
subplot(3,2,5)
plot(x,bn*sin(n*pi*x)); xlabel('x-axis'); ylabel('b_n sin(n*pi*x/L)'); hold on
subplot(3,2,6) % Replaced [6 7] with 6. With subplot(3,2,index) there are only six axes max (3x2=6) for which you can only call a single index at any one time.
y=y+an*cos(n*pi*x)+bn*sin(n*pi*x);
plot(x,y); xlabel('x-axis'); ylabel('f(x)'); hold on; % Was missing hold on and only plotting the last set of values
end
  2 件のコメント
dpb
dpb 2020 年 1 月 26 日
The above fixes the fatal error but doesn't resolve the Q? of what is the actual desired result? There's really no point in the n loop above; just use n=40 because the first 39 are immediately overwritten/overplotted.
Allen
Allen 2020 年 1 月 27 日
Since Daniel is using the hold on command to set the NextPlot property of the axes to add new plots, the for loop is not being overwritten. However, the variables that he is using to assign the data to are being overwritten and certainly not the preferred approach.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by