Filter löschen
Filter löschen

Error Z must be a matrix, not a scalar or vector.

1 Ansicht (letzte 30 Tage)
mohsen
mohsen am 5 Mär. 2016
Bearbeitet: Walter Roberson am 5 Mär. 2016
prompt = 'what is the value of x';
prompt2 = 'what is value of y';
x = input(prompt);
y = input(prompt2);
u = 10*sin(200*x)+0.1*cos(10*y)+(100*x)/y;
[X,Y] = meshgrid(x,y);
Z = u
figure
mesh(Z)
Also, how would I use the element wise operator in this code?

Akzeptierte Antwort

Star Strider
Star Strider am 5 Mär. 2016
I prefer inputdlg to input, but that’s a personal preference and not a criticism of your code in that regard.
As best I can determine, you have your statements in the wrong order. Also, you need to define ranges for ‘x’ and y, not simply scalar values.
See if something like:
x = 1:10;
y = 20:30;
u = @(x,y) 10*sin(200*x)+0.1*cos(10*y)+(100*x)./y; % Create Anonymous Function From ‘u’
[X,Y] = meshgrid(x,y);
Z = u(X,Y);
figure(1)
mesh(X, Y, Z)
That works for me when I run it. The only changes I made in your code otherwise were to vectorise the division in ‘u’, to create a call to ‘u’ in your ‘Z’ assignment, and to add ‘X’ and ‘Y’ to your mesh call, because those tend to produce better (and more understandable) plots.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by