SICP Goodness - Stream(11)

栏目: IT技术 · 发布时间: 4年前

内容简介:Do you think Computer ScienceAre you feeling that you are doingAre you feeling a bit

Do you think Computer Science equals building websites and mobile apps?

Are you feeling that you are doing repetitive and not so intelligent work?

Are you feeling a bit sick about reading manuals and copy-pasting code and keep poking around until it works all day long ?

Do you want to understand the soul of Computer Science ?

If yes, read SICP!!!

In last article we talked about the integral procedure. In this article, we talk about how to use it to solve differential equations.

First let’s make sure we understand the math. Actually we do not need to open your 1000 page calculus textbook and start reading from page 1. To understand the example in the book we simply need to know how to solve that one differential equations:

y' = y

Let me put this in English: we are trying to find a function y, which the derivative of y is equal to y itself.

We do not even need to know how to solve this ourselves, there are many online tools to help us. So the result of this is

y = e^(x + C)

This is all the math we need to know to continue.

Next we will explain how to solve y' = y using the integral procedure.

There is a Henderson Figure given in the book:

SICP Goodness - Stream(11)

To understand this figure, we can divide it into the upper part and the lower part.

The upper part says is just the integral procedure. It takes in the stream of derivative of y and output the stream of y itself.

The lower part describes the equation y = y' by using the stream map function f(t) = t .

Now let’s try out the solve procedure from the book. y0 = 1 , dt = 0.001 .

(define (integral delayed-integrand initial-value dt)
  (define int
    (cons-stream initial-value
                 (let ((integrand (force delayed-integrand)))
                   (add-stream (scale-stream integrand dt)
                               int))))
  int)

(define (solve f y0 dt)
  (define y (integral (delay dy) y0 dt))
  (define dy (stream-map f y))
  y)


(stream-ref (solve (lambda (y) y) 1 0.001) 1000)

;Value: 2.716923932235896

Acually, by following the Henderson figure, we can figure out what the y stream looks like by hand.

1

1 + dt

1 + dt + (1 + dt)dt

1 + dt + (1 + dt)dt + (1 + dt + (1 + dt)dt)dt

... ...

So we can simply write solve in the following form:

(define (solve f y0 dt n)
  (define (s f y0 dt n r)
    (if (= n 0)
        r
        (s f y0 dt (- n 1) (+ r (* (f r) dt)))))
  (s f y0 dt n y0))
  
(solve (lambda (x) x) 1 0.001 1000)
;Value: 2.716923932235896

We can see that the result is the same.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

写给大忙人看的Java SE 8

写给大忙人看的Java SE 8

【美】Cay S. Horstmann(凯.S.霍斯曼) 编 / 张若飞 / 电子工业出版社 / 2014-11 / 59.00元

《写给大忙人看的Java SE 8》向Java开发人员言简意赅地介绍了Java 8 的许多新特性(以及Java 7 中许多未被关注的特性),《写给大忙人看的Java SE 8》延续了《快学Scala》“不废话”的风格。 《写给大忙人看的Java SE 8》共分为9章。第1章讲述了lambda表达式的全部语法;第2章给出了流的完整概述;第3章给出了使用lambda表达式设计库的有效技巧;第4章......一起来看看 《写给大忙人看的Java SE 8》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器