FMINUNC cannot continue.

4 Ansichten (letzte 30 Tage)
Rahat
Rahat am 14 Nov. 2011
Hi, I am learning to use Matlab. I've copied below mentioned function from Matlab help and it is supposed to give me solution of (x) and value of function at (x). But when I run this (I provide input x=[2 2]) matlab gives me an error message.
"Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue."
The function is shown below.
function [ f ] = myfun( x )
f = 3*x(1)^2 + 2*x(1)*x(2) + x(2)^2; % Cost function
x0 = [1,1];
[x,fval] = fminunc(@myfun,x0)
end.
Please help.

Akzeptierte Antwort

Grzegorz Knor
Grzegorz Knor am 14 Nov. 2011
You have to save your function myfun in separate file myfun.m
function [ f ] = myfun( x )
f = 3*x(1)^2 + 2*x(1)*x(2) + x(2)^2; % Cost function
end
And then execute commands:
x0 = [1,1];
[x,fval] = fminunc(@myfun,x0)
Or simpler (in one line without additional file):
[x,fval] = fminunc(@(x)3*x(1)^2 + 2*x(1)*x(2) + x(2)^2,[1 1]);
See also:
  1 Kommentar
Rahat
Rahat am 15 Nov. 2011
Thank you for your help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Problem-Based Optimization Setup finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by