Reputation: 101
trying to understand how matlab plot functions (fplot/ezplot) works.
dont understand why different outputs.
in the original example I see a dashed line on the x=2 (none here) where there is no value.
any help on printing the "right" version via fplot?
I noticed in the matlab help they point you not to use ezplot but fplot
none of the references helped a lot :(
syms x
fx = (x+2)/(x^2 - 4);
ht = matlabFunction(fx);
figure(1), clf
fplot(ht,[0 10]) % wrong plot
title('using fplot');
%ezplot(ht,[0 10]) % prints ok
%title('using ezplot');
references: https://www.mathworks.com/matlabcentral/answers/32820-trouble-substituting-a-value-into-a-symbolic-expression-for-use-with-fplot heaviside function procudes different outputs in ezplot and fplot
Upvotes: 0
Views: 231
Reputation: 60494
I'm using MATLAB R2017a.
When I run this code:
f = @(x) (x+2)./(x.^2 - 4);
fplot(f,[0 10])
I see the following plot:
I'm not using symbolic math above. I directly define an anonymous function that does numeric computation. I'm using ./
and .^
, the element-wise operators, because fplot
evaluates the function at many locations at the same time, by passing in x
as an array, as described in the documentation:
The function must accept a vector input argument and return a vector output argument of the same size.
Note that OP's code with matlabFunction
should produce an equivalent function, I don't know what might be wrong there. But in general, avoiding symbolic math when it is not necessary is always beneficial. (By "necessary" I mean if, for example, you are trying to find an analytical solution to an equation.)
PS: In MATLAB R2021a I ran this code and OP's code, and obtained the exact same result. The ht
from OP's code, and the f
from my code are identical. It is possible that there was a bug in a specific version of MATLAB that has been fixed in a later version.
Upvotes: 2