Jump to content

scheme help

- - - - -

  • Please log in to reply
1 reply to this topic

#1
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
I am trying to use Newton's method for cube roots in scheme. Either I cannot get the correct answer or the loop never ends.


(define (square x) (* x x))

(define (close-to-cube x y) (< (abs (- y x)) 0.0001))

(define (improve-cube x y) (/ (+ (/ x (square y)) (* 2 x)) 3))

(define (cube-recur x y ) (if (close-to-cube x y) x (cube-recur (improve-cube x y) y )))

(define (cube-root y) (cube-recur 1.0 y))



Any suggestions? Help? Advice?
Thanks Alot

#2
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Solved it, problem was in cube-root-recur.


; this method is now correct

(define (cube-root-recur xy) (if (close-to-cube x (improve-cube x y)) x 

          (cube-recur (improve-cube x y) y)))


; and in the close to the variables are switched around (-  y  x) should be (-  x  y)


Only problem now it takes alot of recursion to calculate cube root of 225 because I am trying to get close to .0000001 accuracy using newtons method and it only computes 6.0822........after 3 minutes.

Edited by mr mike, 04 February 2011 - 12:46 PM.
added text





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users