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)
%Fit the data using the fit options and a value of n = 3.
[curve3,gof3] = fit(cdate,pop,ft,'problem',3)
%Plot the fit results with the data.
hold on
plot(curve2,'m')
plot(curve3,'c')
legend('Data','n=2','n=3')
hold off
0 件のコメント
採用された回答
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)
%Fit the data using the fit options and a value of n = 3.
[curve3,gof3] = fit(cdate1,pop1,ft,'problem',3)
%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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Get Started with Curve Fitting Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!