Error in using while loop?
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello, I am using while loop there is error in using it. I am not able to solve this.
x10=2;
x20=-2;
x30=1;
e1=0.00001;
iter=10;
m=2;
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)]; % returns a column vector
p=subs(gradz, [x1 x2 x3], [x10 x20 x30]); % evaluate at (0.25, 0.75)
x1a0=x10+a*p(1);
x2a0=x20+a*p(2);
x3a0=x30+a*p(3);
za0=subs(z, [x1 x2 x3], [x1a0 x2a0 x3a0]);
dza0=diff(za0,a);
k=solve(dza0);
x11=vpa(x10+k*p(1),8);
x21=vpa(x20+k*p(2),8);
x31=vpa(x30+k*p(3),8);
cltr1=((x11-x10)/x10);% THIS IS THE CONDITION THAT THIS VALUE SHOULD BE LESS THAN e1 then iterations will stop.
while ((m<iter) && (cltr1>0.001))
m=m+1;
p=subs(gradz, [x1 x2 x3], [x11 x21 x31]);
x1a=x11+p(1)*a;
x2a=x21+p(2)*a;
x3a=x31+p(3)*a;
za1=subs(z, [x1 x2 x3], [x1a x2a x3a]);
dza1=diff(za1,a);
w=solve(dza1);
x12=x11+p(1)*w;
x22=x21+p(2)*w;
x32=x31+p(3)*w;
cltr1=(x11-x10)/x10;
x1k=x11;
x2k=x21;
x3k=x31;
x11=x12;
x21=x22;
x31=x32;
end
display(x12)
display(x22)
display(x32)
Error: ??? Error using ==> sym.sym>notimplemented at 2514 Function 'gt' is not implemented for MuPAD symbolic objects.
Error in ==> sym.sym>sym.gt at 792
notimplemented('gt');
Error in ==> CAUCHY_1 at 28
while ((m<iter) && (cltr1>0.001))
댓글 수: 0
답변 (2개)
Walter Roberson
2013년 3월 31일
Your p appears to be a vector, so (abs(p)<e1) is going to return a vector result. You cannot use && with vectors. Change the && to &
댓글 수: 4
Walter Roberson
2013년 3월 31일
I do not see any definition of x32 ?
Your error message references x3k which did not exist in your previous code.
In your previous code, the "while" involved x31, which looks to me as if it might be symbolic involving at least one variable.
Ahmed A. Selman
2013년 4월 4일
편집: Ahmed A. Selman
2013년 4월 4일
Please do not use ( less than as < or greater than as > ) when using symbolic math. Instead use:
while (gt(m,iter) && (lt(cltr1,0.001)))
gt (greater than) and lt (less than) are used when logical comparison is needed with objects, rather than matrices.
Also your code fails to calculate the x's at the end of the while loop, the condition is not satisfied. So perhaps you need to re-check your input and/or the criterion you've used.
Regards.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!