Fixed pt iteration

How to find 20th iteration for following fixed point problem.

x = exp(-x/2)

Please help.

gkokovidis's picture

Fixed pt iteration

>restart:

>ans:=[seq( exp(-x/2), x=1..20 )];

>evalf(%);

>evalf(ans[20]);

Later Edit:  After seeing Joe Riel's responsefor maple10 code, I realized that all of the questions were the same.  I misunderstood the original question, thus the posting above which only generates a sequence, not iterating the value back.

Regards,
Georgios Kokovidis
Dräger Medical


which is more accurate ? better?

 

my answer

> restart:
> n:=1:
> x[n]:=exp(1)^(-1/2):
> while n<21 do
> x[n+1]:=exp(1)^(-x[n]/2):
> n:=n+1:
> od:
> evalf(%%);

 

OR(based on Joe Riel's reply on http://www.mapleprimes.com/forum/maple10code )

 

i changed > x := 2. : # try with a rational! to > x := 1: # try with a rational!

> x := 1: # try with a rational!
> to 20 do
>     x := exp(-x/2);
> end do:
> evalf(x);

Variations

We can use foldl here to do this in one line

foldl(x->exp(-x/2),1.$20);
                                 0.7034674218

More amusingly, as

foldl(exp@curry(`*`,-1/2),1.$20);
                                 0.7034674218
or even

foldl(1/sqrt@exp@(()->args[1]),1.$20);

Here's a ridiculous method:

 `@`(seq('`/`,sqrt,exp',i=1..20))(1.);
                                 0.7034674229

The slightly different answer is because the previous versions, uses foldl, actually do only 19 iterations.

wow now we have 3 different answer

your code is amazing, something i have never seen before

i dont even know there is such code as foldl...

coool

 

however, which way is more likely to get the correct answer?

 

thanks

different solutions, same answer (within numerical accuracy)

Here's another variation, which is more clearly done in a few steps, though of course it can be done in one:

iter := `/`@sqrt@exp:
iter20 := iter@@20:
iter20(1.); 
                            0.7034674229

Using the original form, this can be done as

iter := x -> exp(-x/2):
iter20 := iter@@20:
iter20(1.);
                            0.7034674227

I expect (and have verified for x=1.) that the iterator exp(-x/2) is "more accurate" than 1/sqrt(exp(x)).

While foldl is quite useful in its own right, it really isn't warranted here (it composes a binary function rather than a unary function).  Note that the function `/` is n-ary; here it is being used as a unary function (inversion).

the code gives back the

the code gives back the value of 20th iteration. Can we somehow change it so that it gives all  iteration values along the way.?

can we use  'for' statement of 'if' loops.

use a table

An efficient way to do that is to store the intermediate results in a table, and then convert the table to a list:

T := table():
T[0] := 1.:
for i to 20 do
    T[i] := exp(-T[i-1]/2);
end do:
convert(T, 'list');

thanks all of ya.  

thanks all of ya.

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}