hello,
i have defined a function that is model, and want to use a fminsearch for a first value for the lsqnonlin,but i get all the time "Assignment has more non-singleton rhs dimensions than non-singleton subscripts" also in the error it says fv(:,1) = funfcn(x,varargin{:}); the model_error is the output to optimise
my code:
param0=[1, 50];
param0=fminsearch(@(param) Model(param,t,inlet_signal,outlet_signal),param0);
function [error_model,h]= dispmodel_2param(param,t,x,y)
theta = t/param(2);
h = (sqrt(param(1)./(4*pi*theta)).*exp(-param(1)./(theta*4).*(1-theta).^2));
h(1) = 0;
y=[y;y(end)*zeros((length(E_theta)+length(x)-1)-length(y),1)];
error_model= y-conv(h/param(2),x);
end

 Réponse acceptée

Walter Roberson
Walter Roberson le 1 Avr 2020
No matter whether t, inlet_signal and outlet_signal are scalars or vectors, your line
y=[y;y(end)*zeros((length(E_theta)+length(x)-1)-length(y),1)];
is making y into a vector at that point. Then your error_model would be a vector because it involves y.
When you use fminsearch there is a single output for the function, and it must return a scalar.
The form you are using appears to have been designed for use with lsqnonlin, which expects the individual predictions to be returned rather than a sum-of-squares . lsqnonlin internally does the equivalent of sum( (fun(Param) - y).^2 ) but fminsearch does not.

5 commentaires

thanks for the answer,
inlet_signal, outlet_signal, are vectors.
yes, you are completly correct that it was designed for lsqnonlin, as it is how i was doing my solve, but the issue i had was that the lsqnonlin was somewhat dependant of the initial values, so i wanted to use fminsearch to find estimations to be able to have "good" param0 values for the lsqnonlin.
from what you are telling me, i could define another output of the function model to be the norm of error_model and try to minimeze this with fminsearch and then use this values for lsqnonlin, i am correct?
if i am, how can i use a different output of the function that is not the first output? if i want to use the same function for fminsearch and lsqnonlin, i could use:
function [norm_error,error_model,h]= dispmodel_2param(param,t,x,y)
...
end
so then i fminsearch will work (from what i understand from you) but then lsqnonlin no (or at least not with the same line of code that was before...)
[param,resnorm,~,exitflag]=lsqnonlin(@(param) dispmodel_2param(param,t,inlet_signal,outlet_signal),param0);
thanks
param0=fminsearch(@(param) sum((dispmodel_2param(param,t,inlet_signal,outlet_signal)-outlet_signal).^2), param0);
franco otaola
franco otaola le 1 Avr 2020
Modifié(e) : franco otaola le 1 Avr 2020
lol, sometimes we dont think in the easier solutions....
thanks a lot for the help!
either way, i still have the doubt, can we call fmin or lsq for a i output that is not the first one of the function?
best regards!
Walter Roberson
Walter Roberson le 1 Avr 2020
No, not without using an extra function to capture the extra output and return it instead. It would have to be a true function, not an anonymous function, because of the way that MATLAB deals with additional outputs.
franco otaola
franco otaola le 1 Avr 2020
oh, okey, thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2017b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by