Passing Variable to Function in Multistart

1 visualizzazione (ultimi 30 giorni)
Joshua Agar
Joshua Agar il 20 Lug 2015
Risposto: Madhav Rajan il 22 Lug 2015
My ultimate goal is to fit a curve to raw data that with a complex shape to a function that contains 25 variables of known form. I have had best luck obtaining good fits by weighting the fitting function by the derivative and completing the fitting using lsqcurvefit. Is there a way that I can either:
1.Pass or make available the weights calculated to the objective function? 2. Calculate the weights based on the raw Y data?

Risposte (1)

Madhav Rajan
Madhav Rajan il 22 Lug 2015
I understand that you want to pass some parameters to the objective function of "lsqcurvefit" and use them in your calculations. Assuming that you have the Optimization Toolbox available, the following example explains how to pass extra parameters to the objective function.
Let the original 'xdata' and 'ydata' contain 10 elements each similar to the example mentioned in the "lsqcurvefit" documentation available in the following link:
>> xdata = [0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
>> ydata = [455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];
>> x0 = [100; -1] % Starting guess
Lets suppose you want to pass both 'xdata' and 'ydata' to the objective function for your computation. You can create a new variable 'xydata' that stores the concatenated matrix of both 'xdata' and 'ydata' as follows:
>> xydata=[xdata ydata];
You can then pass this 'xydata' variable as the third argument to the 'lsqcurvefit' function as shown:
>> [x,resnorm] = lsqcurvefit(@myfun,x0,xydata, ydata);
Now 'xydata' is available as the second argument to the objective function described in the function file 'myfun.m' as follows:
function F = myfun(x,xdata)
ydata=xdata(11:20);
xdata=xdata(1:10);
F = x(1)*exp(x(2)*xdata);
'ydata' is now available inside the above objective function and you can use it in your calculations.
You can extend this example to pass any number of parameters to your objective function.
I noticed that you obtained your fits by weighting the fitting function by its derivative. Assuming that you have the Curve Fitting Toolbox installed , you could also explore the different types of curve and surface fits available in MATLAB. You can refer the following documentation for the "fittype" function that constructs fit types by specifying library model names:
You can also refer the following documentation for the "fit" function that can fit curves or surface to your data.
Hope I have answered your question.

Categorie

Scopri di più su Global or Multiple Starting Point Search in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by