Problem with fsolve -- Failure in initial user-supplied objective function evaluation

6 visualizaciones (últimos 30 días)
I am not sure whether it is due to the problem fsolve cannot solve this equation:
f=[(log(1+2*d)/2*d)-q];
What I did was, first of all I made a m file like this:
function f=sr(d)
f=[(log(1+2*d)/2*d)-q];
I have a series of q values, so in my main function I wrote this:
D=zeros(184,1);
c0=[0 4];
for i=1:184;
q=Q(i,1);
D(i,1)=fsolve(@sr,c0);
end
where Q stores the values for q.
The I encountered error messages as below:
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==> sr at 2
f=[(log(1+2*d)/2*d)-0.715667311];
Error in ==> fsolve at 248
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE
cannot continue.
Is there anything I did wrong? Or is there any way I can solve this equation?
Many thanks for helping hands in advance.

Respuesta aceptada

Matt J
Matt J el 22 de Jul. de 2014
Editada: Matt J el 22 de Jul. de 2014
c0 should be a scalar. Since you only have 1 equation, you presumably only have 1 unknown. So, if d is a scalar, its initial guess c0 should be a scalar, too.
You also haven't passed q to sr() in any way. See here for ways to pass fixed parameters to obejctive functions,

Más respuestas (1)

Michael Haderlein
Michael Haderlein el 22 de Jul. de 2014
Actually, I'm a bit puzzled why that specific error appears. However, a few questions arise:
(I): Why is there no input variable q in the function sr? It is used in the function. Did you maybe use global variables?
(II): Why do you use the [] brackets in sr?
(III): If you set d to a vector ([0 4] in your example), you most likely want to get sf(0) and sf(4)? Then you need to use element-wise operations in your function (.* instead of *).
(IV): If you really want to solve sr simultaneously from two different starting points, you will get two answers. Accordingly, D needs to have two columns.
Hope I could help you. Best regards,
Michael
  1 comentario
Paladin
Paladin el 24 de Jul. de 2014
I am a newbie and just followed the tutorial videos on youtube, and obviously I have misunderstood some of the examples. Thank you for asking these questions so I can have a better understanding of these examples :-)

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by