質問

Can someone help me write the body of a method for Simpson's rule in scheme? The following is what I have so far, but I don't know whether I should solve this problem recursively or using a different method.

(define (sum-i term a b)
 (if (= a b)
    (term b)  
     (+ (term a) (sum-i term (+ a 1) b))))

(sum-i (lambda (x) x) 0 1)
(sum-i (lambda (x) (* x x)) 0 1)
(sum-i (lambda (x) (* x x x x)) 0 1)

(define (simpsons-rule f a b n)
   (let  ((h (/ (- b a) n)))
   (define (y_k k) (f (+ a (* k h))))
役に立ちましたか?

解決

Your close I'll give you the cases in the function you give and an argument too sum, and how to setup the sum call.

You can set up up recursively, with a a recursive function defined instead of f and calling it where sum it.

(define (simpsons-rule f a b n)
   (define h   (/ (- b a) n))
   (define Y_k (lambda (k) (f (+ a (* k h)))))
   (define (f x)
     (cond ((or (= x 0) (= x n)) ...)
           ((even? x) ...)
           (else ...)))
   (* (/ h 3)
      (sum f 0 plus1 n))) 

他のヒント

#NOW I write a function which calculate a integral value for simpons1/3rule: #I want to integrate this (x^3-7x^2+10) from 0 to 100 say

  ftn<-function(x){ 
  return((x^3-7*x^2+10))
 }
  simpons<-function(a,b,ftn,n){
  h<-(b-a)/n
   fa<-ftn(a)
   fb<-ftn(b)
  sum1<-0
  i<-1
  while(i<=n-1){
   sum1<-sum1+ftn(a+i*h)
 i<-i+2
    }
    j<-2
   sum2<-0
   while(j<=n-2){  
   sum2<-sum2+ftn(a+j*h)
   j<-j+2
   }
  s1<-h/3
  simp<-s1*(fa+fb+4*sum1+2*sum2)
   return(simp)
   }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top