Quadratic/Exponential Best Fit Curve

조회 수: 72 (최근 30일)
Hamish Brown
Hamish Brown 2021년 11월 16일
답변: John D'Errico 2021년 11월 16일
i'd like to add a quadratic or exponential best fit curve to my scatter graph. I cant figure out how to do it. I've tried the following but it doesn't give the type of curve i'd like. I also have the curve-fitting package installed.
y = [0.1 0.2 0.3 1.3];
x = [180 598 2259 8853];
plot(x,y,'x')
p = polyfit(x,y,7)
f = polyval(p,x)
hold on
plot(x,f,'--r')
hold off

채택된 답변

John D'Errico
John D'Errico 2021년 11월 16일
First, you have 4 data points. Trying to fit a degree 7 (SEVEN) polynomial to that is just silly. Yes, you say quadratic. But your words don't fit what you did.
Putting multiple fits on one plot is easy though.
y = [0.1 0.2 0.3 1.3];
x = [180 598 2259 8853];
plot(x,y,'x')
Lets be completely serious. You want to use those 4 data points to try to fit anything? These 4 points do not have the shape of a quadratic polynomial. They lack the shape of an exponential function. And that means what you get out will be complete garbage for a model.
You could arguably get just as valid a fit from a straight line through the data.
mdlpoly1 = fit(x',y','poly1');
mdlpoly2 = fit(x',y','poly2');
You don't say WHICH exponential model you want to use. And with only 4 data points, hoping to do any such fit is problematic for an exponential model.
expft = fittype('a*exp(b*x)','indep','x');
mdlexp = fit(x',y',expft,'start',[1 0.0001])
mdlexp =
General model: mdlexp(x) = a*exp(b*x) Coefficients (with 95% confidence bounds): a = 0.1555 (0.04168, 0.2693) b = 0.00024 (0.0001543, 0.0003257)
Anyway, now we can plot the results.
plot(mdlpoly1,'m')
hold on
plot(mdlpoly2,'b')
plot(mdlexp,'g')
plot(x,y,'ro')
I have little confidence about which of those curves fits the data better. They are jerked around by the influence of the first and third data points, but which of those two points is noisy? Or is it the second point that is garbage?

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by