Chap 1 Intro to computers and the Fortran language

Similar documents
Microsoft PowerPoint - STU_EC_Ch02.ppt

SHIMPO_表1-表4

SHIMPO_表1-表4

C/C++ - 字符输入输出和字符确认

Computer Architecture

C/C++ - 函数

穨control.PDF

PowerPoint Presentation

2015 Chinese FL Written examination

1.ai

Microsoft Word - template.doc

2017 CCAFL Chinese in Context

C/C++ 语言 - 循环

spss.doc

Introduction to Hamilton-Jacobi Equations and Periodic Homogenization

K301Q-D VRT中英文说明书141009

coverage2.ppt

科学计算的语言-FORTRAN95

Microsoft PowerPoint - Lecture7II.ppt

2009 Japanese First Language Written examination

summerCampBookP1~16.pdf

2010 Japanese First Language Written examination

Microsoft Word - Final Exam Review Packet.docx

ENGG1410-F Tutorial 6

2009 Korean First Language Written examination

untitled

untitled

2015年4月11日雅思阅读预测机经(新东方版)

States and capital package

OA-253_H1~H4_OL.ai

PowerPoint Presentation

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

Microsoft Word - TIP006SCH Uni-edit Writing Tip - Presentperfecttenseandpasttenseinyourintroduction readytopublish

C

epub 94-3

入學考試網上報名指南

CHAPTER VC#

01.dvi

C/C++程序设计 - 字符串与格式化输入/输出

投影片 1

2/80 2

92 (When) (Where) (What) (Productivity) (Efficiency) () (2) (3) (4) (5) (6) (7) em-plant( SiMPLE++) Scheduling When Where Productivity Efficiency [5]

Go构建日请求千亿微服务最佳实践的副本

Microsoft PowerPoint - STU_EC_Ch08.ppt

教学大纲

A dissertation for Master s degree Metro Indoor Coverage Systems Analysis And Design Author s Name: Sheng Hailiang speciality: Supervisor:Prof.Li Hui,

WWW PHP

untitled

AN INTRODUCTION TO PHYSICAL COMPUTING USING ARDUINO, GRASSHOPPER, AND FIREFLY (CHINESE EDITION ) INTERACTIVE PROTOTYPING

中国科学技术大学学位论文模板示例文档

QQGQ2.E Power Supplies, Information Technology Equipment Including Ele... 1/10

<4D F736F F F696E74202D20B5DAD2BBD5C228B4F2D3A1B0E6292E BBCE6C8DDC4A3CABD5D>


Knowledge and its Place in Nature by Hilary Kornblith

TX-NR3030_BAS_Cs_ indd

Chapter 24 DC Battery Sizing

IBM 全 球 企 业 咨 询 服 务 部 中 国 五 矿 筑 起 人 力 资 源 信 息 大 厦 2 回 顾 篇 慎 选 巧 选 软 件 平 台 由 于 五 矿 集 团 下 属 的 很 多 公 司 是 最 近 几 年 才 加 盟 的 新 成 员 企 业, 这 些 公 司 所 应 用 的 人 力 资

甄試報告1125.PDF

(baking powder) 1 ( ) ( ) 1 10g g (two level design, D-optimal) 32 1/2 fraction Two Level Fractional Factorial Design D-Optimal D

旅 句 良 年 理 了 來 不 不 更 更 說 識 更 樓 歷 練 靈 旅 論 不 了 契 諒 老 老 老 不 勵 老 不 良 論 漏 不 老 老 不 勵 不 了 了 老 論 利 行 老 見 不 見 更 老 玲 歷 老 料 理

C/C++语言 - 运算符、表达式和语句

Microsoft Word - C-pgm-ws2010.doc

Microsoft PowerPoint - CH 04 Techniques of Circuit Analysis

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

HCD0174_2008

Microsoft PowerPoint - C_Structure.ppt

(Pattern Recognition) 1 1. CCD

【摘要】

Microsoft Word - 3D手册2.doc

Microsoft Word - 09.數學 docx

SuperMap 系列产品介绍

Untitled-3

高中英文科教師甄試心得

OncidiumGower Ramsey ) 2 1(CK1) 2(CK2) 1(T1) 2(T2) ( ) CK1 43 (A 44.2 ) CK2 66 (A 48.5 ) T1 40 (

Preface This guide is intended to standardize the use of the WeChat brand and ensure the brand's integrity and consistency. The guide applies to all d

Chn 116 Neh.d.01.nis

Guide to Install SATA Hard Disks

Microsoft PowerPoint - STU_EC_Ch04.ppt

第二十四屆全國學術研討會論文中文格式摘要

Rotary Switch Catalogue

1 * 1 *

HC70245_2008

PCPDbooklet_high-res.pdf

Microsoft PowerPoint - Eisenstein_ABET_Presentation_Beijing_Oct_2007-Chinese.ppt [兼容模式]

, 2,,,,,,, 3,,,,, 4,,,, 5,,, 6,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 2,,,,[ ] :, : (9),1996,18 3 [] :,1995,474 4 [] :,1995, ,,21 6 : (),1987,

Microsoft Word - A doc

JOURNAL OF EARTHQUAKE ENGINEERING AND ENGINEERING VIBRATION Vol. 31 No. 5 Oct /35 TU3521 P315.

WWW PHP Comments Literals Identifiers Keywords Variables Constants Data Types Operators & Expressions 2

IC L05 Visit friends

Microsoft Word - 105碩博甄簡章.doc

GH1220 Hall Switch

C/C++ - 文件IO

V6800/V6600 3D

ch_code_infoaccess

hks298cover&back

C/C++语言 - 分支结构

pdf

<4D F736F F D20C4A3B0E632A3A8D3EFD1D4CEC4D7D6BCECB2E9B8C4A3A92E646F63>

安 全 指 南 : 必 须 遵 守 所 有 的 警 告 事 项, 以 确 保 自 己 和 他 人 的 安 全 以 及 保 护 产 品 和 连 接 装 置 这 些 警 告 事 项 都 按 警 示 程 度 明 示 出 等 级 有 资 格 的 人 员 : YO-YO 只 能 进 行 与 手 册 有 关 的

27 :OPC 45 [4] (Automation Interface Standard), (Costom Interface Standard), OPC 2,,, VB Delphi OPC, OPC C++, OPC OPC OPC, [1] 1 OPC 1.1 OPC OPC(OLE f

Transcription:

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 是多 少