ERROR: Z must be a matrix, not a scalar or vector.
    조회 수: 5 (최근 30일)
  
       이전 댓글 표시
    
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.
댓글 수: 0
채택된 답변
  Star Strider
      
      
 2017년 5월 13일
        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
  Star Strider
      
      
 2017년 5월 13일
				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.
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

