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

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

View Full Document Right Arrow Icon
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 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 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
Image of page 1

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

View Full Document Right Arrow Icon
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 problem-solving 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, science, and mathematics.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern