Filter löschen
Filter löschen

Curve fit but only for the values that exists with defined values of x.

8 Ansichten (letzte 30 Tage)
I wnat to fit the curve for the point but i only want the curve to fit from value suppose in given code x vary from 1800-1950 and shouldn't extend beyond it. Is there any way to do this?
load census
plot(cdate,pop,'o')
%Create a fit options object and a fit type for the custom nonlinear model , where a and b are coefficients and n is a problem-dependent parameter.
fo = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[0,0],...
'Upper',[Inf,max(cdate)],...
'StartPoint',[1 1]);
ft = fittype('a*(x-b)^n','problem','n','options',fo);
%Fit the data using the fit options and a value of n = 2.
[curve2,gof2] = fit(cdate,pop,ft,'problem',2)
curve2 =
General model: curve2(x) = a*(x-b)^n Coefficients (with 95% confidence bounds): a = 0.006092 (0.005743, 0.006441) b = 1789 (1784, 1793) Problem parameters: n = 2
gof2 = struct with fields:
sse: 246.1543 rsquare: 0.9980 dfe: 19 adjrsquare: 0.9979 rmse: 3.5994
%Fit the data using the fit options and a value of n = 3.
[curve3,gof3] = fit(cdate,pop,ft,'problem',3)
curve3 =
General model: curve3(x) = a*(x-b)^n Coefficients (with 95% confidence bounds): a = 1.359e-05 (1.245e-05, 1.474e-05) b = 1725 (1718, 1731) Problem parameters: n = 3
gof3 = struct with fields:
sse: 232.0058 rsquare: 0.9981 dfe: 19 adjrsquare: 0.9980 rmse: 3.4944
%Plot the fit results with the data.
hold on
plot(curve2,'m')
plot(curve3,'c')
legend('Data','n=2','n=3')
hold off

Akzeptierte Antwort

Torsten
Torsten am 19 Okt. 2022
Bearbeitet: Torsten am 19 Okt. 2022
load census
I = cdate >= 1850 & cdate <= 1950;
cdate1 = cdate(I);
pop1 = pop(I);
plot(cdate,pop,'o')
%Create a fit options object and a fit type for the custom nonlinear model , where a and b are coefficients and n is a problem-dependent parameter.
fo = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[0,0],...
'Upper',[Inf,max(cdate)],...
'StartPoint',[1 1]);
ft = fittype('a*(x-b)^n','problem','n','options',fo);
%Fit the data using the fit options and a value of n = 2.
[curve2,gof2] = fit(cdate1,pop1,ft,'problem',2)
curve2 =
General model: curve2(x) = a*(x-b)^n Coefficients (with 95% confidence bounds): a = 0.005544 (0.004984, 0.006103) b = 1784 (1777, 1790) Problem parameters: n = 2
gof2 = struct with fields:
sse: 70.1612 rsquare: 0.9963 dfe: 9 adjrsquare: 0.9959 rmse: 2.7921
%Fit the data using the fit options and a value of n = 3.
[curve3,gof3] = fit(cdate1,pop1,ft,'problem',3)
curve3 =
General model: curve3(x) = a*(x-b)^n Coefficients (with 95% confidence bounds): a = 1.326e-05 (1.023e-05, 1.63e-05) b = 1723 (1708, 1738) Problem parameters: n = 3
gof3 = struct with fields:
sse: 151.9055 rsquare: 0.9919 dfe: 9 adjrsquare: 0.9910 rmse: 4.1083
%Plot the fit results with the data.
hold on
plot(cdate1,curve2(cdate1),'m')
plot(cdate1,curve3(cdate1),'c')
legend('Data','n=2','n=3')
hold off
  3 Kommentare
Torsten
Torsten am 19 Okt. 2022
I modified the code above accordingly.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by