how to solve a fourth degree polynomial for x / polyval for x

4 views (last 30 days)
I've designed an app that is able to extrapolate the y at any given x. To do this i've used polyfit to generate a fourth degree polynomial, based on testdata. It works perfectly to find y when x is given.
Now i would like to reverse it so that I can enter a y, and get the x-value for the same polynomial. Doesn't MatLab have a built-in function for this?
Below is shown the code I've used to generate the polynomial and find the y-value
y_sample=[18.5610 21.298 27.531 36.8 51.221]; % test data
x_sample = (0.25:0.25:1.25); % x-values for test data
p = polyfit(x_sample,y_sample,4); % fourth degree polynomial
y_value = polyval(p,X) % THIS IS THE LINE I WISH TO BE SOLVED FOR X
I know 4th degree isn't optimal for this example, but it is needed for some of the other uses of the function.

Accepted Answer

Ameer Hamza
Ameer Hamza on 25 Sep 2020
Edited: Ameer Hamza on 25 Sep 2020
What about reversing the interpolation
y_sample=[18.5610 21.298 27.531 36.8 51.221]; % test data
x_sample = (0.25:0.25:1.25); % x-values for test data
p = polyfit(y_sample,x_sample,4); % fourth degree polynomial
x_value = polyval(p,Y)
  4 Comments
Kenneth Carlson
Kenneth Carlson on 28 Sep 2020
Unfortunately it requires the optimization toolbox, which I don't have avalible. I will have to settle for the "not as precise" result, and see how it works out for now.
Thanks again though.
Steven Lord
Steven Lord on 28 Sep 2020
Since you're trying to find a zero of one equation in one unknown you don't actually need fsolve here. fzero is in MATLAB and can handle this problem.

Sign in to comment.

More Answers (1)

Mohlouwa Joseph BOTMAN
Mohlouwa Joseph BOTMAN on 28 Sep 2020
Consider the two polynomials 3 2 p(s) = s + 4s +5s + 2 and q(s) = s +C
1.1 Determine p(s)q(s) based on the simulation software.
(3)
1.2 Determine the poles and zeros of
( )
( )
( )
q s
G s
p s
= based on the simulation software
  1 Comment
Steven Lord
Steven Lord on 28 Sep 2020
Since this is not related to the original question, you should post this as a separate question. When you do, since this sounds like a homework assignment please show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!