"Error not enough input arguments "

1 view (last 30 days)
ananya mittal
ananya mittal on 13 Jun 2020
Commented: Walter Roberson on 14 Jun 2020
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 + ...
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);
Walter Roberson
Walter Roberson on 14 Jun 2020
The lines start from options need to be either before the function or else in a different file.

Sign in to comment.

Accepted Answer

KSSV on 13 Jun 2020
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 Comment
ananya mittal
ananya mittal on 14 Jun 2020
I am trying this on MATLAB 2017a. It is running an infinite loop of error. And shows the error
% 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."

Sign in to comment.

More Answers (0)


Find more on Problem-Based Optimization Setup in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!