Fitting multiple datasets simultaneously

12 views (last 30 days)
Jan
Jan on 30 Aug 2012
Commented: nony lew on 16 Apr 2018
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
Jan
Jan on 31 Aug 2012
Edited: Jan on 31 Aug 2012
All my datasets are of exactly equal size, so weighing can be equal. Attached you find three .mat files, these files contain arrays B_diff and theta. I'm trying to fit this data with the function 'theta=asin(2*torque./B)+beta' where beta is in rad, in the filename you find the beta variable in degrees.
If I fit the data separately I use a for-loop which contains
beta=(angle/360)*2*pi;
fit_function = @(torque, B) asin(2*torque./B)+beta;
fit_result=lsqcurvefit(fit_function,4,B_diff,theta)
I'm not sure where I can attach files, so I uploaded them to my dropbox account. https://dl.dropbox.com/u/6885232/beta%3D2.mat
Kristin Busa
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?

Sign in to comment.

Answers (1)

the cyclist
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
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
x=xdata(k).part is a struct y=ydata(k).part is a struct
Thanks

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!