How can I run this costfunction optimization ?

2 views (last 30 days)
I am trying to run this optimization:
function [jVal,gradient]=costfunction(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','MaxIter',100)
initialTheta=zeros(1,2)
[optTheta,functionVal,exitFlag]=fminunc(@costfunction,initialTheta,options)
But it says that you need more arguements for theta. How should I define theta for this function while I want matlab to calculate and minimize it?
TNX

Answers (1)

Anisha Mishra
Anisha Mishra on 22 Aug 2020
You have initialised initialTheta = zeros(1,2) which makes an empty 1x2 matrix , i.e , it has 1 row and two columns
where as the theta is a vector with 2 rows and 1 column ,i.e 2x1
So change your initialTheta as:
initialTheta=zeros(2,1);
This will solve your error.
Hope this helps.

Categories

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!