Filter löschen
Filter löschen

How can I run this costfunction optimization ?

1 Ansicht (letzte 30 Tage)
payman khayree
payman khayree am 18 Nov. 2013
Beantwortet: Anisha Mishra am 22 Aug. 2020
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

Antworten (1)

Anisha Mishra
Anisha Mishra am 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.

Kategorien

Mehr zu Problem-Based Optimization Setup finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by