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

1 view (last 30 days)
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.

Accepted Answer

Matt J
Matt J on 22 Jul 2014
Edited: Matt J on 22 Jul 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,

More Answers (1)

Michael Haderlein
Michael Haderlein on 22 Jul 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 Comment
Paladin
Paladin on 24 Jul 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 :-)

Sign in to comment.

Categories

Find more on Optimization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!