You are on page 1of 28

X

BACHELOR OF SCIENCE IN CIVIL ENGINEERING

MATH 5 – NUMERICAL SOLTUIONS TO CE PROBLEMS

Engr. Gladys V. Conge

College of Engineering
1 | Numerical Solutions to CE Problems 2

PREFACE

The main purpose of this module is to provide the student with a clear and detailed
presentation of the theory and application of engineering mechanics.

To achieve this objective despite of this pandemic due to COVID-19, this work has
been shaped by the comments and suggestions of the peer reviewer in the teaching
profession, as well as the other faculty members who will ensure quality of the
modules that will be distributed to the LGU.

The course Numerical Methods deals with the study of direct and iterative
numerical methods in engineering, determination of error bounds in calculations,
computation of series expansions, roots of algebraic and transcendental equations,
numerical differentiation and integration, solution to simultaneous linear and non-
linear equations, function approximation and interpolation, differential equations,
optimization, and their applications.

In this course, we will be using a particular software that could help us solve
different types of engineering problems from easy to a difficult one. The course
provides background on numerical analysis needed to solve civil engineering
problems numerically when their analytical solution is either not available or
difficult to obtain. MATLAB programming environment or its equivalent will be
introduced and used in the course.

2|P a g e
1 | Numerical Solutions to CE Problems 3

UNIT I: REVIEW OF MATHEMATICAL FOUNDATION

1.0 Intended Learning Outcomes


a. To assess the mathematical foundation.
b. Review the derivatives, integrals, matrices and determinants.

1.1 Introduction

Numerical methods are techniques by which mathematical problems are


formulated so that they can be solved with arithmetic and logical operations.
Because digital computers excel at performing such operations, numerical
methods are sometimes referred to as computer mathematics.
MATLAB software will be used in this course. It is a high-level computer
language for scientific computation and data visualization built around an
interactive programming environment. It is becoming the premier platform for
scientific computing at educational institutions and research establishments. The
great advantage of an interactive system is that programs can be tested and
debugged quickly, allowing the user to concentrate more on the principles behind
the program and less on programming itself.

1.2 Topics/Discussion (with Assessment/Activities)

 Pre – Assessment
Solve the following problems.

Evaluation Criteria:
1. Timeliness 50%
2. Correctness 20%
3. Organization of Solution & Answer 20%
4. Cleanliness 10%
100%
1. ∫ 𝟒𝒙𝟔 − 𝟐𝒙𝟑 + 𝟕𝒙 − 𝟒 𝒅𝒙
2. ∫ 𝒙𝟕 − 𝟒𝟖𝒛𝟏𝟏 − 𝟓𝒛𝟏𝟔 𝒅𝒛
3. ∫ 𝟏𝟎𝒕−𝟑 + 𝟏𝟐𝒕−𝟗 + 𝟒𝒕𝟑 𝒅𝒕
4. ∫ 𝒘−𝟐 + 𝟏𝟎𝒘−𝟓 − 𝟖 𝒅𝒘
5. ∫ 𝟏𝟐𝒅𝒚

3|P a g e
1 | Numerical Solutions to CE Problems 4

1.2.1 Physical meaning of derivatives and integrals.

Foundational working tools in calculus, the derivative and integral permeate


all aspects of modeling nature in the physical sciences.
Derivative

 The derivative of a function can be geometrically interpreted as the slope of the


curve of the mathematical function 𝑓(𝑥) plotted as a function of 𝑥.
 Its importance lies in the fact that many physical entities such
as velocity, acceleration, force and so on are defined as instantaneous rates of
change of some other quantity.
 The derivative can give you a precise instantaneous value for that rate of
change and lead to precise modeling of the desired quantity.

Integral

 The integral of a function can be geometrically interpreted as the area under


the curve of the mathematical function 𝑓(𝑥) plotted as a function of 𝑥.
 The integral gives you a mathematical way of drawing an infinite number of
blocks and getting a precise analytical expression for the area.
 The area of a little block under the curve can be thought of as the width of the
strip weighted by (i.e., multiplied by) the height of the strip.
 Many properties of continuous bodies depend upon weighted sums, which to
