zoukankan      html  css  js  c++  java
  • SICP:1.29辛普森公式求积分

    使用simpson公式求积分

    #lang racket
    
    ;this function is use recursive
    (define (sum term a next b)
      (if (> a b) 
          0
          (+ (term a)
         (sum term (next a) next b)
         );+
       );if
      );sum
    
    ;this function is use Iteration
    (define (sum-iteration term a next b)
      (define (iter a result)
        (if (> a b)
        result
        (iter (next a)
              (+ result 
             (term a )))
         );if
        )
      (iter a 0)
      );sum-iteration
    
    (define (even? n)
      (= (remainder n 2) 0)
      );even?
    
    (define (cube n)
      (* n n n)
      );cube
    
    (define (simpson-cube-integral f a b n);a:begin num b:end num n:count 
      (define h (/ (- b a) n));h
      (define (next c)
        (+ c 1));next
      (define (multiplier c)
        (cond ((or (= c 0) (= c n)) 1)
          ((even? c) 2)
          (else 4)
          );cond
        );multiplier
      (define (term c)
        (* (multiplier c)
           (f (+ a
             (* h c)));f
           );*
        );term
      
      (* (/ h 3)
       (sum-iteration term (exact->inexact 0) next n) )
    
      );simpson-cube-integral
    
    (simpson-cube-integral cube 0 1 100)
    (simpson-cube-integral cube 0 1 1000)

  • 相关阅读:
    rust 宏
    umi 调试
    rust 编译器工作流
    rust 神奇的特质
    rust 生命周期2
    rust 函数-生命周期
    rust 九九乘法表
    rust 代码生成选项
    rust 编码模式
    恢复到版本并销毁之后的git提交记录
  • 原文地址:https://www.cnblogs.com/wizzhangquan/p/4370225.html
Copyright © 2011-2022 走看看