필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Whats mistake in this code....can anybody guide me?

조회 수: 1 (최근 30일)
Shantanu K
Shantanu K 2013년 3월 24일
마감: MATLAB Answer Bot 2021년 8월 20일
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
the cyclist
the cyclist 2013년 3월 24일
Which lines gives the error, and what is the full text of the error message?
Shantanu K
Shantanu K 2013년 3월 24일
Warning: Explicit solution could not be found.
> In solve at 81
In cauchyloop at 18
??? Error using ==> mupadmex
Error in MuPAD command: Subscripted assignment dimension mismatch
Error in ==> sym.sym>sym.subsasgn at 1420
C = mupadmex('mllib::subsasgn',A.s,B.s,inds{:});
Error in ==> cauchyloop at 20
x1(k)=x1(k-1)+p(1)*w;
This is error i am geting at line x1(k)=x1(k-1)+p(1)*w;

답변 (1개)

Walter Roberson
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
Shantanu K
Shantanu K 2013년 3월 24일
Hello Walter,
Variable 'w' is not empty. It is having some finite value. 'za' is linear equation which should be solved to produce 'w'.

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by