필터 지우기
필터 지우기

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

조회 수: 8 (최근 30일)
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

채택된 답변

Torsten
Torsten 2022년 10월 19일
편집: Torsten 2022년 10월 19일
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
Torsten
Torsten 2022년 10월 19일
I modified the code above accordingly.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by