简体   繁体   中英

Error plotting 3D function

Im trying to plot a 3D function with Matlab, my script contains the following :

function f=Untitled(s1 ,s2 , s3)
    s =[s1 s2 s3];
    f= 0.0663 + 0.2099245 *(s(1)^2+s(2)^2+s(3)^2);
endfunction

xdata = linspace(36,36,36);
ydata = linspace(36,36,36);
zdata = linspace(36,36,36);
contour( xdata , ydata , zdata , [1 36 72 110])

I got the following error:

Error in ====> Untitled at 2
s=[s1 s2 s3]

I'm newest in Matlab, can any one help me to resolve this issue?

There are many issues in your code:

  • s = [s1 s2 s3];

    This concatenates s1 s2 and s3 horizontally to variable s

    To do that, "the number of rows in each variable should be same" . That is the cause of your error.

    You need to check the dimensions of the variable you passed to the function untitled

  • endfunction is Octave syntax. You just need to use end for Matlab

  • Then you are using s(1) , s(2) , s(3) separately inside your function.

    Then why did you cat them? you could have used the corresponding variables itself like this:

     f= 0.0663 + 0.2099245 *(s1^2+s2^2+s3^2); 
  • Then you are using linspace (I don't know what you actually intend to do with linspace ) for replicating values. Although this doesn't pose any issues, there is a separate built-in function for that called repmat

    You could use it like this: repmat(36,[1,36])

Note: Also name your function to any other name for good practice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM