Fit model to measured data without toolbox

nlinfit () in the stat toolboox is likely to what I want but I do not have the stat toolbox (nor do I have the optimization toolbox). I need to fit my model to measured data and extract model parameters. My model function ( myModel.m) is of the form
function Pout = myModel (pVector, Lvector, G, H)
k = 1;
for L = Lvector
P = AnotherFunction (pVector, L, G, H)
Pout(k) = P;
k=k+1;
end
where, pVector = [a, b, c, d, ...]'; contains the model parameters a, b, c, d...so on and L_vector is another vector of different length (= number of measured data points). G and H are constants for a given case (measurement). Pout is a vector of the same length as Lvector. I have tried the following using a cost function and fminsearch but throws an error which I am unable to resolve.
pGuess = [aGuess, bGuess, cGuesss, dGuess...]; % initial guess values for model paramters
G = SomeknownConstant; H = SomeknownConstant;
load MeasuredData.dat; mData = MeasuredData;
myCostFunc = @(pVector) sum ((myModel (pVector, Lvector, G, H) - mData).^2); % my COST function
opts = optimset('MaxFunEvals',40000, 'MaxIter',10000);
pVectorEstimated = fminsearch (myCostFunc,pGuess, opts);
This gives me an error when I use Lvector as a vector but runs when Lvector is single valued? Lvector needs to be a vector (with length equal to that of mData) as used in myModel.m in order to generate a model data close to the measured data, which then gets fitted to the measured data by varying the model parameters. Where am I going wrong? Are there alternatives to nlinfit e.g in fileexchange that I could use?

Answers (2)

I suggest that you try evaluating myCostFunc(pGuess) and seeing if the result is a scalar.
If it is a scalar, then please provide the exact error message (copy-paste) for more help, as we cannot tell what is happening from the incomplete information you have provided.
Alan Weiss
MATLAB mathematical toolbox documentation

3 Comments

Please note, Pout is a vector of the same length as Lvector. Evaluating the code with fminsearch above gives an error (when Lvector = vector of same length as the measured data)
Error using -
Matrix dimensions must agree.
Error in @(pVector)sum((myModel(pVector,Lvector,G,H)-mData).^2)
Error in fminsearch (line 190)
fv(:,1) = funfcn(x,varargin{:});
Error in LetsFit2 (line 15) %%I called this piece of code - LetsFit2
pVectorEstimated = fminsearch(myCostFunc, pGuess, opts) ;
evaluating (as you suggest)
myCostFunc(pGuess)
gives an error
>> myCostFunc(pGuess)
Error using -
Matrix dimensions must agree.
Error in @(pVector)sum((myModel(pVector,Lvector,G,H)-mData).^2)
just read that fminsearch needs a scalar function..my bad. What are my options for a vector function as in my case?
Torsten
Torsten on 24 Oct 2017
Edited: Torsten on 24 Oct 2017
"myModel (pVector, Lvector, G, H)" must return a vector of the same length as "mData".
This does not seem to be the case.
Best wishes
Torsten.

Sign in to comment.

Categories

Products

Asked:

J M
on 24 Oct 2017

Answered:

JM
on 21 Nov 2017

Community Treasure Hunt

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

Start Hunting!