1

I am looking for a method to fit two different datasets with different fit models but depending on the same parameters in Matlab. All solutions i found so far are either not for matlab or do not treat this exact problem.

Here is a rough example what my problem looks like: I have acquired two datasets that should take these forms:

Dataset 1: f(x) = a*exp(x)+b

Dataset 2: g(x) = atan(b*x/a)

The real formulas are more complicated but the principle should be the same.

When i try to fit them with a NonlinearLeastSquares independently, matlab obviously provides different values for the variables a and b for the two different datasets. I tried feeding one solution into the other fitting routine as a starting point but that rarely improves accuracy. Is there a way to tell Matlab to fit both datasets at the same time or at least find the variables that fit bost models the best?

Linus-
  • 119
  • 2
  • 10
  • You want to have _only one_ set of (a,b) for both equations. Am I right? So you must be aware that this might bring models with more errors regarding your datasets. – Mohammad Feb 21 '14 at 10:20
  • Yes, the parameter set (a,b) should be the same for both equations, you are right. I realise that might lead to larger errors but since they are the same physical property they have to be identical (both datasets are acquired through one measurement of different quantities). If I get two different values I would not know which is “correct”. Additionaly I hope this would rule out errors where one parameter gets messed up by one of the fits but is fine in the other (e.g. set to the boundary value). – Linus- Feb 21 '14 at 10:38

2 Answers2

2

In general you can use fmincon to do that. The idea is to define a function that takes both f(x) and g(x) in to account.

Lets do that.

function error2 = myFunction(betas,x)

lambda=0.5;

error2=0;

a=betas(1,1);
b=betas(2,1);

x1=x(:,1); %Assuming that both datasets have the same size. If they are not you can adjust it
y1=x(:,2);
x2=x(:,3);
y2=x(:,4);

n=size(x,1); 

for i=1:n
    f1=a*exp(x1(i,1))+b;
    f2=atan(b*x2(i,1)/a);
    error2=error2+lambda*(y1(i,1)-f1)^2 + (1-lambda)*(y2(i,1)-f2)^2;
end

Note that in "betas" I am keeping the parameters and "x" I am keeping the data. I had to introduce a new variable "lambda" in order to weight both functions f and g. This is good because varying lambda you are able to see how one of the functions affected the estimations of the other. You can actually start with lambda=0 and run several times this routine for values such as 0.1, 0.2,...,1.

Now you have to call this function using fmincon.

clear all
close all

    % Here you have to create your data x: Remember the structure I used for x=[x1,y1,x2,y2]

   x1=
   y1=
   x2=
   y2=

   x=[x1,y1,x2,y2];


    % you need to initiate the values of your parameters beta

a0=
b0=

beta0(1,1)=a0;
beta0(2,1)=b0;

beta = fmincon(@(beta)myFunction(beta,x), beta0);

This must work!

DanielTheRocketMan
  • 3,199
  • 5
  • 36
  • 65
  • Thank you for your idea. However, `fmincon()` requires at least four arguments, not only the function and the starting value but also parameters for a matrix (in)equation. I guess i can use that to set boundaries for my coefficients `a` and `b`? I will try to make it work. Nethertheless, any further help is highly appreciated. – Linus- Feb 24 '14 at 09:09
  • It works like a charm, thank you very much. The Matrix inequation could indeed be used for boundaries (although it can specify a lot more like relations between `a` and `b`for example). Just some further general cosmetic issue, can i somehow suppress the text output generated by `fmincon()`? I tried to use `'options'` with `'Display','off'` but the function seems to mistake the options for a different (optional) argument. – Linus- Feb 24 '14 at 10:27
  • @Linus, you are very welcome. I guess you have tried exacly this: options = optimset('Display', 'off') ; [....] = fmincon(@fun, x0, A, b, Aeq, beq, lb, ub, nonlcon, options) ; – DanielTheRocketMan Feb 24 '14 at 11:06
  • 1
    @Linus- Have you seem this: http://stackoverflow.com/questions/3029636/suppressing-a-functions-command-window-output. It may work. I dont have matlab now to test. As I understood it is basically overriding the behavior of the function display. – DanielTheRocketMan Feb 24 '14 at 11:10
  • I did not know `evalc()`, (I am fairly new to matlab, been working with python so far), it does exactly what i wanted, thx. – Linus- Feb 24 '14 at 12:14
  • One more thing I'd like to know, can I somehow generate confidence bounds with this method? That was always quite useful when using the normal fit routine. – Linus- Feb 24 '14 at 14:53
  • @Linus- Sure! This is a basic nonlinear least squares! – DanielTheRocketMan Feb 24 '14 at 18:38
  • @Linus-, maybe you should ask a specific question about this. Because I guess it is going to require another specific implementation. I dont think there is some cooked in Matlab to do that. The point is that the shape of your model is very unnusual. So, Many people can help you! If I am around I can try! – DanielTheRocketMan Feb 24 '14 at 19:03
  • Since i didn't want to plow through piles of statistical math, I came up with a different approach, where I define a piecewise function and shift the data in a way that the x-values do not overlap. That way I can use the standard fit methods provided by matlab and access all the additional information (confint, predint, etc.). – Linus- Feb 28 '14 at 16:34
  • @DanielTheRocketMan I have a somewhat similar problem where I want to fit a system of ODEs to two data sets in order to estimate 3 parameters. These 3 estimated values of the parameters are same for both data sets. If I don't have any constraints to be used as the parameters for a matrix (in)equation what do I use as the four parameters of fmincon(). Also, can you please suggest a way how I can use this to optimise ODE system – sam_rox Mar 06 '18 at 12:09
0

Off the wall, but how about try fitting: f(x)+g(x) = a*exp(x)+b + atan(b*x/a)

What do you think? Is that totally stupid?

I know it won't work (and probably little will) if the magnitudes of f(x) & g(x) don't match because then the errors (which your are minimizing) for the two different parts are going to be unequal and you'll end up fitting one function or the other anyway.

Frederick
  • 1,271
  • 1
  • 10
  • 29
  • I think one of the assumptions is that he has a different data set for each function. So this solution is not implementable. – DanielTheRocketMan Feb 22 '14 at 01:21
  • I have different cases, some where the x data is indeed the same and I could try to implement your version there, but as Daniel pointed out, it won't work if that is not the case. However, it might be faster than his suggestion in case of same x data – Linus- Feb 24 '14 at 09:33