You are on page 1of 7

9

REVIEW ON the formula F= C +32. Write a


5
COMPUTER program that reads in a Celsius
temperature and then outputs the
PROGRAMMING equivalent Fahrenheit temperature.
3. (CP) The relationship between the sides
(a,b) of a right triangle and the
Objectives: hypotenuse (h) is given by the
Pythagorean formula a2 + b2 = h2.
 Review the elements of computer Write a program that reads in the lengths
programming with emphasis on good of the two sides of a right triangle and
programming principles. computes the hypotenuse of the triangle.
 Adapt a systematic method of 4. (CP) The area of a triangle whose sides
developing computer programs. are a, b, and c can be computed by the
formula A=√ s (s−a)( s−b)( s−c),
(a+b+ c)
COMPUTER where s=
2
. Write a program

PROGRAMMING that reads in the lengths of the three


sides of a triangle and outputs the area
A computer requires a program to of the triangle.
do any useful work. Programming
Using Conditional Statements
languages are used to write computer
program. 5. (CP) Write a program that gives the user
the choice of computing the area of any
Computer Program is a set of
of the following: a circle, a square, a
instructions in a programming language rectangle, or a triangle. The program
intended to be executed on a computer to should include a loop to allow the user
perform a task. Computer programming to perform as many calculations as
involves much more than simply writing a desired. Use a procedure for each of the
list of instructions. Problem solving is a different kinds of calculations.
crucial component of programming and
requires a good deal of preplanning. Before Using Repetitive Statement
writing a program to solve a particular
6. (CP) Write a program to calculate the
problem, you must consider carefully all
sum: 1+4 + 7 + . . . +100.
aspects of the problem and then develop and
7. Write a program to calculate the sum:
organize its solution.
1 1 1
1+ + + .. .+ , where terms is
2 3 Terms
Computer specified by the user. This sum is called
harmonic series.
Problems - - - - - - 8. (CP) Write a program to list the
numbers from 0 to 25, their squares,
square roots, fourth power, and fourth
1. Write a program that reads in two root. The output should be in a neat five-
integers and then outputs their sum, column format.
difference, and product. 9. (CP) Write a program that reads in a
2. A Celsius (centigrade) temperature number N and then outputs the sum of
(C) can be converted to an equivalent the squares of the numbers from 1 to N.
Fahrenheit temperature (F) according to If the input is 3, for example, the output
should be 14, because 12 + 22 + 32 = 1 Using Arrays
+ 4 + 9 = 14. The program should allow 16. CP) Write a program that reads 10
the user to repeat this calculation as integers into an array, computes the
often as desired. average, largest, and smallest numbers
10. (CP) Write a program to compute the in the array.
factorial of a number (N!). For example, 17. (CP) Write a program that allows the
3! = 1 x 2 x 3 = 6. By convention, 0! is user to type in up to 10 positive numbers
set equal to 1. and then echoes back the numbers typed
11. (CP) A perfect number is a positive in but in reverse order.
integer that is equal to the sum of all 18. (CP) Write a program that reads in an
those positive integers (excluding itself) m by n matrix (m row by n column),
that divide it evenly. The first perfect then reads in an n by p matrix (n row by
number is 6, because its divisors p column), and then computes the
(excluding itself) are 1, 2, 3, and product matrix and displays the two
because 6 = 1 + 2 + 3. Write a program matrices as well as their product matrix
to find the first three perfect numbers on the screen.
(include 6 as one of the three).
12. (CP) Write a program to find all integer
solutions to the equation 4x + 3y – 9z =
5 for values of x, y, and z between 0 and
100.
13. (CP) The value ex can be
approximated by the sum
2 3 n
x x x
1+ x+ + +. ..+ . Write a
2 ! 3! n!
program that takes a value x as input and
outputs this sum for n taken to be each
of the values 1 to 100. The program
should repeat the calculation for new
values of x until the user says he or she
is through.

Using Procedures or Functions for Subtasks


