求告知以下matlab求解的程序哪里错误了 5
n=2;theta1=sym('x',[1,n]);[jval,grad]=daijia(theta1);options=optimset('GradObj','on',...
n=2;
theta1=sym('x',[1,n]);
[jval,grad]=daijia(theta1);
options=optimset('GradObj','on','MaxIter',100);
initialTheta=zeros(2,1);
[optTheta,functionVal,exitFlag]=fminunc(@daijia,initialTheta,options);
function [jval,grad]=daijia(theta1)
x=[1 0;0 1];
y=[5 5];
s1=0;
s2=0;
for i=1:2
s1=s1+x(1,i)*theta1(i);
end
s1=s1-y(1);
jval1=s1^2;
for i=1:2
s2=s2+x(2,i)*theta1(i);
end
s2=s2-y(1);
jval2=s2^2;
jval=jval1+jval2;
grad=gradient(jval,theta1); 展开
theta1=sym('x',[1,n]);
[jval,grad]=daijia(theta1);
options=optimset('GradObj','on','MaxIter',100);
initialTheta=zeros(2,1);
[optTheta,functionVal,exitFlag]=fminunc(@daijia,initialTheta,options);
function [jval,grad]=daijia(theta1)
x=[1 0;0 1];
y=[5 5];
s1=0;
s2=0;
for i=1:2
s1=s1+x(1,i)*theta1(i);
end
s1=s1-y(1);
jval1=s1^2;
for i=1:2
s2=s2+x(2,i)*theta1(i);
end
s2=s2-y(1);
jval2=s2^2;
jval=jval1+jval2;
grad=gradient(jval,theta1); 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询