Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.04 KB | None | 0 0
  1. #|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. HW4 #4
  3. FindMax.rkt by Ryan Liszewski
  4. calculates the maximum of function f within the interval of [x1,x2]
  5. using the trisection method and finds the coordinate of xmax
  6. with accuacy of 6 significant decimal digits
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|#
  8. #lang racket
  9. (define (fmax X Y funct)  
  10.   (let* ((trisect (/ (- Y X) 3.))
  11.          (xtri (+ X trisect))
  12.          (ytri (- Y trisect)))
  13.           ;using abs library function
  14.           ;if absolute value of functX - functY then divide (X + Y)/2
  15.           ;basically keep running until reaching precision of 0.0000001
  16.     (cond [(< (abs (- (funct X) (funct Y))) .0000001)
  17.            (/ (+ Y X) 2.)]
  18.           ;elseif (funct xtri < funct ytri) then findmax of X and ytri
  19.           ;basically run again with a trisected Y value
  20.           ;recursive call
  21.           [(> (funct xtri) (funct ytri))
  22.            (fmax X ytri funct)]
  23.           ;Y must be bigger than xtri
  24.           ;keep Y and run again with trisected x value
  25.           [else (fmax xtri Y funct)])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement