Using Polyfit to determine new X values (instead of Y)
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alexander
am 7 Mai 2018
Kommentiert: Steven Lord
am 7 Mai 2018
I have a sample of data that I would like to fit a curve to:
x1 = (164:1:182)';
y1 = [-11.95; -11.66; -11.29; -11.08; -11.00; -11.00; -10.99; -11.08; ...
-11.27; -11.29; -11.68; -11.61; -11.80; -11.77; -11.78; -11.69; ...
-11.48; -11.04; -10.81];
p = polyfit(x1, y1, 3); % Fit 3rd degree polynomial to orginal data
x2 = (164:0.5:182)'; % New X2 scale is twice as dense
y2 = polyval(p, x2); % Evaluate polynomial curve to new X2 scale
Returns the following plot, with a curve fitting the original data:

I understand that polyfit determines new y2 values to plot against the new x2 scale (notice that each point on the PolyFit curve is a y2 evaluated at an x2 value, so one y2 value every 0.5 units on the x2 scale)
What I would like to do is determine new x2 values based on the original data, and plot each new x2 against a new scale y2. In a way, it's doing the reverse of what the above code is doing. I would like my new y2 scale to be:
y2 = (-11.9:0.1:-10.9)';
x2 = ??
So I want to determine a new x2 value for every y2 value, not the other way around. However, when I alter the above code to try to accomplish this, I don't get what I want:
p = polyfit(y1, x1, 3); % Fit 3rd degree polynomial to orginal data
y2 = (-11.9:0.1:-10.9)'; % New Y2 scale
x2 = polyval(p, y2); % Evaluate curve to new Y2 scale

How can I accomplish this? I'm having trouble understanding what exactly is going on in my new plot...
0 Kommentare
Akzeptierte Antwort
John D'Errico
am 7 Mai 2018
You CANNOT simply swap x and y, and fit a new polynomial to the result, thus model the curve as x(y).
Think about what that would mean. When you swap x and y, you now have a relationship that has THREE values of x for most values of y. This is not a function. A polynomial is useless there. No polynomial exists that predicts three possible values at any given input. And worse, you want to extrapolate that polynomial that you cannot create in any sensible way?
Sorry, but no polynomial can do what you are asking to do. That is just basic mathematics.
What can you do? Consider the existing polynomial that you did manage to fit, thus as y(x). You can solve for 1 or 3 real values of x for any given y. (For some rare values of y, there will be 2 distinct real solutions.) What you want to do with those points, god only knows.
2 Kommentare
Steven Lord
am 7 Mai 2018
In the case of a polynomial, using roots will work. For more general fits, you can use fzero. For example, to find the x values that correspond to -11.5:
% originally posted code
x1 = (164:1:182)';
y1 = [-11.95; -11.66; -11.29; -11.08; -11.00; -11.00; -10.99; -11.08; ...
-11.27; -11.29; -11.68; -11.61; -11.80; -11.77; -11.78; -11.69; ...
-11.48; -11.04; -10.81];
p = polyfit(x1, y1, 3); % Fit 3rd degree polynomial to orginal data
x2 = (164:0.5:182)'; % New X2 scale is twice as dense
y2 = polyval(p, x2); % Evaluate polynomial curve to new X2 scale
% Plot the original data and the fitted curve
plot(x1, y1, 'bo-', x2, y2, 'ro-')
% Find the three places where the curve crosses the line y = -11.5
% Rewrite polyval(p, x) = -11.5 as
% polyval(p, x) - (-11.5) = 0 and find some solutions for x
desiredX(1) = fzero(@(x) polyval(p, x)-(-11.5), 170);
desiredX(2) = fzero(@(x) polyval(p, x)-(-11.5), 175);
desiredX(3) = fzero(@(x) polyval(p, x)-(-11.5), 180);
% Plot the intersections: X marks the spots
hold on
plot(desiredX, repmat(-11.5, size(desiredX)), 'gx-')
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Polynomials finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
