Skip to Main Content
Mathematica 錦囊妙計
book

Mathematica 錦囊妙計

by Sal Mangano
April 2014
Beginner to intermediate content levelBeginner to intermediate
830 pages
15h 21m
Chinese
GoTop Information, Inc.
Content preview from Mathematica 錦囊妙計
60
|
第二章
[Page-060]
討論說明
如果你想得到迭代過程的中間值,此時可使用
NestList
指令。假設你想得到幾何形狀的
旋轉過程圖形,可使用
NestList
指令進行旋轉與位移的迭代,以下以正方形加上一頂點
作為初始的幾何圖形,持續旋轉
d
角度直到超過
2Pi
360 度)為止。
NestWhile
NestWhileList
指令分別推廣自
Nest
NestList
指令,它們增加了測試條
件作為判斷迭代繼續運行的依據,除此之外,也可以指定迭代次數的上限。以下範例為
旅行推銷員問題(traveling salesperson problem)的應用,此問題搜尋一個旅行路徑小
於某個指定的距離,旅行路徑應遊歷每個城市並回到起始地點,每個城市以數字 1
n
代表,城市間的距離以一稀疏矩陣表示。
ch02.indd 60 2014/4/2 上午 03:32:31
2.11 迭代指令的應用
|
61
隨機建立一組城市
以下函數回傳城市組中,給定二城市之距離
給定一旅行路徑,回傳總路徑長度,
注意此路徑需經過每一城市並回到起始城市
依序遊歷城市組,作為起始猜測的旅行路徑
隨機重排城市順序作為旅行路徑,直到路徑長度
低於指定數值,或是猜測次數超過 maxTries
061]
此演算法並不聰明,但用來介紹
NestWhile ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ubuntu for Non-Geeks, 4th Edition

Ubuntu for Non-Geeks, 4th Edition

Rickford Grant, Phil Bull

Publisher Resources

ISBN: 9789863470106