How to plot function from m file with vectors

조회 수: 4 (최근 30일)
Michael Urban
Michael Urban 2018년 5월 21일
댓글: phoenix 2018년 5월 23일
Hello I have this function in my m file
function F = A(x)
F = 2 * x(1)^2 - x(2)^2;
end
and I want to plot this function but I have no idea how to do this. I tried fplot(@(x)A(x)) but I got this error
Warning: Function behaves unexpectedly on array inputs. To improve performance,
properly vectorize your function to return an output with the same size and shape as
the input arguments.
> In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function.FunctionLine/updateFunction
In matlab.graphics.function.FunctionLine/set.Function_I
In matlab.graphics.function.FunctionLine/set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 234)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 193)
In fplot>vectorizeFplot (line 193)
In fplot (line 163)
Warning: Error updating FunctionLine.
The following error was reported evaluating the function in FunctionLine update:
Index exceeds array bounds.

답변 (2개)

Star Strider
Star Strider 2018년 5월 21일
First, vectorizing will help:
F = 2 * x(1).^2 - x(2).^2;
Second, you are confusing fplot, since it does not know what to do with a bivariate function. A solution is to use fsurf (or fmesh), and create ‘A’ as a function of two variables (which it actually is):
figure(1)
fsurf( @(x1,x2) A([x1,x2]), [-1 1 -1 1] )
The plot then works. The warning continues to appear for some reason. You can safely ignore it.
  댓글 수: 3
Star Strider
Star Strider 2018년 5월 21일
My pleasure.
If my Answer helped you solve your problem, please Accept it!
phoenix
phoenix 2018년 5월 23일
Hello Star Strider, Your comments are not showing.Please tell why 2^18 is used in freqz(nz,dz,2^18)?

댓글을 달려면 로그인하십시오.


Abraham Boayue
Abraham Boayue 2018년 5월 21일
편집: Abraham Boayue 2018년 5월 21일
If x is a vector (1×N), then F must be a function of x, write F = x.^2 in this case. Else if x is a matrix (2×N) containing two vectors, x1 and x2, then F must be a function of two variables. You should write F = 2x1-x2;
Plotting F in case 1
N = 200;
x = -2:4/(N-1):2;
F = A(x);
Plot (x,F,'linewidth ',2);
Plotting F for case 2
First modify your function as
function F = A (x1, x2)
F = 2*x1.^2-x2.^2;
end
N = 200;
x1 = 0:2/(N-1):2;
x2 = -2:4/(N-1):2;
[x1,x2]= meshgrid(x1,x2);
F = A (x1, x2);
mesh (x1, x2, F)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by