Clear Filters
Clear Filters

How can I fit curve quadratically such that fit goes like in the picture?

5 views (last 30 days)
I want to fit the curve based on equation
f(x) = b1+b2*x+b3*(x^2)
I got the curve as shown in the fiqure below:
I mean, this is the perfect fit, but I want fit that goes like following figure:
I got this from eqn f(x) = 20+0.1*x+b3*(x^2), by pre-defining b1 and b2 parameters.
Is there a way of curve plotting so that I can get all b1,b2,b3 automatically, such that curve goes in beween from all values?

Accepted Answer

Image Analyst
Image Analyst on 5 Nov 2022
Try this:
T_b = [.143, .163, .168];
P_b = [87, 107, 123];
plot(T_b, P_b, 'b.', 'MarkerSize', 25);
coefficients = polyfit(T_b, P_b, 2)
x = linspace(min(T_b), max(T_b), 1000);
yFit = polyval(coefficients, x);
hold on;
plot(x, yFit, 'r-', 'LineWidth', 2)
grid on;
xlabel('T_b', 'Interpreter', 'none');
ylabel('P_b', 'Interpreter', 'none');

More Answers (1)

John D'Errico
John D'Errico on 5 Nov 2022
Edited: John D'Errico on 5 Nov 2022
It appears you have chosen to fit a quadratic polynomial to your data. And you say you want a quadratic polynomial, but you DREW a STRAIGHT line.
So which is it? You fit a quadratic. But then when you got a quadratic, you don't want it.
help polyfit
POLYFIT Fit polynomial to data. P = POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of degree N that fits the data Y best in a least-squares sense. P is a row vector of length N+1 containing the polynomial coefficients in descending powers, P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1). [P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a structure S for use with POLYVAL to obtain error estimates for predictions. S contains these fields: R - Triangular R factor (possibly permuted) from a QR decomposition of the Vandermonde matrix of X df - Degrees of freedom normr - Norm of the residuals If the data Y are random, an estimate of the covariance matrix of P is (Rinv*Rinv')*normr^2/df, where Rinv is the inverse of R. [P,S,MU] = POLYFIT(X,Y,N) finds the coefficients of a polynomial in XHAT = (X-MU(1))/MU(2) where MU(1) = MEAN(X) and MU(2) = STD(X). This centering and scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm. Warning messages result if N is >= length(X), if X has repeated, or nearly repeated, points, or if X might need centering and scaling. Example: simple linear regression with polyfit % Fit a polynomial p of degree 1 to the (x,y) data: x = 1:50; y = -0.3*x + 2*randn(1,50); p = polyfit(x,y,1); % Evaluate the fitted polynomial p and plot: f = polyval(p,x); plot(x,y,'o',x,f,'-') legend('data','linear fit') Class support for inputs X,Y: float: double, single See also POLY, POLYVAL, ROOTS, LSCOV. Documentation for polyfit doc polyfit Other uses of polyfit codistributed/polyfit gpuArray/polyfit tall/polyfit
Given 3 general data points, there exists a unique quadratic polynomial that passes through the points. That it may not have the shape you wanted is the fault of your data. Only 3 points is not enough to define a curve terribly well. It is sufficient for a quadratic fit, but you are appranelty not happy with it. The faulty is therefore not in the fitting tool, but in your data, or in what you asked of the fitting tool.
So you could have done
p1 = polyfit(x,y,1);
Or, use the curve fitting toollbox, using the 'poly1' fitting method.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!