Fitting multiple datasets simultaneously
12 views (last 30 days)
Show older comments
I have 4 different datasets which have to be fitted with the same (nonlinear) fitting function. There is only one fitting parameter which can be varied to fit all these datasets. The problem is that in my fitting function there is a variable 'beta', which is different for all these datasets!
I'm looking for a way to fit all my sets simultaneously with these different curves, rendering only one solution of the fitparameter.
I hope my question is clear, please let me know if it isn't.
3 Comments
Kristin Busa
on 3 Sep 2012
I am in a similar predicament and would love to hear how this turns out! I have 5 sets of data, each with ~8 variables, but 2 of the variables to be fit in each data set need to be global over all 5 data sets. Help?
Answers (1)
the cyclist
on 4 Sep 2012
You should be able to do this with nlinfit(). Here is a simple example using that function, that may help get you started:
% Here is an example of using nlinfit(). For simplicity, none of
% of the fitted parameters are actually nonlinear!
% Define the data to be fit
x=(0:1:10)'; % Explanatory variable
y = 5 + 3*x + 7*x.^2; % Response variable (if response were perfect)
y = y + 2*randn((size(x)));% Add some noise to response variable
% Define function that will be used to fit data
% (F is a vector of fitting parameters)
f = @(F,x) F(1) + F(2).*x + F(3).*x.^2;
F_fitted = nlinfit(x,y,f,[1 1 1]);
% Display fitted coefficients
disp(['F = ',num2str(F_fitted)])
% Plot the data and fit
figure
plot(x,y,'*',x,f(F_fitted,x),'g');
legend('data','fit')
You can tailor it to your example by adding a second column to x that takes on the different fixed values for your different data sets.
1 Comment
nony lew
on 16 Apr 2018
Hello,
I have used your answer to my data and went perfectly, thank you. x=[....]; y=[....]; f = @(F,x) F(1)*x +F(2)+F(3)*(sin(F(4)*x+F(5))); F_fitted = nlinfit(x,y,f,[1 1 1 1 1]); % Display fitted coefficients disp(['F = ',num2str(F_fitted)]) % Plot the data and fit figure plot(x,y,'*',x,f(F_fitted,x),'r'); legend('data','fit')
How can I modify this code for structures? If in may case
Thanks
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!