Using Polyfit to determine new X values (instead of Y)

18 Ansichten (letzte 30 Tage)
Alexander
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...

Akzeptierte Antwort

John D'Errico
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
Alexander
Alexander am 7 Mai 2018
Thank you very much, John. That was my suspicion (x(y) is no longer a function). Not sure why I had a hard time accepting that fact...
I went ahead and took your advice and took the polynomial coefficients that MATLAB computed, and I found the roots of the polynomial that fits my data. I then used the only real number of of three possible values for x (two had imaginary components, one did not), and plotted those numbers against my new y2 scale:
y3 = (-11.9:0.1:-11.0)'; % New y scale
for i = 1:length(y3)
f = [ p(1); p(2); p(3); p(4)-y3(i) ]; % Polynomial == 0 coifficients
r = roots(f); % Roots
x3(i) = r(3) ; % Only use the real number returned from r
end
Plotting the results on my existing plot reveal the value(s) I was originally looking for:
Basically, it's going to be a headache to compute the desired x values for each monotonic portion of the data (I many more sets of data, some of which cannot be fitted with just a 3rd degree polynomial...). However, this puts me on the right track in terms of another solution.
Thank you!
Steven Lord
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-')

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Polynomials finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by