MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Avigdor on 6 Sep 2012

Hi,

I am trying to model two vector data sets f1(x) and f2(x) using two non-linear equations with common variables.

For example:

f1(x) = a0 + a1(k1,t)*exp(-l1*x) + a2(k1,t)*exp(-l2*x) f2(x) = b0 + b1(k1,t)*exp(-l1*x) + b2(k1,t)*exp(-l2*x)

I would like to simultaneously fit f1(x) and f2(x) to these two equations. Then graphically display the data and fits. The a1, a2, b1, and b2 are expressions which are different but contain the variables k1 and t which will be determined from the simultaneous fit. I would like to use matlab script to do this.

Is fsolve the way to do this? If so, can somebody please give a little direction as to setting this up?

Edit: I have been trying fmincon, but need the minimization to output a global minimum for the vector data sets, not a minimum at each point. Is there a way around this in simple code?

Thanks!

*No products are associated with this question.*

Answer by Matt Tearle on 6 Sep 2012

Edited by Matt Tearle on 6 Sep 2012

You can treat this as a least-squares problem with 6 parameters: `a0`, `b0`, `k1`, `t`, `l1`, and `l2`. Then make your objective function the total square error ((y1 - f1(x))^2 + (y2 - f2(x))^2). So something like

function err = myerrorfun(c,x1,y1,x2,y2) f1 = c(1) + [function of c(3) and c(4)]*exp(-c(5)*x1) + ...; f2 = c(2) + [function of c(3) and c(4)]*exp(-c(5)*x2) + ...; err = (y1-f1).^2 + (y2-f2).^2;

Then in your main program, call a minimization routine like `fmincon`:

x1 = ... % enter/load x2 = ... % all y1 = ... % the y2 = ... % data

% make a function handle of one variable (the parameters), with the data embedded objective = @(c) myerrorfun(c,x1,y1,x2,y2); % do the fitting c_fit = fmincon(objective,...);

Avigdor on 9 Sep 2012

Thank you for the information and concise code!

Do you have any suggestions as to estimation of goodness of fit, or error in fit from such a method?

Matt Tearle on 12 Sep 2012

In this case, `myerrorfun` is actually returning the sum of the squared error. You can get the function value as a second output from `fmincon` (or whatever minimization function you used). But if you want more detailed info, you could make functions to evaluate `f1` and `f2`; call these with `c_fit` and `x1` or `x2`; take the difference with `y1` and `y2` and now you have the residuals. Apply whatever standard analysis you normally would to the residuals of a regression -- `hist`, `normplot`, `scatter(resids(1:end-1),resids(2:end))`, etc. Or, of course, apply your favorite goodness-of-fit formula (eg adjusted R^2).

## 0 Comments