Problem with fsolve -- Failure in initial user-supplied objective function evaluation
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponse acceptée
Matt J
le 22 Juil 2014
Modifié(e) : Matt J
le 22 Juil 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,
0 commentaires
Plus de réponses (1)
Michael Haderlein
le 22 Juil 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
Voir également
Catégories
En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!