Double parameter optimisation using optimisation toolbox

2 views (last 30 days)
Hi,
I am wondering if someone could start me off in the right direction.
I have two parameters. P1 ranges from 30:-30 and P2 from 0.00000000000000001:1.
These two parameters can be used to generate a graph. I can then have the X,Y data for that graph.
I have a second graph that i obtain from an experiment and i want to know what values of P1 and P2 will produce that graph(or one that is similar to a fine value).
So i need to sum (experimentalYpoint - generatedYpoint)^2 across all my x points. This will give me a value of how 'similar' the two are. i then optimise P1 and P2 so that the value of my sum gets as close to 0 as it can be.
I think i can use the Optimization Toolbox for this but would someone be able to point me to exactly which part of it that would be useful.
Forry for the long winded question, wanted to try make it clear.
  3 Comments
Charles Mitchell-Thurston
Charles Mitchell-Thurston on 31 May 2022
'given input data xdata, and the observed output ydata, where xdata and ydata are matrices or vectors, and F (x, xdata) is a matrix-valued or vector-valued function of the same size as ydata.'
In this is my xdata P1 and ydata P2?
Torsten
Torsten on 31 May 2022
Edited: Torsten on 31 May 2022
xdata = independent variable on which your "experimentalYpoints" depend (e.g. time, length or whatever)
ydata = experimentalYpoints
x = [P1,P2] = vector of parameters to be fitted

Sign in to comment.

Accepted Answer

Matt J
Matt J on 31 May 2022
Edited: Matt J on 31 May 2022
In this is my xdata P1 and ydata P2?
xdata, ydata are what you call experimtalX and experimentalY. The unknown parameters that you are trying to solve for go in the vector x, that is x=[P1,P2].
  19 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 1 Jun 2022
Great. In the function you'll have to assign the relevant result to the y-variable.
Perhaps end the function with:
y = cell2mat(inter_y{K});

Sign in to comment.

More Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 31 May 2022
The standard fitting method I use is:
% Your curve-function definition (I just mock one up, if you have it defined in a
% matlab-function you don't need this step)
curve_fcn = @(P1P2,x) P1P2(1)*(exp(-(x-P1P2(2)).^2/1e3) + exp(-(x+P1P2(2)).^2/1e3));
% a sum-of-squares error-function for the fitting
err_fcn = @(P1P2,x,y,fcn) sum((y-fcn(P1P2,x)).^2);
% or a residual between the observed y and your curve-function
res_fcn = @(P1P2,x,y,fcn) (y-fcn(P1P2,x));
P1P2_guess = [1,2]; % Initial guess for the parameters P1 and P2, (adjust to be good values)
% Parameter-fitting with fminsearch
P1P2_fms = fminsearch(@(par) err_fcn(par,x,y,curve_fcn),P1P2_guess);
% Parameter-fitting with lsqnonlin:
P1P2_lsq = lsqnonlin(@(par) res_fcn(par,x,y,curve_fcn),P1P2_guess);
That should give you good parameters for P1 and P2 as the first and second element of P1P2_fms and/or P1P2_lsq.
I find these functions easier to work with since this is more transparend and I know what goes in (old dogs and new tricks are said to not go well...).
HTH
  2 Comments
Matt J
Matt J on 31 May 2022
Edited: Matt J on 31 May 2022
???
But the only difference between your workflow and lsqcurvefit, is that lsqcurvefit takes your curv_fcn directly:
curve_fcn = @(P1P2,x) P1P2(1)*(exp(-(x-P1P2(2)).^2/1e3) + exp(-(x+P1P2(2)).^2/1e3));
P1P2_guess = [1,2]; % Initial guess for the parameters P1 and P2, (adjust to be good values)
P1P2_lsq = lsqcurvefit(curve_fcn,P1P2_guess, x,y);
Moreover, in either workflow, you will inevitably need to provide curve_fcn for when it comes time to plot the fit.
Bjorn Gustavsson
Bjorn Gustavsson on 1 Jun 2022
@Matt J, you're right (again). It is probably only due to habit, I started using fmins/fminsearch (and fminsearchbnd) and lsqnonlin and never bothered starting to use lsqcurvefit because I had gotten used to the setups for the others. You teach me a lot about myself recently, I don't like what I learn but I do appreciate it...

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!