FMINUNC cannot continue.

4 visualizzazioni (ultimi 30 giorni)
Rahat
Rahat il 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.

Risposta accettata

Grzegorz Knor
Grzegorz Knor il 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:

Più risposte (0)

Categorie

Scopri di più su Problem-Based Optimization Setup in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by