How to evaluate a polynomial p at each point in y?
10 views (last 30 days)
More Answers (2)
Rik on 4 Nov 2020
Edited: Rik on 4 Nov 2020
As far as I'm aware, this is not directly possible. If you rewrite the p-factors you can still use polyval.
x=(y-p(2))/p(1)=1/p(1)*y + -p(2)/p(1)
%so for numel(p)==2:
I haven't bothered to write a general inverter for p, but I suspect that isn't very hard.
Edit: I realised this is quite tricky for any order beyond 1, luckily you can empirically estimate the parameters:
%this requires a reasonable range of x:
Steven Lord on 4 Nov 2020
For the simple case of a polynomial, you can use roots. For example if you want to find a value of x for which is equal to 50:
pOrig = [1 3 3 1];
p = pOrig; % Make a copy so we can use the original for checking later on
p(end) = p(end)-50;
r = roots(p);
check = polyval(pOrig, r) % Each element of check should be very close to 50
To check this graphically:
f = @(x) polyval(pOrig, x);
fplot(f); % Plot the polynomial
yline(50); % Draw the line y = 50
realroot = r(imag(r) == 0); % Find the real root for plotting
plot(realroot, f(realroot), 'o') % Indicate where the polynomial crosses the horizontal line
For a more general function you can use fzero as Ameer Hamza suggested.