How to evaluate a polynomial p at each point in y?

조회 수: 16 (최근 30일)
Angelavtc
Angelavtc 2020년 11월 4일
댓글: Ameer Hamza 2020년 11월 6일
How to evaluate a polynomial p at each point in y? I know polyval(p,x) makes the same but for each point in x, but I would like to know if there exist something similar for y.
Thanks!

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 11월 4일
편집: Ameer Hamza 2020년 11월 4일
You can use fzero(). For example
p = [1 0 0]; % polynomial y = x.^2
y = 4; % value of y
x_sol = fzero(@(x) polyval(p, x)-y, rand()); % corresponding value of x
Result
>> x_sol
x_sol =
2
  댓글 수: 2
Angelavtc
Angelavtc 2020년 11월 5일
편집: Angelavtc 2020년 11월 5일
@Ameer Hamza thanks for your answer, it was really useful. But what if I want to do the same for a set of values x,y like the ones I am uploading here.
Lets say for the specific value y=0
I have seen that fzero only works for functions. What to do then?
Thanks in advance!
Ameer Hamza
Ameer Hamza 2020년 11월 6일
There can be several ways, but I would use interpolation to convert the series of 'x' and 'y' points into a continuous function and then apply fzero()
fun = @(xq) interp1(x, y, xq);
x_sol = fzero(@(xq) fun(xq)-0, mean(x)); % mean(x) so that initial point is within 'x' vector

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

추가 답변 (2개)

Rik
Rik 2020년 11월 4일
편집: Rik 2020년 11월 4일
As far as I'm aware, this is not directly possible. If you rewrite the p-factors you can still use polyval.
y=p(1)*x+p(2)
x=(y-p(2))/p(1)=1/p(1)*y + -p(2)/p(1)
%so for numel(p)==2:
p_=[1 -p(2)]/p(1);
x=polyval(p_,y)
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:
p_=polyfit(polyval(p,x),x,numel(p)-1);

Steven Lord
Steven Lord 2020년 11월 4일
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
hold on
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.

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by