be exact must be infinite weighted sums - a problem tailor-made for the
integral.
o For example, finding the center of mass of a continuous body involves
weighting each element of mass by its distance from an axis of rotation, a process
for which the integral is necessary if you are going to get a precise value.
o A vast number of physical problems involve such infinite sums in their
solutions, making the integral an essential tool for the physical scientist.

Example 1. You can think of the derivative as representing a rate of change (speed
is one example of this). This makes it very useful for solving physics
problems.
From Physics problem statements:
𝒅𝒒
1. If 𝒒 is an amount of electric charge, the derivative is the change in that
𝒅𝒕
charge over time, or the electric current.
2. A second, more tangible example is to let s stand for distance; then the rate
𝒅𝒔
of change is what we call speed. Let’s investigate this second example in
𝒅𝒕
more detail to get a visceral sense of what instantaneous speed means.

4|P a g e
1 | Numerical Solutions to CE Problems 5

3. On Halloween, MIT students have a tradition of dropping pumpkins from


the roof of the building this lecture was given in. Let’s say that the building
is about 300 feet tall. We’ll use a slightly smaller value of 80 meters for the
height because it makes the problem easier to solve.
4. The equation of motion for objects near the Earth’s surface (which we will
just accept for now) says that the height above the ground ℎ of the pumpkin
𝑡 seconds after it’s dropped from the building is roughly:
𝒉 = 𝟖𝟎 − 𝟓𝒕𝟐 𝒎𝒆𝒕𝒆𝒓𝒔
The instant the pumpkin is dropped, 𝑡 = 0 and ℎ = 80 𝑚𝑒𝑡𝑒𝑟𝑠. When 𝑡 =
4 𝑠𝑒𝑐𝑜𝑛𝑑𝑠 and ℎ = 80 − 5(4)2 = 0, and the pumpkin has reached the
ground. The average speed of the pumpkin over the time it’s falling is
∆ℎ 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑡𝑟𝑎𝑣𝑒𝑙𝑒𝑑 0 − 80 𝑚𝑒𝑡𝑒𝑟𝑠
= = = −20 𝑚/𝑠
∆𝑡 𝑡𝑖𝑚𝑒 𝑒𝑙𝑎𝑝𝑠𝑒𝑑 4 − 0 𝑚𝑒𝑡𝑒𝑟𝑠
(The numerator is 0 − 80 and not 80 − 0 because we must subtract the initial
position from the final position, not the other way around.) The people
watching the pumpkin drop probably don’t care about the average speed.
They want to know how fast the pumpkin is going when it slams into the
ground. That’s known as the instantaneous speed, and is the derivative
𝑑 𝑑
ℎ′(𝑡) = 𝑑𝑡 ℎ. To find the instantaneous velocity at 𝑡 = 5, we evaluate 𝑑𝑡 ℎ.

𝑑
ℎ = 0 − 10𝑡 = −10𝑡
𝑑𝑡
We know that the pumpkin hits the pavement 4 seconds after it’s dropped,
at time 𝑡 = 4, so the pumpkin’s speed is:
𝑚
ℎ′ (4) = (−10)(4) = −40 (𝑎𝑏𝑜𝑢𝑡 90 𝑚𝑝ℎ 𝑜𝑟 145 𝑘𝑝ℎ).
𝑠

1.2.2 Taylor series and expansion

Some functions can be represented as series, which may give valuable


information about the function. Suppose that

𝑓′(𝑥 ) = ∑ 𝑛𝑎𝑛 𝑥 𝑛−1 = 𝑎1 + 2𝑎2 𝑥 + 3𝑎3 𝑥 2 + 4𝑎4 𝑥 3 + ⋯


𝑛=1

𝑓′′(𝑥 ) = ∑ 𝑛(𝑛 − 1)𝑎𝑛 𝑥 𝑛−2 = 2𝑎2 + 3 ∙ 2𝑎3 𝑥 + 4 ∙ 3𝑎4 𝑥 2 + ⋯


𝑛=2

𝑓′′′(𝑥) = ∑ 𝑛(𝑛 − 1)(𝑛 − 2)𝑎𝑛 𝑥 𝑛−3 = 3 ∙ 2𝑎3 + 4 ∙ 3 ∙ 2𝑎4 𝑥 + ⋯


𝑛=2

