How to plot function from m file with vectors
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Antworten (2)
Star Strider
am 21 Mai 2018
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 Kommentare
Star Strider
am 21 Mai 2018
My pleasure.
If my Answer helped you solve your problem, please Accept it!
phoenix
am 23 Mai 2018
Hello Star Strider, Your comments are not showing.Please tell why 2^18 is used in freqz(nz,dz,2^18)?
Abraham Boayue
am 21 Mai 2018
Bearbeitet: Abraham Boayue
am 21 Mai 2018
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)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!