TroubleShoot FMinSearch with subscript assignment mismatch

1 visualizzazione (ultimi 30 giorni)
I keep getting the following error:
Subscripted assignment dimension mismatch.
Error in fminsearch (line 190)
fv(:,1) = funfcn(x,varargin{:})
when I try to run the following optimization
objective = @(p) log(besseli(0,(((p(1)*exp(-xData/p(2))).*yData)./Variance),1)) - (((p(1)*exp(-xData/p(2))).^2) ./ (2.*Variance));
% unconstrained nonlinear optimization
parEst = fminsearch(objective,p0);
p0 is [1,2]. Variance is a predefined scalar. xData and yData are both [1,8].
Please help me as I have tried reading the help documents for fminsearch and for anonymous functions, but can't figure it out.

Risposta accettata

Torsten
Torsten il 15 Gen 2016
A scalar must be returned to fminsearch from "objective" ; you return a vector.
Best wishes
Torsten.
  3 Commenti
Torsten
Torsten il 15 Gen 2016
You will have to supply the sum of squared differences of your model expression and your experimental data. But I must admit, since your function involves both xtata and ydata, I don't know what is yi_model and what is yi_experiment.
Best wishes
Torsten.
Eric Diaz
Eric Diaz il 15 Gen 2016
Initially, I tried to break down my objective function, however I ran into issues with not having my p (parameter vector) variable defined as Matlab executed the term1 definition. So, I just put my model straight into my objective function in my previous post.
This is how I would like to write my code, as it reads more clearly. Please note that I put the sum function around the objective function below. Is the correct way to do the sum? Also, do you know a way around not having the parameter vector defined?
signalModel = p(1)*exp(-xData/p(2));
term1 = (signalModel.*yData)./Variance;
term2 = (signalModel.^2) ./ (2.*Variance);
objective = @(p) sum(log(besseli(0,term1,1)) - term2);

Accedi per commentare.

Più risposte (1)

John D'Errico
John D'Errico il 15 Gen 2016
This happens over and over again. fminsearch is an OPTIMIZER. It does not do nonlinear regression. There is a difference.
An optimizer finds the minimum value of a general function of multiple parameters. So a SCALAR valued function of one or more variables.
A nonlinear regression can be turned into an optimization by forming the sum of squares of residuals, and minimizing that as an objective.
But if you just throw a vector into a tool like fminsearch, expect it to fail. fminsearch has no idea what you want to do. Computers cannot read your mind.
  1 Commento
Eric Diaz
Eric Diaz il 15 Gen 2016
Thanks for your input John. I think what I need to do is just figure out the correct way to do that summation over the time points.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by