14. (CP) Write a program that reads in a
real number (representing that many
inches) and then outputs the area of a
square with sides of that length and the
area of a circle with a diameter of that
length. Use two procedures to compute
the two areas.
15. Write a procedure that has two formal
parameters, one for the radius of a circle
and one for the circumference. Given a
radius, the procedure computes the
circumference of the circle and stores
the answer in the parameter for the
circumference. Embed this in a program
to compute the circumference of a
circle.
NUMERICAL Numerical methods are techniques
by which mathematical problems are
METHODS formulated so that they can be solved with
arithmetic operations. It provides the
transition from the mathematical model of a
Introduction problem (the equations or functions obtained
in calculus or algebra, etc) to an
The advancement of computer ALGORITHM that we can program to
technology and its availability has affected obtain the solution of the problem in the
education in many aspects. To prepare form of numbers.
students for current and future challenges, an Numerical methods combine two of
introduction to computer-based methods are the most important tools in the engineering
needed. Currently, practical mathematical repertoire: mathematics and computers. In
problems demand the use of computers to fact, numerical methods can be loosely
obtain solutions in a timely manner and with defined as computer math.
acceptable accuracy.
The problem-solving traits and
Computers have increased the modeling background of experts enable
number of alternative solution procedures them to be successful in both technical and
that are available to professionals. Whereas non-technical endeavors. The traditional
the experts of past generations were limited technical activities include research, design,
to graphical, analytical, and simple empirical testing, manufacturing, and education.
methods, experts can now consider
numerical and simulation methods in their For example, engineers depend
design work. heavily on mathematical modeling in
performing their tasks. These models must,
Numerical methods are used to of course, yield physically meaningful
produce quantitative approximations to solutions if they are to be useful. Solutions
solutions of mathematical problems; they may be obtained by standard mathematical
have become increasingly important as techniques if the models are simple enough.
access to computing tools has become Even in such cases, however, the values of
easier. the solutions may require a large amount of
Numerical methods can be used to calculation. Computers are ideal tools for
solve a variety of quantitative problems such tasks because they can perform
faced by many. For many mathematical calculations very quickly and because,
problems, an analytical solution is either unlike people, they are neither subject to
impossible or the cost or effort of tedium nor prone to clerical mistakes. The
performing an analytical solution would be direct methods for solving systems of
prohibitive. Furthermore, in an effort to simultaneous linear equations are examples
achieve greater accuracy in mathematical of numerical methods for calculating
solutions, more complex analyses are being solutions from mathematically well-
used. Very often, the complexity of the established forms.
problem becomes such that an analytical Other types of numerical methods
solution is not possible. In cases where are those aimed at solving problems for
analytical solutions are not practical or the which solutions cannot be obtained by
problem is very complex, numerical ordinary mathematical manipulation. In
methods can often be used to find a solution. these cases, we replace the mathematical
equations to be solved by discrete processes 1. The solution procedure is iterative, with
and try to obtain sufficiently accurate the accuracy of the estimated solution
numerical approximations of the solutions. improving with each iteration.
Many of these methods require iteration of a 2. The solution procedure provides only an
process until convergence to a solution is approximation to the true solution.
achieved. Others, like the methods for 3. An initial estimate of the solution may
be required.
solving ordinary differential equations,
4. The solution procedure is conceptually
require us to proceed in a step-by- step
simple, with algorithms representing the
fashion from an initial condition to a solution procedure that can be easily
specified destination. programmed on a digital computer.
Implementation of numerical 5. The solution procedure may
occasionally diverge from rather than
methods on computers requires a
converge to the true solution.
programming language through which we
can provide instructions to the computer.
One might wonder at this point why
Numerical Methods and
we bother to study numerical methods if Engineering Practice
codes for the common methods already exist
in commercial packages. Some reasons for There are a number of reasons why
doing so are listed below: you should study numerical methods.

 Routines may not be available for the 1. Numerical methods are extremely
language or the type of computer we are powerful problem-solving tools. They
using. are capable of handling large systems of
 An affordable commercial package may equations. As such, they greatly enhance
not contain all of the methods we need. your problem-solving skills.
 Adaptation of methods to fit special 2. Many problems cannot be approached
situations may be impossible to using canned programs. If you are
accomplish with pre-written tools. conversant with numerical methods and
 Programming our own routines helps us are adept at computer programming, you
