1

I want to plot a surface in MATLAB using surf. I have this equation: x = y^2 +4z^2.

What I am doing is the following:

[x,y] = meshgrid(-4:.1:4, -4:.1:4);

z = sqrt((x - y.^2)./4);              % Basically I'm just clearing for z

surf(x,y,z)

But with this I am getting the error: Error using surf X,Y,Z and C cannot be complex. I know there is a complex number because of the values that x and y have, plus the square root. Is there another way to plot a surface in MATLAB? because I really don't know what to do, and my skills are very basics.

HansHirse
  • 18,010
  • 10
  • 38
  • 67
Mike
  • 13
  • 3
  • 5
    If you're just interested in the real part of `z`, you can write `z = real(sqrt(...))`. The following `surf` command then will execute without problems. – HansHirse Mar 02 '20 at 05:40
  • Very closely related: [How can I plot a function with two variables in Octave or Matlab?](https://stackoverflow.com/q/16868074/8239061) – SecretAgentMan Mar 02 '20 at 13:38

1 Answers1

0

Why do you feel that you need to grid x and y, and not use the form of the original equation itself?

This seems to work perfectly fine

[y,z] = meshgrid(-4:.1:4, -4:.1:4);
x = y.^2 + 4*z.^2;
surf(x,y,z)

to produce

enter image description here

Kavka
  • 4,191
  • 16
  • 33