0

我不明白出现以下错误的原因是什么?这里给出了代码

for i=1:20 
      x(i) = i-10; 
      squared(i) = x(i) ^ 2; 
      cube(i) = x(i) ^ 3; 
      linear(i) = x(i); 
      log_of(i) = log(x(i)); 
      sqrt_of(i) = sqrt(x(i)); 
    end 
    subplot(2,3,1); 
    plot(x,squared); 
    title('square'); 

        subplot(2,3,4); 
    plot(sqrt_of,cube); 
    title('sqrt'); 

    subplot(2,3,5); 
    plot(linear,cube); 
    title('linear'); 

    subplot(2,3,6); 
    plot(log_of,cube); 
    title('log'); 


    subplot(2,3,3); 
    plot(x,cube); 
    title('cube'); 

错误说

subplot1
Attempt to execute SCRIPT subplot as a function:
C:\Users\D.Datuashvili\Desktop\subplot.m

Error in subplot1 (line 9)
    subplot(2,3,1);

代码中似乎一切正常,但为什么会出现以下错误?你能帮我吗?编辑:

for i=1:20 
      x(i) = i-10; 
      squared(i) = x(i).^ 2; 
      cube(i) = x(i).^ 3; 
      linear(i) = x(i); 
      log_of(i) = log(x(i)); 
      sqrt_of(i) = sqrt(x(i)); 
    end 
    subplot(2,3,1); 
    plot(x,squared); 
    title('square'); 

        subplot(2,3,4); 
    plot(sqrt_of,cube); 
    title('sqrt'); 

    subplot(2,3,5); 
    plot(linear,cube); 
    title('linear'); 

    subplot(2,3,6); 
    plot(log_of,cube); 
    title('log'); 


    subplot(2,3,3); 
    plot(x,cube); 
    title('cube'); 

错误:

 subplot1
Error using plot
Vectors must be the same lengths.

Error in subplot1 (line 10)
    plot(x,squared);
4

2 回答 2

3

您的工作区中可能有一个名为 subplot 的文件

于 2013-04-10T12:27:57.720 回答
0

如上所述,第一个错误大部分来自您的文件 C:\Users\D.Datuashvili\Desktop\subplot.m,它掩盖了 Matlab 的默认设置。

第二个错误来自数组长度squared较长的事实,可能是因为您之前更改了它。

只需在脚本之前添加以下行就可以了

 clear squared cube linear log-of sqrt_of
于 2013-05-15T12:03:55.240 回答