coms100mchapter1 - Chapter 1 From Formula to Program 1.1 A...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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, run-time errors, built-in func- tions, sqrt , sin , cos , arctan 1.3 Maxs and Mins 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 non-century year divisible by 4. Sometimes the application of a formula involves a simple substitution. Thus, the surface area of a one-inch 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. Well see that the mere possession of a formula is just the start of the problem-solving process. The real ticket to solution space, if you travel by computer, is the program. And thats 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

Page1 / 27

coms100mchapter1 - Chapter 1 From Formula to Program 1.1 A...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online