will have the capability of designing
to understand the steps of a process
more fully; it also allows us to explore a your own programs to solve problems
without having to buy expensive
problem beyond the mere determination
of a solution. Such exploration may software.
3. Numerical methods are an efficient
provide insights that can expand our
view of a particular problem or help us vehicle for learning to use computers. It
is well known that an effective way to
in a different problem.
learn programming is to actually write
 Even if pre-written codes are available,
computer programs.
an understanding of the numerical
4. Numerical methods provide a vehicle
processes allows us to choose the best
for you to reinforce your understanding
method among several for performing a
of mathematics. One function of
task.
numerical methods is to reduce higher
 We become better educated in the tools
mathematics to basic arithmetic
of our profession. operations.

Characteristics of
Numerical Errors
Numerical Methods
An error in estimating or determining a We can deal with a complicated
quantity of interest can be defined as a problem more easily if we can break it down
deviation from its unknown true value. into relatively small, manageable pieces.
Errors can be classified based on their
A code segment of the task that
sources as non-numerical and numerical
must be performed at different stages of the
errors.
problem has to be written only once.
Non-numerical errors include (1)
Modules may be tested
modeling errors, (2) blunders and mistakes,
independently of other parts of the code; in
and (3) uncertainty in information and data.
this way, debugging (or error detection and
Numerical errors include the following: correction) is simplified.
1. Truncation Errors – results from the Well-chosen modules for production
termination of an infinite series codes simplify program maintenance and
expansion after a finite number of terms. upgrading because modification of one
2. Round-off Errors – results when module either leaves other modules
numbers are shortened to fit into unaffected or minimizes any necessary
computer words of limited length. changes in other modules.
3. Mathematical-approximation Errors –
result from using approximate
mathematical expressions for a Mathematical
relationship or solution. For example,
numerical methods sometimes call for Background
the use of a linear model for
representing nonlinear mathematical The following are the types of mathematical
expressions. subject areas that are covered in this course:
4. Error (e) – is the difference between the
computed (xc) and true (xt) values of a 1. Roots of Equations. The solution of
number x. many scientific and engineering
e=x c −x t problems requires finding the roots of
equations that can be nonlinear. These
5. Relative True Error (er) – is defined as problems are concerned with the value
the error e relative to the true value. of a variable or a parameter that satisfies
a single equation.
x c −x t 2. Simultaneous Linear Equations. Many
e=
xt engineering and scientific problems can
be formulated in terms of systems of
simultaneous linear equations. When
Modularity in these systems consist of only a few
equations, a solution can be found
Programming analytically using the standard methods
from algebra, such as substitution.
Modules are code segments for However, complex problems may
performing specific tasks; they are variously involve a large number of equations that
known as procedures, functions, cannot realistically be solved using
subroutines, etc., depending on the analytical methods. In these cases, we
language. Some advantages of modularity in will need to find the solution
programming are as follows. numerically using computers.
3. Numerical Integration. A physical
interpretation of numerical integration is
the determination of a close
approximation to the area under a curve domains. The methods that will be
of a function that has been determined considered are finite-difference methods
either from experimental data or from a in which derivatives of a function are
mathematical expression. Numerical replaced by finite-difference equations.
integration formulas play an important 6. Curve Fitting. In engineering, curve
role in the solution of differential fitting plays an important role in the
equations. analysis and interpretation of
4. Ordinary Differential Equations. experimental data and in the correlation
ODE’s are of great significance in of mathematical models formulated
engineering practice. This is because from fundamental engineering
many physical laws are expressed in principles. It involves the determination
terms of the rate of change of a quantity of a continuous function which results in
rather than the magnitude of the quantity the most “reasonable” or “best” fit of
itself. Two types of problems are experimentally measured values of
addresses: initial-value and boundary- (x1,y1) through (xn,yn). The least-
value problems. squares approximation is one form of
5. Partial Differential Equations. PDE’s curve fitting in which we seek the
are used to characterize engineering parameters of a given model that best fit
systems where the behavior of a the data
physical quantity is expressed in terms
of its rate of change with respect to two
or more independent variables. The
course is intended to provide an
introduction to methods of PDE and will
focus on the classical problems of
parabolic, elliptic, and hyperbolic
equations with relatively simple

You might also like