This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Chapter 1 From Formula to Program § 1.1 A Small Example Program structure, comments, variables, names, types, input , printing and for matting messages with fprintf , the assignment statement. § 1.2 Sines and Cosines Fragments, assertions, overwriting, syntactic errors, runtime errors, builtin func tions, sqrt , sin , cos , arctan § 1.3 Max’s and Min’s if else , boolean expressions, relational operators, compound statements, top down development of nested if s, logical operators § 1.4 Quotients and Remainders mod , if elseif else , fix , round We grow up in mathematics thinking that “the formula” is a ticket to solution space. Want the surface area of a sphere? Use A = 4 πr 2 . Have the cosine of some angle θ ∈ [0 , π/ 2] and want cos( θ/ 2)? Use cos( θ/ 2) = r 1 + cos( θ ) 2 . Want the minimum value μ of the quadratic function q ( x ) = x 2 + bx + c on the interval [ L, R ]? Use μ = q ( b/ 2) if L ≤  b/ 2 ≤ R min { q ( L ) , q ( R ) } otherwise . Want to know if year y is a leap year? Use the rule that y is a leap year if it is a century year divisible by 400 or a noncentury year divisible by 4. Sometimes the application of a formula involves a simple substitution. Thus, the surface area of a oneinch ball bearing is 4 π (1 / 2) 2 = π square inches. Sometimes we have to check things before choosing the correct “option” within a formula. Since 0 ≤ 3 ≤ 10, the minimum value of 1 2 Chapter 1. From Formula to Program q ( x ) = x 2 6 x +5 on the interval [0,10] is q (3) = 4. Sometimes we must clarify the assumptions upon which the formula rests. Thus, if a century year means something like 1400, 1500, and 2000, then 1900 was not a leap year. Writing programs that use simple formulas like the above are a good way to begin our in troduction to computational science. We’ll see that the mere possession of a formula is just the start of the problemsolving process. The real ticket to solution space, if you travel by computer, is the program. And that’s what we have to learn to write. 1.1 A Small Example A program is a sequence of instructions that can be carried out by a computer. Let us write a program that solicits the radius of a sphere and then computes and prints its surface area. The surface area of a sphere that has radius r is given by A ( r ) = 4 πr 2 . There would be little more to say if we were to solve this problem “by hand.” Indeed, we would (1) get r , (2) plug into the formula, and (3) write down the result. However, if the computer is to do the same thing, then each of these steps becomes more involved: % Example 1 1: Compute the surface area of a sphere % A: surface area of the sphere % r: radius of the sphere r= input(’Enter the radius: ’); A= 4*3.14159*r*r; fprintf(’Surface area is %7.2f.\n’, A); This program, like all the others in this text, is written in a programming language called Matlab . Matlab is a programming language and tool that is widely used in engineering,...
View
Full Document
 Spring '07
 FAN/CHEW
 matlab, Expression

Click to edit the document details