FMINUNC cannot continue.

조회 수: 4 (최근 30일)
Rahat
Rahat 2011년 11월 14일
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.

채택된 답변

Grzegorz Knor
Grzegorz Knor 2011년 11월 14일
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
Rahat
Rahat 2011년 11월 15일
Thank you for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by