Borrar filtros
Borrar filtros

ERROR: Z must be a matrix, not a scalar or vector.

2 visualizaciones (últimos 30 días)
geometry geometry
geometry geometry el 13 de Mayo de 2017
Comentada: Star Strider el 13 de Mayo de 2017
What's the problem with the following code?
syms x y;
f=input('enter function: ','s');
f = symfun(eval(f), [x y]);
[X,Y]=meshgrid(-10:1:10);
mesh(f);
when i run the above code after entering a function the following error occurs:
Z must be a matrix, not a scalar or vector.

Respuesta aceptada

Star Strider
Star Strider el 13 de Mayo de 2017
There is no reason to use the Symbolic Math Toolbox here. Core MATLAB fiunctions will work.
The Code
f=input('enter function: ','s')
% f = 'x.^2 + sin(y)'; % Test Function
f = str2func(['@(x,y)' f]);
[X,Y]=meshgrid(-10:1:10);
mesh(X,Y,f(X,Y));
  2 comentarios
geometry geometry
geometry geometry el 13 de Mayo de 2017
Editada: geometry geometry el 13 de Mayo de 2017
Thank you...
but one question:
when I enter the function log(x^2+y^2) an empty figure appears; what's the problem with this?
and when I enter log(x) the following error occurs:
X, Y, Z, and C cannot be complex.
Star Strider
Star Strider el 13 de Mayo de 2017
My pleasure.
You need to be certain that ‘f’ does element-wise operations. Change the str2func call to include a vectorize call:
f = str2func(['@(x,y)' vectorize(f)]);
See the documentation on the various functions to understand how they work, and the documentation on Array vs. Matrix Operations (link) to understand the reason the differences are important.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by