FMINUNC cannot continue.

4 vues (au cours des 30 derniers jours)
Rahat
Rahat le 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.

Réponse acceptée

Grzegorz Knor
Grzegorz Knor le 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 commentaire
Rahat
Rahat le 15 Nov 2011
Thank you for your help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Problem-Based Optimization Setup dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by