Jump to content
  • Advertisement
Sign in to follow this  
OMGitsHeather

Scheme programming- newton's method

This topic is 3615 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi My name is Heather. I am trying to write a program in Scheme and the way we have to do it in my class is completely different from most scheme programs I have seen. The first part I am writing is call ev->pd which is a function that takes one input, which may be any exact value, and outputs the precision decimal representation of it. For example, (ev->pd -98/9) --> (-10 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8) this will use a function called PDPREC. This is what I have so far. (define PDPREC '20) (define ev->pd (lambda (v) (cond [(>= v 0) (ev->pd-help (- v (sub1 v)) (cons (sub1 v) '()) PDPREC)] [else (ev->pd-help (- (* v -1) (cons (* v -1))) (cons (* -1 (cons (* v -1))) '()) PDPREC)]))) (define ev->pd-help (lambda (frac dlist prec) (cond [(= prec 0) (rest dlist)] [else (ev->pd-help (prec (* 10 frac) (newfloor (* frac 10))) (rest (newfloor (* frac 10)) dlist) (sub1 prec))]))) The error I am getting is procedure application: expected procedure, given: 20; arguments were: 10 10 The other part I am working on is to Define NMTRIES to be 10. This variable will be the number of jumps we allow ourselves in any Newton's Method attempt before giving up. Recall that in Scheme, functions can accept other functions as inputs. We take advantage of this feature here. (15 pts.) 5. Write nm, a function that takes three inputs (a real-valued function f, its derivative fdash, and a starting guess x0 in exact value form), and outputs either - a zero of f in precision decimal form, - a doubleton whose first component is fdash-is-zero-at and whose second component is the current test point in exact value form, or - a doubleton whose first component is nmtries-done-guess-is and whose second component is the current test point in exact value form, when the method has already jumped NMTRIES times. (nm (lambda (x) (+ (* x x) (* -4 x) -21)) (lambda (x) (+ (* 2 x) -4)) -20) --> (-3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) (nm (lambda (x) (+ (* x x) (* -4 x) -21)) (lambda (x) (+ (* 2 x) -4)) 20000000001/10000000000) --> (numtries-done-guess-is 555/297) (nm (lambda (x) (+ (* x x) (* -4 x) -21)) (lambda (x) (+ (* 2 x) -4)) 2) --> (fdash-is-zero-at 2) What I have so far is (define NMTRIES '10) (define NM (lambda (f fdash x0) (NM-help f fdash x0 NMTRIES))) (define NM-help (lambda (f fdash x0 triesleft) (cond [(almost= (fdash x0) 0) (cons 'fdash-is-zero-at (cons x0 '()))] [(almost= (f x0) 0) (sub1 x0)] [(= 0 x0) (cons 'nmtries-done-x0-is (cons x0 '()))] [else (NM-help f fdash (- x0 (/ (f x0) (fdash x0))) (sub1 triesleft))]))) (define almost= (lambda (ev1 ev2) (equal? (add1 ev1) (sub1 ev2)))) But it keeps timing out. any is help is greatly appreciated! Thanks!

Share this post


Link to post
Share on other sites
Advertisement
We don't do homework here.

But as a first step, your almost= looks suspect to me. This is supposed to check if two floating-point values are close to each other, yes? what are add1 and sub1? How are you expecting this to work? As for the first part, your description of the problem says "this will use a function called PDPREC", but then you define PDPREC to be the integer 20; which is it?

Share this post


Link to post
Share on other sites
What school do you go to that still requires you to take Scheme?
Just wondering since even MIT doesn't anymore and I'm assuming you aren't learning it just for fun as I am.

Share this post


Link to post
Share on other sites
Quote:
Original post by daviangel
What school do you go to that still requires you to take Scheme?
Just wondering since even MIT doesn't anymore and I'm assuming you aren't learning it just for fun as I am.


I don't know about "requires", but MIT's 6.001 course is Scheme. That's the second course they have in their comp sci/ electrical engineering

(6.00 teaches Python, if you wanted to know.) source

Share this post


Link to post
Share on other sites
Quote:
Original post by daviangel
What school do you go to that still requires you to take Scheme?
Just wondering since even MIT doesn't anymore and I'm assuming you aren't learning it just for fun as I am.


Many schools still use Scheme, including MIT. The most up-to-date list I could find is here.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezbez
Quote:
Original post by daviangel
What school do you go to that still requires you to take Scheme?
Just wondering since even MIT doesn't anymore and I'm assuming you aren't learning it just for fun as I am.


I don't know about "requires", but MIT's 6.001 course is Scheme. That's the second course they have in their comp sci/ electrical engineering

(6.00 teaches Python, if you wanted to know.) source

I guess I was thinking Berkeley Computer Science and Engineering majors anyways since I was looking at the following:
Advice regarding courses in computer science:
The Computer Science department at Berkeley recognizes the fact that courses
comparable to Berkeley's computer science 61A-61B-61C series are difficult to
find at most community colleges. With few exceptions, transfer students must
take CS 61A and CS 61C during their first semester/s at Berkeley.

To be competitive for admission purposes, the department advises prospective
transfer students to take UC-transferable courses in:
1) data structures--even if not officially comparable to Berkeley's CS 61B; and
2) Java (preferred) or C++.

The entire Computer Science 61 series is also offered during the Berkeley
summer session. The department recommends that, when possible, students take one
of these courses during the summer sessions prior to transfer.

p.s. Anyways to respond to the OP while we don't do people's homework, which is how your original question sounded, even looks like you copied homework question verbatim, that doesn't mean we can't answer other reasonable questions about Scheme and things about it you are finding difficult;)
For example a while back I posted my C++ solution for finding the value of PI and got valuable suggestions on how to write it more elegantly since my original was quite clumsy,etc.

Share this post


Link to post
Share on other sites
Its ok. I ended up getting those to work now! :D Now I'm on the last part and its a biiiiiiiiiiiitch, but its ok. Anything is better than a 0 at this point!! :D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!