Error in fminunc function line 477

The fminunc function gives the error below any time I try to use the function, even for the simple example listed in the MATLAB help. Is the fminunc function corrupted on my computer?
fun = @(x)3*x(1)^2 + 2*x(1)*x(2) + x(2)^2 - 4*x(1) + 5*x(2);
x0 = [1,1];
[x,fval] = fminunc(fun,x0)
***************
Error using message/getString
An unknown error occurred in accessing the Message Catalog.
Error in createExitMsg (line 56)
stopCriteriaDetails = getString(message('optimlib:commonMsgs:StopCriteriaDetails',detailedMsgCmd));
Error in fminusub (line 301)
output.message = createExitMsg(msgData{:});
Error in fminunc (line 472)
[x,FVAL,GRAD,HESSIAN,EXITFLAG,OUTPUT] = fminusub(funfcn,x, ...
***************

4 件のコメント

Adam Danz
Adam Danz 2021 年 8 月 30 日
編集済み: Adam Danz 2021 年 8 月 30 日
Datapoint:
The code runs file from R2021a
fun = @(x)3*x(1)^2 + 2*x(1)*x(2) + x(2)^2 - 4*x(1) + 5*x(2);
x0 = [1,1];
[x,fval] = fminunc(fun,x0)
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
x = 1×2
2.2500 -4.7500
fval = -16.3750
I don't have r2020a running at the moment so I cannot confirm whether this error occurs for that release.
John D'Errico
John D'Errico 2021 年 8 月 30 日
fun = @(x)3*x(1)^2 + 2*x(1)*x(2) + x(2)^2 - 4*x(1) + 5*x(2);
x0 = [1,1];
[x,fval] = fminunc(fun,x0)
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
x = 1×2
2.2500 -4.7500
fval = -16.3750
That is what you should have seen.
Where does the error occur? We can see it had actually finished the work, only to get hung up as it was creating its return.
Error in createExitMsg (line 56)
stopCriteriaDetails = getString(message('optimlib:commonMsgs:StopCriteriaDetails',detailedMsgCmd));
Error in fminusub (line 301)
output.message = createExitMsg(msgData{:});
So it was almost done.
My guess is, you may have accidentally edited the code of some function necessary for fminunc to work. Or, you may have done something like move these functions out of their initial location on the search path. That is a bad thing to do. In some way, yes, fminunc has essentially been corrupted. The simplest solution is to just redownload a valid version of MATLAB, or at least the impacted toolbox, thus the optimization toolbox. As long as you have a valid license, there is no problem to do this. I would just download the entire installation, to make sure all is working properly.
Do NOT move functions around or edit them. If you wish to look at code, then use the type function, which will allow you to view the code in MATLAB.
Sohaib Kiani
Sohaib Kiani 2023 年 2 月 1 日
I am getting the same error. Just Installed the fresh copy and didn't modify anything but fminunc is throwing the same error.
Christopher
Christopher 2023 年 4 月 19 日
I am getting the same error. This is my first time installing Matlab.

サインインしてコメントする。

回答 (2 件)

Steven Lord
Steven Lord 2023 年 4 月 19 日

0 投票

Let's check that you haven't written or downloaded a file that's conflicting with a function provided by MATLAB. What do the following two commands show when you run them on your system?
which -all message
message is a built-in method % message constructor /MATLAB/toolbox/matlab/lang/message.m % Shadowed /MATLAB/toolbox/shared/dsp/filterdesignlib/@FilterDesignBlockDialog/message.m % FilterDesignBlockDialog method /MATLAB/toolbox/signal/sigtools/@siggui/message.m % siggui method
which -all getString
getString is a Java method % com.mathworks.mde.desk.MLDesktop method getString is a Java method % com.mathworks.widgets.desk.Desktop method getString is a built-in method % message method getString is a built-in method % matlab.internal.Catalog method getString is a Java method % java.util.ResourceBundle method getString is a Java method % java.util.PropertyResourceBundle method
If you see entries that are not "a built-in method", "a Java method", or a file under matlabroot you may need to rename or remove those other files. Renaming would be safer, as you could always change the name back if absolutely necessary. Leave anything under matlabroot alone.
Álvaro Pérez Fernández
Álvaro Pérez Fernández 2023 年 12 月 19 日

0 投票

Hi, Adriane,
I was getting the same error until I installed the Optimization Toolbox software for Matlab. Maybe that would fix your problem too.

3 件のコメント

Matt J
Matt J 2023 年 12 月 19 日
It seems impossible that you would be getting the same error message if you didn't have the Optimization Toolbox installed. The error is thrown by an Optimization Toolbox function.
Álvaro Pérez Fernández
Álvaro Pérez Fernández 2023 年 12 月 19 日
Well, all I can say is that it happened to me. I was getting the forenamed error while using fminunc. However, fminsearch worked fine. Then I tried fmincon for another problem and Matlab showed me a message asking me to install the Optimization Toolbox in order to use that function. And after installing the toolbox I ran once again the script with fminunc and it worked.
Before installing the toolbox I checked the files mentioned by Steven and I got the same results as him. So the error was not due to a conflict between a file and a Matlab function.
Dyuman Joshi
Dyuman Joshi 2023 年 12 月 19 日
Because there's a built-in function named fminsearch as well, similar to the one from the Optimization toolbox.
So, fminsearch() works without the optimization toolbox, but fminunc, which has no built-in counterpart, requires Optimization toolbox.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品

リリース

R2020a

タグ

質問済み:

2021 年 8 月 30 日

コメント済み:

2023 年 12 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by