How can I fit a smooth curve to this data and get the equation of the curve?

7 views (last 30 days)
I tried lsline but this is for best fit, and doesn't really match this data
How can I fit a smooth curve to this data and get the equation of the curve?
%% current vs power
current = [26, 30, 40, 50, 60, 70, 80];
power = [0.00460174, 0.0475519, 0.214428, 0.441353, 0.712228, 0.91066, 1.05903];
figure;
plot(current,power,'o');

Accepted Answer

John D'Errico
John D'Errico on 4 Apr 2022
It is a bad idea to name your variables with the same name as an existing and useful MATLAAB function. Power is one such example.
current = [26, 30, 40, 50, 60, 70, 80];
powr = [0.00460174, 0.0475519, 0.214428, 0.441353, 0.712228, 0.91066, 1.05903];
plot(current,powr,'o')
The problwm is, there are infinitely many functions that will fit that data. Any set of data in fact, can be fit by infintely many possible models. However, here a polynomial is probably adequate. The inflection point in the curve suggests a cubic polynomial would be necessary, so I might try it:
p3 = fit(current',powr','poly3')
p3 =
Linear model Poly3: p3(x) = p1*x^3 + p2*x^2 + p3*x + p4 Coefficients (with 95% confidence bounds): p1 = -8.102e-06 (-1.117e-05, -5.032e-06) p2 = 0.001298 (0.0008073, 0.001788) p3 = -0.04391 (-0.06845, -0.01938) p4 = 0.413 (0.03479, 0.7912)
hold on
plot(p3)
legend('Current vs power data','Cubic fit','location','northwest')
a higher order polynomial would not be justified, but the cubic seems adequate here.
  5 Comments
Voss
Voss on 5 Apr 2022
p3 is the coefficients of the cubic polynomial (see the comment about coefficients in the code I posted), in order of decreasing powers of x, so the equation would be:
p3(1)*x^3 + p3(2)*x^2 + p3(3)*x + p3(4)
polyval evaluates the polynomial at any x you want. I used polyval to get the values to plot in red.
Alex Sha
Alex Sha on 5 Apr 2022
If prefer to another fitting formula rather than the polynomial, one of will be:
y = p1-p2*Exp(-p3*x^p4);
Sum Squared Error (SSE): 0.000194020813066487
Root of Mean Square Error (RMSE): 0.00526471832195211
Correlation Coef. (R): 0.999908440561234
R-Square: 0.999816889505599
Parameter Best Estimate
---------- -------------
p1 1.14840232711552
p2 1.22125975267146
p3 1.50816314630436E-6
p4 3.27583408702712

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!