Info
此问题已关闭。 请重新打开它进行编辑或回答。
Whats mistake in this code....can anybody guide me?
1 次查看(过去 30 天)
显示 更早的评论
z = sym('2*x1 * x1 + x2*x2 + 3*x3*x3');
syms x1 x2 x3 a;
gradz = [diff(z,x1); diff(z,x2); diff(z,x3)];
x1(1)=2;
x2(1)=-2;
x3(1)=1;
for k=2:5
p=subs(gradz, [x1 x2 x3], [x1(k-1) x2(k-1) x3(k-1)]);
x1a=x1(k-1)+p(1)*a;
x2a=x2(k-1)+p(1)*a;
x3a=x3(k-1)+p(1)*a;
za=subs(z, [x1 x2 x3], [x1a x2a x3a]);
w=solve(za);
x1(k)=x1(k-1)+p(1)*w;
x2(k)=x2(k-1)+p(2)*w;
x3(k)=x3(k-1)+p(3)*w;
end
Its giving error
2 个评论
回答(1 个)
Walter Roberson
2013-3-24
When the explicit solution could not be found, solve() is returning the empty matrix, which you assign into "w". The empty matrix multiplied by p(1) gives the empty matrix. Add x1(k-1) to the empty matrix and you get the empty matrix. You then try to assign that empty matrix, which is of size 0, to x1(k) which is of size 1.
1 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!