Loops 2/2 迴圈 Chap 4 Loops and Character Manipulation 迴圈和字元操控 Fortran 95/2003 for scientists and Engineers by Stephen J. Chapman Prepared by Walter Chen, Dept. of Civil Engineering, NTUT For classroom teaching purpose
Incomplete loops & nested loops
CYCLE statement If the cycle statement is executed in the body of a DO loop, the execution of the current iteration of the loop stops and control returns to the top of the loop 在 DO 迴 圈中碰到 CYCLE 指令時 回到迴圈的開端 程式碼 結果
EXIT statement If the EXIT statement is executed in the body of a loop, the execution of the loop stops and control is transferred to the first executable statement after the loop 在迴圈中碰 到 EXIT 指令時 跳出迴圈到迴圈後的第一行 結果 程式碼
CYCLE 流程圖 EXIT 流程圖
Named loops 取名的迴圈
Named loops Name may be up to 31 alphanumeric characters long, beginning with a letter 最多 31 個字元 以字母開 頭 Must be unique 必須是獨特的 If a name is assigned to a loop, then the same name must appear on the associated END DO 如果迴圈有 名字 那麼 END DO 也要有名字 Names are optional on any CYCLE and EXIT statements 但是 CYCLE 和 EXIT 的名字可有可無
Nesting loops 巢狀迴圈 程式碼 結果
Incorrectly nested loops Wrong! 錯誤 迴圈交叉了
CYCLE in nested loops 程式碼 結果 If a CYCLE or EXIT statement appears inside an unnamed set of nested loops, then the CYCLE or EXIT statement refers to the innermost of the loops containing it 如果迴圈沒有取名的話 CYCLE 和 EXIT 的對象是最裡面的迴圈
CYCLE outer loop 結果 程式碼 It is possible to make the CYCLE and EXIT statement refer to the outer loop by specifying a loop name in the statement 如果迴 圈有名字的話 也可以指定 CYCLE 和 EXIT 的對象是外層的 迴圈
Determine how many times each loop is executed 決定每個迴圈執行多少次 4 0 1 7 9 0
Determine the value in ires at the end of each loop 決定迴圈 終了時 ires 的值 10 55 10 100 21
Whether or not valid 是否有效 Invalid: these statements redefine DO loop index i within the loop Valid Illegal: DO loops overlap
Example 4-7 the flight of a ball 球 的軌跡 球丟出後在任 一時間的高度 球的軌跡是拋物線 球丟出後在任 一時間的水平 距離 球的初速的分量
Problem 問題分析 Assume that the ball is initially thrown from position (0, 0) with an initial velocity of 20 m/sec 假定球在座標原點 初 速是 20 m/sec Design, write, and test a program to determine the horizontal distance traveled by the ball from the time it is thrown until it touches the ground again 決定球在落地前 飛行的距離 The program should do this for all angles from 0 to 90 degrees in 1 degree steps 針對 0 到 90 度的每一度加以計 算 Determine the angle that maximizes the range of the ball 求 出那個角度的飛行距離最遠
先解出落地時間 再把時間代入 求水平距離的表示式
Solution State the problem 定義問題 Define the inputs and outputs 決定輸入和輸出 No inputs are required Outputs: a table showing the range of the ball for each angle and the angle for which the range is maximum Design the algorithm 導出演算法 Turn the algorithm into Fortran statements 轉變為程 式碼 Test the program 測試程式
流程圖
程式碼
Test run
4-25 Geometric mean 幾何平均值 geometric _ mean = n x1 x2 x3...xn Write a Fortran program to read in an arbitrary number of positive values and then calculate the arithmetic mean and the geometric mean of those values. Please use a while loop when inputting the data, and stop the loop when a negative number is encountered. 寫一個 Fortran 程式來讀取任意個正的輸入值 然後計算 1 算數平均值及 2 幾何平均值 請使用 WHILE loop 來接受輸 入值 並且在使用者輸入負值時中止 測試值 10, 5, 4, 5 Test values: 10, 5, 4, 5
4-27 Harmonic mean 調諧平均值 harmonic _ mean = N 1 1 1 + +... + x1 x2 xn Write a Fortran program to read in an arbitrary number of positive values and then calculate the harmonic mean of those values. Any method to take user input is allowed. 寫一個 Fortran 程式來讀取任意個正的輸入值 然後計算調諧 平均值 允許使用任何方式來接受輸入值 測試值 10, 5, 2, 5 Test values: 10, 5, 2, 5
Exercise 4-24 infinite series 無窮級 數 Original infinite series for sin x 原始的 無窮級數 Truncated infinite series for sin x 有限 項的級數 Write a Fortran program that reads in a value for x in radians and calculates the sine of x using the intrinsic function 以內建函數計算 sin x Calculate the sine of x using truncated series with N = 1, 2, 3,, 10 以 有限項級數和不同的 N 計算 sin x Compare the true value of sin x with the values calculated 比較結果 How many terms are required to calculate sin x to the full accuracy of the computer? 需要多少項才能達到電腦的精確度
測試值是 3 wchen@retro:~$./aaaaaaaaa.out Please enter x: 3 sine intrinsic = 0.1411200 N = 1 sin(x) = 3.000000 N = 2 sin(x) = -1.500000 N = 3 sin(x) = 0.5250000 N = 4 sin(x) = 9.1071405E-02 N = 5 sin(x) = 0.1453125 N = 6 sin(x) = 0.1408746 N = 7 sin(x) = 0.1411306 N = 8 sin(x) = 0.1411196 N = 9 sin(x) = 0.1411200 N = 10 sin(x) = 0.1411200 wchen@retro:~$ 舊的 32 位元電腦
誤差值小於 1e-17 才停下來 新的 64 位元電腦 請將 real 改宣告為 double precision
指數函數 e 的泰勒展開式 x Taylor Series Expansion of Exponential Function 2 3 x x e =1+ x+ + +... 2! 3! x
Exercise 4-18 binary to decimal conversion 二進位到十進位轉換 Write a program that prompts a user for a binary number, and then convert the input binary number into a decimal number 寫一個程式 將使用者輸入的二進位字串轉換為十進位數字 The program should handle numbers from 0000000000 to 1111111111 處理數字的範圍 The program should also test for and handle an invalid value among the input characters 程式也要能測試並處理無效的字元 Test the program with these numbers: 以這些數字加以測試 0010010010 1111111111 10000000001 01111111110
Exercise 4-13 1. 請寫一個 Fortran 程式計算右式的值 2. 其中 ln 是自然對數 以 e 為底 請用 一個 While 迴圈讓程式不斷重複 直到 出現無效值為止 當無效值出現時 請 印出相關訊息並終止程式 3. 注意 ln(x) 中 x 必須大於 0 1 y ( x) = ln 1 x
Ex 4-21 Tension on a Cable 張力 T= W lc lp d lp 2 d 2 寫一個程式找出使張力最小的 d 間距 0.1 m T = tension on the cable W = weight of the object lc = length of the cable lp = length of the pole d = distance along the pole at which the cable is attached d = 0.5 m to 2.8 m
4-30 Ideal Gas Law 理想氣體定律 理想氣體有三項特徵參數 絕對壓力 P 體積 V 和絕對溫度 T 而且它們滿足以下的關係式 PV = nrt 其中 P 的單位是 kpa V 的單位是 L 公升 n 是氣體的分 子數目 單位 莫耳 mol R 是常數 8.314 L*kPa/mol*K 而 T 的單位是 K 註 1 mol = 6.02*10^23 個分子 假設現有 1 莫耳的氣體 溫度是 273 K (a) 請寫一個程式計算當壓力從 1 kpa 增加到 1001 kpa 時 該 氣體的體積會是多少 間隔 100 kpa (b) 假設氣體的溫度升為 373 K 請將 (a) 重算一次
4-32 槓桿施力 槓桿施力與所能舉起的物體的重量關係如下 如果 Weight = 600 kg, d2 = 1 m 請計算當 d1 = 0.5-3.0 m 時 0.1 m 間隔 施力 F 應各為多少 如果施力最 大為 400 kg 可以舉起 600 kg 重物的最短距離 d1 是多 少