Problem with fsolve -- Failure in initial user-supplied objective function evaluation
조회 수: 6 (최근 30일)
이전 댓글 표시
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
채택된 답변
Matt J
2014년 7월 22일
편집: Matt J
2014년 7월 22일
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
추가 답변 (1개)
Michael Haderlein
2014년 7월 22일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!