using nonlinearmodek fit for the first time

2 vues (au cours des 30 derniers jours)
Michal Naim Ben Eliyahu
Michal Naim Ben Eliyahu le 24 Avr 2022
Commenté : Star Strider le 25 Avr 2022
Hi,
I wanted to fit a nonlinear model to my data but I keep getting an error.
This is the error:
>> untitled2
Unable to resolve the name NonLinearModel.fit.
Error in untitled2 (line 17)
myFit = NonLinearModel.fit(X,Y, 'y ~ b0 + b1*cos(b2*x1 + b3)*exp(-b4*x1)', [B0, B1, B2, B3,B4]);
This is my code:
X=table2array(X);
Y=table2array(Y);
% Note that we need to pass three sets of input arguments to NonLinearModel
% # The X and Y data
% # A string describing our model
% # Starting conditions for the optimization solvers
% Generate some good starting conditions for the solvers
scatter(X, Y)
hold on
B0 = mean(Y); % Vertical shift
B1 = (max(Y) - min(Y))/2; % Amplitude
B2 = 2; % Phase (Number of peaks)
B3 = 0; % Phase shift (eyeball the Curve)
B4 = 5;
myFit = NonLinearModel.fit(X,Y, 'y ~ b0 + b1*cos(b2*x1 + b3)*exp(-b4*x1)', [B0, B1, B2, B3,B4]);
% Note that all the coefficient estimates are very good except for b3 where
% any even integer is equally valid
%%look at the complete set of methods
methods(myFit)
%%Generate a plot
hold on
plot(X, myFit.Fitted)
hold off
Do I need to define the nonlinearmodel? or download another extent?
Thank you so much in advanced!

Réponses (1)

Star Strider
Star Strider le 24 Avr 2022
The NonLinearModel is a class.
To do the actual nonlinear regression, use the fitnlm function.
  4 commentaires
Michal Naim Ben Eliyahu
Michal Naim Ben Eliyahu le 25 Avr 2022
sorry for keep asking but this time I used fitnlm and I keep getting a straight line as my fit and changing my parameters doesn't affect anything.
this is the code this time:
X=data(100:250,1);
Y=data(100:250,2);
X=table2array(X);
Y=table2array(Y);
% Note that we need to pass three sets of input arguments to NonLinearModel
% # The X and Y data
% # A string describing our model
% # Starting conditions for the optimization solvers
% Generate some good starting conditions for the solvers
scatter(X, Y)
hold on
B0 = mean(Y); % Vertical shift
B1 = (max(Y) - min(Y))/2; % Amplitude
B2 = 2 ; % Phase (Number of peaks)
B3 = 0; % Phase shift (eyeball the Curve)
B4 = 5;
myFit = fitnlm(X,Y, 'y ~ b0 + b1*cos(b2*x1 + b3)*exp(-b4*x1)', [B0, B1, B2, B3,B4]);
yfit=predict(myFit,X);
% Note that all the coefficient estimates are very good except for b3 where
% any even integer is equally valid
%%look at the complete set of methods
%%methods(myFit)
%%Generate a plot
plot(X,Y,'xb')
hold on
plot(X,yfit)
hold off
and this is thr result:
Star Strider
Star Strider le 25 Avr 2022
Change ‘B4’ to something smaller (perhaps 2). It may also be necessary to subtract ‘X(1)’ from the rest of ‘X’ so that the record begins at 0. That should not affect the parameter estimates.
It may be necessary to experiment a bit, especially with ‘B4’.
It would help to have your data, preferably as a spreadsheet or text file since the online Run feature does not work well with .mat files.
That aside, my code works!
Running your data with the code from How to filter noise from time-frequency data and find natural frequency of a cantilever? could help with estimating the parameters. Then use those as the initial parameter estimates for fitnlm. It will improve on the initial estimates, and give all the necessary statistics.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Biological and Health Sciences 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!

Translated by