"Error not enough input arguments "

조회 수: 3 (최근 30일)
ananya mittal
ananya mittal 2020년 6월 13일
댓글: Walter Roberson 2020년 6월 14일
This is my code and I am getting error" not enough input parameters" in line 4 .Can anyone please rectify the problem ?
function[jval, gradient]= costfuntion(theta)
jval= (theta(1)-5)^2 + ...
(theta(2)-5)^2;
gradient= zeros(2,1);
gradient(1)= 2*(theta(1)-5);
gradient(2)= 2*(theta(2)-5);
options = optimset('GradObj','on', 'MaxIte', 100);
initialTheta= zeros(2,1);
[optTheta,funcval, exitFlag]= fminunc(@costfunction, initialTheta, options);
  댓글 수: 2
Aditya Verma
Aditya Verma 2020년 6월 13일
Hi! May I know how you're trying to call this function? According to my understanding, you're trying to operate on a matrix with two elements. A possible call to this function could be:
costfuntion([10 20])
Walter Roberson
Walter Roberson 2020년 6월 14일
The lines start from options need to be either before the function or else in a different file.

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

채택된 답변

KSSV
KSSV 2020년 6월 13일
I think you are running your code by using either run button or F5 button. This is a function, you cannot run like that. You need to save it in a directory on the name costfunction.m. And call it in command window.
theta = rand(1,2) ; % some random values for demo
[jval, gradient]= costfunction(theta) ;
I have checked the code, it is running fine.
  댓글 수: 1
ananya mittal
ananya mittal 2020년 6월 14일
I am trying this on MATLAB 2017a. It is running an infinite loop of error. And shows the error
"THROWFMINUNCGRADOBJANDLARGESCALEWARNING throws a warning for the
% following case. For R2017a and R2017b only, we are still supporting
% "optimset" users who specify the trust-region algorithm in fminunc via
% the GradObj and LargeScale options for fminunc. From 18a onwards,
% LargeScale will be ignored for fminunc and users will have to use
% Algorithm."

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by