Question

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?

Was it helpful?

Solution

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!

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top