Computer specific fsolve error

I am encountering an error using the fsolve function that appears to be specific to my computer. The code below runs perfectly fine on other computers (and when I paste it in here annoyingly) but when I run it on my machine I get the errors shown below. I was originally on matlab R2019a and have since updated to R2022b to try and fix the issue but I am still seeing the same errors.
Error Messages from command prompt:
Error using formatFsolveMessage
'optim:fsolve:Exit100basic' is an invalid option
Error in fsolve (line 457)
[EXITFLAG,OUTPUT.message] = formatFsolveMessage(Resnorm,sqrtTolFunValue,EXITFLAG, ...
Error in Rec3 (line 15)
lamda(i) = fsolve(@(l) fun(l),x0(i)) - Show complete stack trace
Code:
n = 4;
l = linspace(0,150,10000);
% approx the roots
x0 = [0,3,37,23.15,62.67];
fun = @(l) sin(sqrt(l)) + 2.*sqrt(l).*cos(sqrt(l));
for i =1:n
lamda(i) = fsolve(@(l) fun(l),x0(i))
end
Equation solved at initial point. fsolve completed because the vector of function values at the initial point is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
lamda = 0
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
lamda = 1×2
0 3.3731
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
lamda = 1×3
0 3.3731 23.1923
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
lamda = 1×4
0 3.3731 23.1923 23.1923

1 Comment

I don't recognize the error message, but the problem formulation has issues. You should be using lsqnonlin to impose positiveity constraints, and you should remove the sqrt() operations, because they are non-differentiable,
x0 = [0,3,37,23.15,62.67]
x0 = 1×5
0 3.0000 37.0000 23.1500 62.6700
fun = @(x) sin(x) + 2.*x.*cos(x);
for i =1:numel(x0)
lamda(i) = lsqnonlin(fun,sqrt(x0(i)),0,[]).^2;
end
Initial point is a local minimum. Optimization completed because the size of the gradient at the initial point is less than the value of the optimality tolerance. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
lamda
lamda = 1×5
0 3.3731 23.1923 23.1923 62.6797

Sign in to comment.

Answers (0)

Categories

Products

Release

R2022b

Asked:

on 2 Feb 2023

Edited:

on 2 Feb 2023

Community Treasure Hunt

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

Start Hunting!