Hi All!
I have 3 functions and I need to plot them individually on a plot window. However, I kept getting the error 'Z must be a matrix, not a scalar or vector.' Even though I have change my function into 2d (dependent on x and y only). It still doesnt work.
f1(x,y,z) = 8*x.^3+36*y.^3+z.^2-36; f2(x,y,z) = 4x.^2-y.^2-21*z; f3(x,y,z) = 4x.^2-2y.^2+10z.^2;
xxx = linspace(-3,3); yyy = linspace (-3,3);
a = sym(f1); b= sym(f2); c= sym(f3); s = subs(a,'z',1); t = subs(b,'z',1); u = subs(c,'z',1);
[x,y] = meshgrid(xxx,yyy); mesh(x,y,s); hold on mesh(x,y,t); hold on mesh(x,y,u);
Thanks in advance!

2 comentarios

John Chilleri
John Chilleri el 20 de En. de 2017
Editada: John Chilleri el 20 de En. de 2017
To reproduce the error, run:
f1 = @(x,y,z) 8*x.^3+36*y.^3+z.^2-36;
f2 = @(x,y,z) 4*x.^2-y.^2-21*z;
f3 = @(x,y,z) 4*x.^2-2*y.^2+10*z.^2;
xxx = linspace(-3,3);
yyy = linspace (-3,3);
a = sym(f1);
b= sym(f2);
c= sym(f3);
s = subs(a,'z',1);
t = subs(b,'z',1);
u = subs(c,'z',1);
[x,y] = meshgrid(xxx,yyy);
mesh(x,y,s);
hold on mesh(x,y,t);
hold on mesh(x,y,u);
When you give mesh(x,y,s), s is just a symbolic function. I'm guessing you need to say:
[x,y] = meshgrid(xxx,yyy);
s = subs(s,'y',y);
s = subs(s,'x',x);
mesh(x,y,s)
but it's taking a while to run so I can't confirm if this is correct. It feels like this produces an s that is far too large.
www
www el 20 de En. de 2017
I'm still getting the same error :/

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 20 de En. de 2017

0 votos

You were missing some multiplication operators in your functions. (MATLAB does not recognise implicit multiplication.) I also created your functions as anonymous functions (see Function Basics (link) for details), and then did the plots. The Symbolic Math Toolbox is not always the best option unless you want to specifically do symbolic calculations. Here, it is not necessary.
The Code:
f1 = @(x,y,z) 8*x.^3+36*y.^3+z.^2-36;
f2 = @(x,y,z) 4*x.^2-y.^2-21*z;
f3 = @(x,y,z) 4*x.^2-2*y.^2+10*z.^2;
xxx = linspace(-3,3);
yyy = linspace (-3,3);
[x,y] = meshgrid(xxx,yyy);
z = 1;
figure(1)
mesh(x,y,f1(x,y,z))
hold on
mesh(x,y,f2(x,y,z))
mesh(x,y,f3(x,y,z))
hold off
grid on

4 comentarios

www
www el 20 de En. de 2017
It's Mr Polar Bear again! This method works but is there any good reason why symbolic math isn't functioning as it should for my problem that I have?
Star Strider
Star Strider el 20 de En. de 2017
Yup!
You probably need to use the ezmesh functions with the symbolic functions. The problem is that you have three arguments to your functions, and that is going to create problems. You would specifically have to re-define them as functions of two variables for that, setting ‘z’ to 1. As I mentioned, the Symbolic Math Toolbox makes certain things more difficult, not easier.
If you want to do it all symbolically, this works:
syms x y z
f1(x,y,z) = 8*x.^3+36*y.^3+z.^2-36;
f2(x,y,z) = 4*x.^2-y.^2-21*z;
f3(x,y,z) = 4*x.^2-2*y.^2+10*z.^2;
z = 1;
a(x,y) = f1(x,y,z);
b(x,y) = f2(x,y,z);
c(x,y) = f3(x,y,z);
figure(1)
fmesh(a, [-3 3 -3 3])
hold on
fmesh(b, [-3 3 -3 3])
fmesh(c, [-3 3 -3 3])
hold off
You may have to substitute ezmesh for fmesh, but the result should be the same.
www
www el 20 de En. de 2017
Editada: www el 20 de En. de 2017
You are truly a saver! I'll never forget to donate to polar bear conservation project. :)
Star Strider
Star Strider el 20 de En. de 2017
Thank you!
With accelerating global warming, we need all the help we can get!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

www
el 20 de En. de 2017

Comentada:

el 20 de En. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by