Curve fit but only for the values that exists with defined values of x.
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Yashvardhan Bhati
le 19 Oct 2022
Commenté : Yashvardhan Bhati
le 20 Oct 2022
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 commentaires
Réponse acceptée
Torsten
le 19 Oct 2022
Modifié(e) : Torsten
le 19 Oct 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)
%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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with Curve Fitting Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!