why do I get an error with fsolve in R2012b if in R2007a it works fine ?
1 view (last 30 days)
Show older comments
Antonio Ribeiro
on 15 May 2014
Commented: Antonio Ribeiro
on 16 May 2014
I'm facing with different results returned by the algorithm fsolve between the releases R2007a and R2012b. In my case, when I run the code below in Matlab R2007a:
opts = optimset('Display','iter','FunValCheck','on','MaxIter',200,'MaxFunEvals',400,'TolX',1e-6,'TolFun',1e-6);
%
[filename,pathname] = uigetfile('*.dat','Select the data file to test fsolve(.)');
fstr = [pathname filename];
a = dlmread(fstr,' '); % load envelope data file with 1000 i.i.d. samples
%
Z = a;
ER = mean(Z);
ER2 = mean(Z.^2);
ER4 = mean(Z.^4);
x0 = [2.25; 1.00];
%
fl = @(x)ekm6(x,ER,ER2,ER4); % function handle
[x,fval,exitflag,output] = fsolve(fl,x0,opts)
which calls the function ekm6.m:
function f = ekm6(x,ER,ER2,ER4)
%
f = [(1 + 2*x(1))/(x(2)*(1 + x(1))^2) - (ER4/ER2^2 - 1);
(gamma(x(2) + 1/2)*(hypergeom(-1/2,x(2),-x(1)*x(2))/gamma(x(2))))/sqrt((1 + x(1))*x(2)) - ER/sqrt(ER2)];
I get the result below (the expected result):
Norm of First-order Trust-region
Iteration Func-count f(x) step optimality radius
0 3 0.00312567 0.0294 1
1 6 2.70862e-005 0.103684 0.00224 1
2 9 2.37394e-009 0.0123891 2.06e-005 1
3 12 1.43461e-017 0.000183517 1.6e-009 1
Optimization terminated: first-order optimality is less than options.TolFun.
x =
2.2729
1.1127
...
output =
iterations: 3
funcCount: 12
algorithm: 'trust-region dogleg'
firstorderopt: 1.5979e-009
message: 'Optimization terminated: first-order optimality is less than options.TolFun.'
NOTE: the file loaded by command dlmread() is a sequence of 1000 i.i.d. random variables (attached)
On the other hand, when the same code above is applied in Matlab R2102b, I get the error:
Error using lsqfcnchk/checkfun (line 136)
User function '@(x)ekm6(x,ER,ER2,ER4)' returned a complex value when evaluated;
FSOLVE cannot continue.
Error in C:\Program Files\MATLAB\R2012b\toolbox\shared\optimlib\finDiffEvalAndChkErr.p>finDiffEvalAndChkErr (line 26)
Error in C:\Program Files\MATLAB\R2012b\toolbox\shared\optimlib\finitedifferences.p>finitedifferences (line 128)
Error in trustnleqn (line 97)
[JACfindiff,~,~,numFDfevals] = finitedifferences(x,funfcn{3},[],[],[],Fvec,[],[], ...
Error in fsolve (line 403)
[x,FVAL,JACOB,EXITFLAG,OUTPUT,msgData]=...
Error in test_fsolve (line 17)
[x,fval,exitflag,output] = fsolve(fl,x0,opts)
The optimization toolbox is properly installed in R2012b since I've successfully tested the Example 1 in the fsolve() help page.
Is there any change in the fsolve() code of R2012b that leads to this error ? How can I run this code without error with R2012b ? I would appreciate any help, cause I've not found similar question in the Mathworks support page. Best regards.
0 Comments
Accepted Answer
Matt J
on 15 May 2014
Edited: Matt J
on 15 May 2014
I doubt the key question is the difference between the MATLAB versions. The key question is probably whether ekm6 is indeed generating complex values, and if so, why. Modify as follows (or set a conditional breakpoint) to trap the occurrence of a complex f.
function f = ekm6(x,ER,ER2,ER4)
%
f = [(1 + 2*x(1))/(x(2)*(1 + x(1))^2) - (ER4/ER2^2 - 1);
(gamma(x(2) + 1/2)*(hypergeom(-1/2,x(2),-x(1)*x(2))/gamma(x(2))))/sqrt((1 + x(1))*x(2)) - ER/sqrt(ER2)];
is ~isreal(f), keyboard; end
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!