0

i can't understand what is reason for following error?here is given code

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'); 

and error says

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);

it seems everything ok in code, but why is following error?could you help me? EDITED:

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'); 

error:

 subplot1
Error using plot
Vectors must be the same lengths.

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

2 Answers 2

3

Probably there is a file named subplot in your workspace

Sign up to request clarification or add additional context in comments.

6 Comments

what is C:\Users\D.Datuashvili\Desktop\subplot.m ? is it matlab's own function or your file?
i have changed directory now,but whenever i run it shows me only one figure
it says that vector should be same length,i have done some changes, and upload now
x and squared arrays have different lengths, you can try clearing those variables by adding clear all to the begining of the m file
now it works,thanks a lot,so it is better to use clear all always when writing M-file?
|
0

As noted above, the first error most comes from your file C:\Users\D.Datuashvili\Desktop\subplot.m which shadows the Matlab default.

the second error comes from the fact that the length of array squared is longer, probably because you changed it earlier.

just add the following line before your script and it will be fine

 clear squared cube linear log-of sqrt_of

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.