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

조회 수: 2 (최근 30일)
mohsen
mohsen 2016년 3월 5일
편집: Walter Roberson 2016년 3월 5일
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