Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

strange behavior in a while loop

8 views
Skip to first unread message

pluton

unread,
Feb 8, 2012, 3:02:35 AM2/8/12
to
Hi,

I have the following code for a fixed point iteration in two dimensions:

Restart; with(linalg);
Gt:=matrix(2,1,[2-2*x2,1/2*sqrt((2-x1)*(2+x1))]);
#Maple command to get a vector as a function of x1 and x2;
G:=array@op@unapply([op](eval(Gt)),x1,x2):
# initial quantities
X0:=matrix(2,1,[0.5,0.5]);epsilon:=1e-12;Nor:=2;i:=1;
# loop
while Nor > epsilon do
X1:=G(X0[1,1],X0[2,1]);
Y:=evalm(X1-X0);
N:=evalm(transpose(Y)&*(Y));
Nor:=evalf(sqrt(N[1,1]));
X0:=X1;
i:=i+1;
end do:

If you run this, you'll see that from the second iteration, X0 and X1 are identical and I do not know why? It means that when X1:=G(X0[1,1],X0[2,1]); is performed (in loop 2) X0 gets the value of X1 right away and Y is then 0 and the procedure stops while it should not. I think there is some kind of a tricky thing happening inside Maple (13) but I am not sure what. I'd be happy to have your insight. Thanks


acer

unread,
Feb 8, 2012, 3:37:01 AM2/8/12
to
Try replacing the line inside the loop which updates X0 to instead be,

X0:=evalm(X1);

You probably want that, for X0 to be a copy of X1 and thereafter be distinct from X1. That is, you want X1 to not be identical to X1 even after any subsequent change in the value of X1.

The motivation is that the code assigns X1-X0 to Y. And that would always produce scalar 0 after the unfortunate X0:=X1, no matter how X1 changed.

acer


pluton

unread,
Feb 8, 2012, 2:58:10 PM2/8/12
to
Thank you for your answer. I found a way to solve this problem by avoiding the use of the linalg bank and by using LinearAlgebra instead. I'll try your suggestion very soon
0 new messages