using nonlinearmodek fit for the first time
조회 수: 2 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
답변 (1개)
Star Strider
2022년 4월 24일
댓글 수: 4
Star Strider
2022년 4월 25일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Biological and Health Sciences에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!