5|P a g e
1 | Numerical Solutions to CE Problems 6

By examining these it’s not hard to discern the general pattern. The 𝑘𝑡ℎ
derivative must be

𝑓 𝑥 ) = ∑ 𝑛(𝑛 − 1)(𝑛 − 2) … (𝑛 − 𝑘 + 1)𝑎𝑛 𝑥 𝑛−𝑘


𝑘(

𝑛=𝑘

= 𝑘 (𝑘 − 1)(𝑘 − 2) … (2)(1)𝑎𝑘 + (𝑘 + 1)(𝑘 ) … (2)𝑎𝑘+1 𝑘

+(𝑘 + 2)(𝑘 + 1) … (3)𝑎𝑘+2 𝑥 2 + ⋯

We can shrink this quite a bit by using factorial notation:



𝑛! (𝑘 + 2)!
𝑓 𝑘 (𝑥 ) = ∑ 𝑎𝑛 𝑥 𝑛−𝑘 = 𝑘! 𝑎𝑘 + (𝑘 + 1)! 𝑎𝑘+1 𝑥 + 𝑎𝑘+2 𝑥 2 + ⋯
( 𝑛 − 𝑘 )! 2!
𝑛=𝑘

Now substitute 𝑥 = 0:

𝑛!
𝑓 𝑘 (0) = 𝑘! 𝑎𝑘 + ∑ 𝑎 0𝑛−𝑘 = 𝑘! 𝑎𝑘 ,
(𝑛 − 𝑘 ) ! 𝑛
𝑛=𝑘+1

And solve for 𝑎𝑘 :

𝑓 𝑘 (0)
𝑎𝑘 = .
𝑘!

Note the special case, obtained from the series for 𝑓 itself, that gives
𝑓(0) = 𝑎0.
So if a function 𝑓 can be represented by a series, we know just what series
it is. Given a function 𝑓, the series

𝑓 (0) (0) 𝑛
∑ 𝑥
𝑛!
𝑛=0
is called the Maclaurin series for 𝑓.

1
Example 2. Find the Maclaurin series for 𝑓(𝑥 ) = (1−𝑥). We need to compute the
derivatives of 𝑓 (spot the pattern).
𝑓 (𝑥 ) = (1 − 𝑥)−1
𝑓′(𝑥 ) = (1 − 𝑥)−2
𝑓′′(𝑥) = 2(1 − 𝑥)−3
𝑓′′′(𝑥 ) = 3(1 − 𝑥)−4
𝑓 (4) (𝑥 ) = 4! (1 − 𝑥)−5

𝑓 𝑥 = 𝑛! (1 − 𝑥)−𝑛−1
𝑛( )

So

6|P a g e
1 | Numerical Solutions to CE Problems 7

𝑓 (𝑛) (𝑥) 𝑛! (1 − 0)−𝑛−1


= =1
𝑛! 𝑛!
and the Maclaurin series is
∞ ∞

∑ 1 ∙ 𝑥𝑛 = ∑ 𝑥𝑛
𝑛=0 𝑛=0
Note:

The general form of Taylor series for a function is given by,


𝑓 (𝑛)(𝑎)
(𝑥 − 𝑎)𝑛 .
𝑛!
A Maclaurin series is simply a Taylor series with 𝒂 = 𝟎.

1.2.3 Definition of matrix and vector; Matrix algebra – summation, subtraction


and multiplication of matrices; transpose, determinant and rank of a matrix.

Matrices and Determinants

 Matrix
- A matrix is a rectangular collection of variables or scalars contained within
a set of square [ ] or round ( ) brackets. A matrix consist of 𝑚 rows and 𝑛
columns.
 Classification of Matrices
- Square Matrix
o A matrix whose number of rows 𝑚 is equal to the number of
columns 𝑛.
- Diagonal Matrix
o A diagonal matrix is a square matrix with all zero values except for
the 𝑎𝑖𝑗 value for all 𝑖 = 𝑗.
2 0 0
[0 3 0]
0 0 1
- Identity Matrix
o An identity matrix is a diagonal matrix with all non-zero entries
equal to 1.

1 0 0
[0 1 0]
0 0 1
- Scalar Matrix
o A scalar matrix is a diagonal matrix with all non-zero entries equal
to some other constant.

8 0 0
[0 8 0]
0 0 8

7|P a g e
1 | Numerical Solutions to CE Problems 8

- Triangular Matrix
o A triangular matrix has zero in all positions above or below the
diagonal.

 Important Algebraic Operations in Matrices


1. Equality of Matrices
- Two matrices are equal if they have the same number of rows and columns
and their corresponding entries are also equal.

2. Addition and Subtraction of Matrices


- Addition (or subtraction) of two matrices can be accomplished by adding
(or subtracting) the corresponding entries or two matrices which have the
same shape.

Example.

1 4 1 3 0 2
Add [ 7 1 6] + [2 5 6]
−3 0 4 9 1 1
1+3 4+0 1+2 4 4 3
= [ 7 + 2 1 + 5 6 + 6] = [9 6 12]
−3 + 9 0 + 1 4 + 1 6 1 5

3. Multiplication of Matrices
- Multiplication of matrix can be done only if the number of columns of the
left-hand matrix is equal to the number of rows of the right-hand matrix.
Multiplication is accomplished by multiplying the elements in each right-
hand matrix column, adding the products, and then placing the sum at the
intersection point of the involved row and column.

Example 1.
2 3 1 3
If 𝐴 = [ ] 𝑎𝑛𝑑 𝐵 = [ ], find 𝐴 × 𝐵.
3 10 5 −2
Solution:

2 3 1 3
𝐴×𝐵 = [ ][ ]
3 10 5 −2

2(1) + 3(5) 2(3) + 3(−2) 𝟏𝟕 𝟎


𝐴×𝐵 =[ ]=[ ]
3(1) + 10(5) 3(3) + 10(−2) 𝟓𝟑 −𝟏𝟏

Example 2.
2 2(2) + 1(4) + 5(1)
2 1 5 𝟏𝟑
[ ] × [4] = [ ]=[ ]
1 4 7 1( 2) + 4 ( 4) + 7(1) 𝟐𝟓
1

Example 3.

8|P a g e
1 | Numerical Solutions to CE Problems 9

1 3 𝑥 5
If [ ] [ ] = [ ] then
2 4 𝑦 6

1𝑥 + 3𝑦 = 5
2𝑥 + 4𝑦 = 6

4. Division of Matrices
- Division of matrices can be accomplished only by multiplying the inverse
of the denominator matrix.

 Other Operations on a Matrix

1. The Transpose of a Matrix


The transpose is an (𝑛 × 𝑚) matrix formed from the original (𝑚 × 𝑛)
matrix by taking the 𝑖 𝑡ℎ row and making it the 𝑖 𝑡ℎ column. The diagonal is
unchanged in this operation. The transpose of a matrix is indicated as 𝐴𝑡 .

Example:

1 6 9
Determine the transpose of 𝐴 = [2 3 4]
7 1 5

Solution:

𝟏 𝟔 𝟗
𝑨 = [𝟐 𝟑 𝟒]
𝟕 𝟏 𝟓

2. The Determinant of a Matrix


The determinant 𝐷, is a scalar calculated from a square matrix. The
determinant of a matrix is indicated by enclosing the matrix by vertical lines.

 Properties of Determinants
A. If a matrix has a row or column of zeros, the determinant is zero.

1 4 0 2 1 5
|4 5 0 | = 0 |0 0 0| = 0
1 7 0 4 1 9

B. If a matrix has two identical rows or columns, the determinant is zero.

1 2 5 2 1 5
|4 6 1 | = 0 |0 0 0| = 0
1 2 5 4 1 9

9|P a g e
1 | Numerical Solutions to CE Problems 10

C. If a matrix is triangular, the determinant is equal to the product of the


diagonal entries.

2 0 0
|0 3 0| = 2(3)(5)
0 0 5

D. The value of the determinant is not changed if corresponding rows and


columns are interchanged.

1 4 6 1 2 1
|2 5 2| = |4 5 7| = 0
1 7 9 6 2 9
E. If each of a column or row of a determinant is multiplied by 𝑚, the value
of the determinant is multiplied by 𝑚.
1 4 5 1 2×2 5 1 2 5
|4 6 1| = |4 3 × 2 1| = 2 |4 3 1|
2 8 4 2 4×2 4 2 4 4

By properties defined in 𝐵 & 𝐸, the following can be applied:


1 4 2
|5 1 10| = 0
3 6 6
(since the elements of columns 1 and 3 are exact multiples)

F. If two columns or rows of a determinant are interchanged, the sign is


changed
2 1 6 2 6 1
|5 4 7| = − |5 7 4|
1 3 9 1 9 3

G. The value of a determinant is not changed if each element of a column (or


rows) is multiplied by a number 𝑘 and added (or subtracted) to the
corresponding elements of a column (or row)

1 4 5 1 4 5 + 1(3) 1 4 8
|4 6 1| = |4 6 1 + 4(3)| = |4 6 13|
2 8 4 2 8 4 + 2(3) 2 8 10

H. If each element of a column (say the 𝑘 𝑡ℎ column) of a matrix is expressed


as the sum of two terms, the determinant is equal to the sum of the two
determinants, where (a) the elements of each of the two determinants are
identical to the corresponding elements of the given determinant except
for the elements of the 𝑘 𝑡ℎ column, and (b) the first term of the 𝑘 𝑡ℎ column
of the given determinant form the 𝑘 𝑡ℎ column of one of the two

10 | P a g e
1 | Numerical Solutions to CE Problems 11

determinants and the second term form the 𝑘 𝑡ℎ column of the other
determinant.
4 1 2 2 1 2 2 1 2
|6 2 3| = | 5 2 3 | + | 1 2 3|
2 5 1 1 5 1 1 5 1
Example 1. Solve for x: (2nd order)
4 5
𝑥=| | = 4(3) − 2(5) = 2
2 3
Example 2. Solve for x: (3rd order)
1 −3 2
𝑥 = |2 1 −4|
3 −2 1

1 −4 −3 2 −3 2
𝑥 = 1| | − 2| |+3| |
−2 1 −2 1 1 −4
𝑥 = 1(1 − 8) − 2(−3 + 4) + 3(12 − 2) = 𝟐𝟏

+ − +
Use of sign: |− + −|
+ − +

Another Solution:
1 −3 2 1 −3
𝑥 = |2 1 −4| 2 1|
3 −2 1 3 −2
𝑥 = [1(1)(1) ± 3(−4)(3) + 2(2)(−2)]
− [3(1)(2) + (−2)(−4)(1) + 1(2)(−3)]
𝒙 = 𝟐𝟏
Example 3. Solve for x: (4th order)
2 0 1 −1
𝑥 = |1 2 −2 3|
3 −2 3 −2
4 −4 −2 − 3
By pivotal element method, select the element in the second row, first
column (since it’s unity)

0 − (2)(2) 1 − (2)(−2) −1 − (2)(3)


2+1 |−2 −(3)(2) 3 − (3)(−2) −2 − (3)(3) |
𝑥 = 1(−1)
−4 − (4)(2) −2 − (4)(−2) −3 − (4)(3)

11 | P a g e
1 | Numerical Solutions to CE Problems 12

−4 5 −7 −4 5
𝑥 = −1 | −8 9 −11| −8 9|
−12 6 −15 −12 6
𝑥 = −1{[540 + 660 + 336] − [756 + 264 + 600]} = 𝟖𝟒

3. The Cofactor of an entry in a Matrix


The cofactor of an entry in a matrix is the determinant of the matrix
formed by omitting the entry’s row and column in the original matrix. The
sign of the cofactor is determined from the following positional matrices:

+ − +
|− + − |
+ − +
or, the sign of the cofactor can be determined by the relation (−1)𝑖+𝑗 , where
𝑖 is column number and 𝑗 is the row number.

Example 1. Find the cofactor of -2 in the following matrix.


2 7 3
|−2 5 6|
3 4 7
Solution: -2 is at column 1 row 2. The resulting matrix is:
7 3
(−1)1+2 [ ]
4 7
The cofactor is

(−1) [7 3] = −[7(7) − 4(3)] = −37


4 7

4. Classical Adjoint
The classical adjoint is a matrix formed from the transposed cofactor
matrix with the conventional sign arrangement. The resulting matrix is
represented as 𝐴𝑎𝑑𝑗 .

Example 1. Determine the classical adjoint of


2 3 −4
[0 −4 2 ]
1 1 5
Solution:

After solving the cofactors of each entry, the matrix of cofactors is:

12 | P a g e
1 | Numerical Solutions to CE Problems 13

−18 2 4
[−11 −14 5]
−10 −4 −8
The classical adjoint is
−18 −11 −10
𝐴𝑎𝑑𝑗 = [ 2 14 −4 ]
4 5 −8

5. The Inverse Matrix

The inverse, 𝐴−1 , of a matrix A is a matrix such that (𝐴)( 𝐴−1 ) = 𝐼, where
𝐼 is a square matrix with ones along the left-to-right diagonal and zeros
elsewhere.
4 5
Example 1. Determine the inverse of [ ]
2 3
Solution: The determinant is:
4 5
𝐷=| | = 4(3) − 2(5) = 2
2 3
The inverse is:
1 3 −5 3/2 −5/2
[ ]=[ ]
2 −2 4 −1 2

13 | P a g e
1 | Numerical Solutions to CE Problems 14

UNIT II: FUNDAMENTALS OF COMPUTER AIDED


MATHEMATICAL CALCULATIONS

2.0 Intended Learning Outcomes


a. To review the basic knowledge of computer aided mathematical
calculations.
b. To gain a general understanding of built-in functions and how you can learn
more about them with MATLAB’s Help facilities

2.1 Introduction

MATLAB is a computer program that provides the user with a convenient


environment for performing many types of calculations. In particular, it provides
a very nice tool to implement numerical methods. The most common way to
operate MATLAB is by entering commands one at a time in the command
window.
In this chapter, we use this interactive or calculator mode to introduce you to
common operations such as performing calculations and creating plots. In this
unit we show how such commands can be used to create MATLAB programs.
One further note. This chapter has been written as a hands-on exercise. That is,
you should read it while sitting in front of your computer. The most efficient way
to become proficient is to actually implement the commands on MATLAB as you
proceed through the following material.

2.2 Topics/Discussion (with Assessment/Activities)

Features of MATLAB

Following are the basic features of MATLAB:


 It is a high-level language for numerical computation, visualization and
application development.
 It also provides an interactive environment for iterative exploration,
design and problem solving.
 It provides vast library of mathematical functions for linear algebra,
statistics, Fourier analysis, filtering, optimization, numerical integration
and solving ordinary differential equations.
 It provides built-in graphics for visualizing data and tools for creating
custom plots.

14 | P a g e
1 | Numerical Solutions to CE Problems 15

 MATLAB's programming interface gives development tools for


improving code quality, maintainability, and maximizing performance.
 It provides tools for building applications with custom graphical
interfaces.
 It provides functions for integrating MATLAB based algorithms with
external applications and languages such as C, Java, .NET and Microsoft
Excel.

Uses of MATLAB
MATLAB is widely used as a computational tool in science and engineering
encompassing the fields of physics, chemistry, math and all engineering streams.
It is used in a range of applications including:
 signal processing and Communications
 image and video Processing
 control systems
 test and measurement
 computational finance
 computational biology

Understanding the MATLAB Environment


MATLAB uses three primary windows:
• Command window. Used to enter commands and data.
• Graphics window. Used to display plots and graphs.
• Edit window. Used to create and edit M-files.
After starting MATLAB, the command window will open with the
command prompt being displayed

The calculator mode of MATLAB operates in a sequential fashion as you type in


commands line by line. For each command, you get a result. Thus, you can think of it
as operating like a very fancy calculator. For example, if you type in

MATLAB will display the result

Notice that MATLAB has automatically assigned the answer to a


variable, ans. Thus, you could now use ans in a subsequent calculation:

15 | P a g e
1 | Numerical Solutions to CE Problems 16

with the result

MATLAB assigns the result to ans whenever you do not explicitly


assign the calculation to a variable of your own choosing.

The desktop has the following panels:

1. Current Folder - This panel allows you to access the project folders and files.

2. Command Window - This is the main area where commands can be entered at
the command line. It is indicated by the command prompt (>>).

16 | P a g e
1 | Numerical Solutions to CE Problems 17

3. Workspace - The workspace shows


all the variables created and/or
imported from files.

4. Command History - This panel


shows or rerun commands that are
entered at the command line.

17 | P a g e
1 | Numerical Solutions to CE Problems 18

Basic Syntax

Practice!
Type a valid expression, for example,

and press ENTER

when you click the Execute button, or type Ctrl+E, MATLAB executes it
immediately and the result returned is:

try this one,

when you click the Execute button, or type Ctrl+E, MATLAB executes it
immediately and the result returned is:

another example,

When you click the Execute button, or type Ctrl+E, MATLAB executes it
immediately and the result returned is:

Another example,

18 | P a g e
1 | Numerical Solutions to CE Problems 19

When you click the Execute button, or type Ctrl+E, MATLAB executes it
immediately and the result returned is:

Another example,

When you click the Execute button, or type Ctrl+E, MATLAB executes it
immediately and the result returned is:

MATLAB provides some special expressions for some mathematical symbols, like
pi for π, Inf for ∞, i (and j) for √-1 etc. Nan stands for 'not a number'.

Use of Semicolon (;) in MATLAB

Semicolon (;) indicates end of statement. However, if you want to suppress and
hide the MATLAB output for an expression, add a semicolon after the expression.

For example,

When you click the Execute button, or type Ctrl+E, MATLAB executes it
immediately and the result returned is:

19 | P a g e
1 | Numerical Solutions to CE Problems 20

Adding Comments
The percent symbol (%) is used for indicating a comment line. For example,

You can also write a block of comments using the block comment operators % {
and % }.

The MATLAB editor includes tools and context menu items to help you add,
remove, or change the format of comments.

Commonly used Operators and Special Characters


MATLAB supports the following commonly used operators and special characters:

Operator Purpose
+ Plus; addition operator
- Minus; subtraction operator
* Scalar and matrix multiplication operator
.* Array multiplication operator
^ Scalar and matrix exponentiation operator
.^ Array exponentiation operator
\ Left-division operator
/ Right-division operator
.\ Array left-division operator
./ Array right-division operator
: Colon; generates regularly spaced elements and represents an
entire row or column.
() Parentheses; encloses function arguments and array indices;
overrides precedence.
[ ] Brackets; enclosures array elements.
. Decimal point
… Ellipsis; line-continuation operator
, Comma; separates statements and elements in a row
; Semicolon; separates columns and suppresses display
% Percent sign; designates a comment and specifies formatting
- Quote sign and transpose operator
˙_ Non-conjugated transpose operator
= Assignment proper

20 | P a g e
1 | Numerical Solutions to CE Problems 21

Special Variables and Constants

Name Meaning
ans Most recent answer
eps Accuracy of floating-point precision
i,j The imaginary unit √-1.
Inf Infinity
Nan Undefined numerical result (not a number)
pi The number n

Naming Variables
Variable names consist of a letter followed by any number of letters, digits or
underscore.

MATLAB is case-sensitive.
Variable names can be of any length, however, MATLAB uses only first N characters,
where N is given by the function namelengthmax.
How to save your work?
The save command is used for saving all the variables in the workspace, as a file with
.mat extension, in the current directory.

For example,

You can reload the file anytime later using the load command.

Commands for Managing a Session


Commands Purpose
Clc Clears command window
Clear Removes variables from memory
Exist Checks for existence of file or variable
Global Declares variables to be global
Help Searches for a help topic
Lookfor Searches help entries for a keyword
quit Stops MATLAB
who Lists current variables
whos Lists current variables (long display)

21 | P a g e
1 | Numerical Solutions to CE Problems 22

Commands for Working with the System


Commands Purpose
cd Changes current directory
date Displays current date
delete Deletes a file
diary Switches on/off diary file recording
dir Lists all files in current directory
load Loads workspace variables from a file
path Displays search path
pwd Displays current directory
save Saves workspace variables in a file
type Displays contents of a file
what Lists all MATLAB files in the current directory
wklread Reads .wk1 spreadsheet file
Input and Output Commands
disp Displays contents of an array or string
fscanf Read formatted data from a file
format Controls screen-display format
fprintf Performs formatted writes to screen or file
input Displays prompts and waits for input
; Suppresses screen printing
%s Format as a string
%d Format as an integer
%f Format as a floating point value
%e Format as a floating point value in scientific notation
%g Format in the most compact form: %f or %e
\n Insert a new line in the output string
\t Insert a tab in the output string
format short Four decimal digits (default)
format long 16 decimal digits
format short e Five digits plus exponent
format long e 16 digits plus exponents
format bank Two decimal digits
format + Positive, negative, or zero
format rat Rational approximation
format compact Suppresses some line feeds
format loose Resets to less compact display mode
Vector, Matrix, and Array Commands
cat Concatenates arrays.

22 | P a g e
1 | Numerical Solutions to CE Problems 23

Find Finds indices of nonzero elements


length Computes number of elements
linspace Creates regularly spaced vector
logspace Creates logarithmically spaced vector
max Returns largest element
min Returns smallest element
prod Product of each column
reshape Changes size
size Computes array size
sort Sorts each column
sum Sums each column
eye Creates an identity matrix
ones Creates an array of ones
zeros Creates an array of zeros
cross Computes matrix cross products
dot Computes matrix dot products
det Computes determinant of an array
inv Computes inverse of a matrix
pinv Computes pseudoinverse of a matrix
rank Computes rank of a matrix
rref Computes reduced row echelon form
cell Creates cell array
celldisp Displays cell array
cellplot Displays graphical representation of cell array
num2cell Converts numeric array to cell array
deal Matches input and output lists
iscell Identifies cell array
Plotting Commands
axis Sets axis limits
fplot Intelligent plotting of functions
grid Displays gridlines
plot Generates xy plot
print Prints plot or saves plot to a file
title Puts text at top of plot
xlabel Adds text label to x-axis
ylabel Adds text label to y-axis
axes Creates axes objects
close Closes the current plot
close all Closes all plots

23 | P a g e
1 | Numerical Solutions to CE Problems 24

figure Opens a new figure window


gtext Enables label placement by mouse
hold Freezes current plot
legend Legend placement by mouse
refresh Redraws current figure window
set Specifies properties of objects such as axes
subplot Creates plots in sub windows.
text Places string in figure
bar Creates bar chart
loglog Creates log-log plot
polar Creates polar plot
semilogx Creates semi log plot. (logarithmic abscissa)
semilogy Creates semi log plot. (logarithmic ordinate)
stairs Creates stairs plot
stem Creates stem plot

24 | P a g e
1 | Numerical Solutions to CE Problems 25

Post – Assessment. Solve the following problems. Show your complete solution.

Evaluation Criteria:
Timeliness 50%
Correctness 20%
Organization of Solution & Answer 20%
Cleanliness 10%
100%

A number of matrices are defined as


4 5 4 3 7
[𝐴] = [1 2] [𝐵] = [1 2 6] Perform the following
5 6 2 0 4 operations:

1. [𝐸] + [𝐵]
2. [𝐴] + [𝐹]
2
{𝐶 } = {6} [𝐷 ] = [5 4 3 − 7] 3. [𝐵] − [𝐸]
2 1 7 5 4. 7 × [𝐵]
1
5. {𝐶 }𝑇
6. [𝐸] × [𝐵]
1 5 6 7. [𝐵] × [𝐸]
[𝐸] = [7 1 3] 8. [𝐷 ]𝑇
4 0 6 9. [𝐺] × {𝐶 }
10. [𝐼 ] × [𝐵]
[𝐹 ] = [2 0 1
] ⌊𝐺 ⌋ = ⌊8 6 4⌋ 11. [𝐸]𝑇 × [𝐸]
1 7 4
12. {𝐶 }𝑇 × {𝐶 }

25 | P a g e
1 | Numerical Solutions to CE Problems 26

2.3 References
1. Ayyub, BilaL. Numerical Analysis for Engineers. 2016.
2. Tutorials Point (I) Pvt.Ltd. MATLAB Numerical Computing. 2014.
3. Kreyszig, E. and Norminton, E. 10th Edition. 2011.

2.4 Acknowledgement

The images, tables, figures and information contained in this module were
taken from the references cited above.

26 | P a g e
1 | Numerical Solutions to CE Problems 27

6700

27 | P a g e
1 | Numerical Solutions to CE Problems 28

28 | P a g e

You might also like