On Oct 2, 2008, at 09:35 , Janzo wrote:

>
> Hi guys I have a problem with this recursive function, I am getting a
> infinite loop.
>
> The thing is that it is working fine for x(h), x(2*h) but not for any
> upper value.
>
> Any suggestions? Thanks a lot.
>
>
>
> var('h,t')
> h = 0.1
> f = exp(t)*cos(t) - exp(t)*sin(t) #la ecuacion diferencial
>
> def x(n):
>    if n == 0:
>        return 1 #punto conocido
>    else:
>        return (x(n-h) + h*f(n-h)) #funcion de Euler
>
> for i in range(10):
>    print x(i*h)

My guess is that your termination condition isn't going to work: you  
are using a real (ok, "float") value as an argument, and checking to  
see if that value is zero.  The problem is that "zero" for floating  
point is notoriously hard to detect.

"h=0.1" is "inexact", and therefore, so will be "i*h", and so on.   
That's why "n == 0" doesn't work for you.

David's suggestion (h=1/10) does work because the results are "exact".

HTH

Justin


--
Justin C. Walker, Curmudgeon-At-Large
Director
Institute for the Enhancement of the Director's Income
--------
"Weaseling out of things is what separates us from the animals.
  Well, except the weasel."
       - Homer J Simpson
--------



--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to