How can I run this costfunction optimization ?
1 次查看(过去 30 天)
显示 更早的评论
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
回答(1 个)
Anisha Mishra
2020-8-22
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Problem-Based Optimization Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!