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

1 次查看(过去 30 天)
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?

采纳的回答

Star Strider
Star Strider 2016-3-5
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by