FMINUNC cannot continue.

4 visualizaciones (últimos 30 días)
Rahat
Rahat el 14 de Nov. de 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.

Respuesta aceptada

Grzegorz Knor
Grzegorz Knor el 14 de Nov. de 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 comentario
Rahat
Rahat el 15 de Nov. de 2011
Thank you for your help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Problem-Based Optimization Setup en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by