You are on page 1of 47

Experiment (1):

Environment of MATLAB

INTRODUCTION:
The name MATLAB stands for Matrix Laboratory. The basic building block of
MATLAB is the matrix. It is not confined to the solution of Matrix related problems. With
its inbuilt functions, it is an excellent tool for linear algebraic computations, data analysis,
signal processing, optimization, numerical solutions of ordinary differential equations
(ODE), quadrature, 2D & 3D, graphics and many other types of scientific computation.
Therefore, we can say: MATLAB is a software package in high performance language for
technical computing. It integrates computation, visualization and programming in an easy
to use environment where problems and solutions are expressed in familiar mathematical
notation.
Exercises:

1. Change the font size, type and color of the code


2. Change the layout of the windows in Matlab
3. Write any mathematical expression in the command window. Where could you find
the results with and without the ; at the end of the line?
4. Write the same expression in m file, name the file, save it and find the results with
and without putting ; in the end of each line
5. Use help and search bar to find how could you use the following functions in the m
file
a. Log, ln, exp
b. The trigonometric functions
c. Sum, mean, average
d. Integral and derivative
6. Save your m file and your workspace in a specific folder
7. Create a folder and save all your files (m file and workspace) in it.

1
Experiment (2):

Mathematical Functions and Arithmetic Operations

MATLAB can be used as a hand calculator to perform arithmetic operations. MATLAB


uses the symbols +, –, *, /, and ^ for the addition, subtraction, multiplication, division, and
exponentiation (power) of scalars, respectively. Other different mathematical functions can
be used to solve problems in matlab such as log, ln, exp, abs, statistical functions and
trigonometric functions.
Exercises:

1. Using MATLAB, find the results of the following



a. =

b. = |3 + 4 |

c. = sin + cos − 2√3

d. = + 4 + 15
e. = cos 30°" − tan 45°" + 3
2.

3. Use [ round , fix , ceil, floor] commands to round the following numbers towards
integer numbers
a. Pi
b. 4.25
c. 6.99
d. 100.001
e. 30.67

2
f. 50.499
g. 60.51

4. Represent the following complex numbers in polar coordinate, and find the
conjugate of them
a. Z= 2 + 5j
b. Y= -3 - 3j
c. D= -2 + 6j

5. Represent the following numbers in rectangular coordinate


a. W= 5∟30o
b. A= 2.5∟-20o
c. Q= 3∟-73o
6.

7.

3
Experiment (3):

Matrix Operations

Exercises:

1. For Matrices A and B

3 2 1 1 3 0
%=& 0 3 4 ' )*+ , = & 2 6 4'
−1 1 −1 −1 0 2

Find the following

a. A+B
b. A-B
c. A*B
d. B*A
e. A\B
f. B\A
g. A-1
h. B-1
i. 2A
4 −1 1 2 −4 1 4 1 3
2. Let % = & 2 3 0', , = & 1 1 7', / = & 3 2' and 0 = &0'
−2 −3 6 −1 −3 0 −6 0 1

Find the following


a. A+B
b. AB
c. AC
d. Ax
e. B-A
f. A-1
g. (BA)-1

4
3. Generate the following matrices (the size of the generated matrix is 3 by 3 )
a. Magic matrix
b. Zero matrix
c. One matrix
d. Random matrix
e. Diagonal matrix

1 4
4. If 0 = 1 3, find :
8 3
a) the inverse matrix of x .
b) the diagonal of x.
c) the sum of each column and the sum of whole matrix x.
d) the transpose of x.

2 8 5
5. If 0 = 1 3, 5 = 62 4 57 find:
9 7 1
a) find the maximum and minimum of x.
b) find median value over each row of x.
c) add the vector b as a third row to x.

6. For the x=[4 -1 3 5 9], find the mean, median, standard deviation, variance, sum,
product, maximum and minimum.

2 −3 5
7. let 0 = 1 3, then
0 11 0
a) find elements in x that are greater than 2
b) find the number of nonzero elements in x

5
8.

9.

In 50

6
Experiment (4):
Built in Functions and User defined functions
1.
a. Factor the number 322
b. Find the Greatest common denominator 0f 322 and 6
c. Find 10!
d. Find the number of possible ways to choose 3 people from a group of 20
without repetition
2. The Heat index HI, calculated from the air temperature and relative humidity is the
apparent temperature felt by the body. The following equation is used:
89 = −42.379 + 2.04901523; + 10.14333127< − 0.22475541<;
− 6.83783 × 10> ; − 5.481717 × 10> <
+ 1.22874 × 10> ; < + 8.5282 × 10> ;< − 1.9910>? ; <

Where T is the temperature in Fahrenheit and R is the relative humidity percentage.


Write a MATLAB program to fine the HI when T=90 F and R=90%

3.

7
4.

5. Write a user-defined MATLAB function for the following math function:


r(theta) = 4cos(4sin (theta) )
The input to the function is theta (in radians) and the output is r. Write the function
such that theta can be a vector. Use the function to calculate r(pi/6) and r(5pi/6).
6.

8
7.

9
Experiment (5):

Dealing with Errors

1. A student writes a Matlab program to solve the following exercise, but the
program dose not run, list the errors and fix them to run the program
correctly.

Exercise: For Matrices A and B

3 2 1 1 3 0
%=& 0 3 4 ' )*+ , = & 2 6 4'
−1 1 −1 −1 0 2

Find the following

a. A+B
b. A*B
c. A\B
d. A-1
e. 2A
Student’s solution (program is saved in file named (matlab.m)
cls
clears all
A=[3,2,1,0,3,4;-1,1,-1];
B={1,3,0;2,6,4;-1,0,2};
Part a
addAB=A.+B
%part b
mulAB=a*b;
%part c
divAB=A/B;
%part d
Inv =A’;
%part e 2A=A^2;

10
2. A student writes a matlab program to find the solution for the following

Student’s solution
tan(X)+sinx;
lhs=Cos^2(x/2);
x=pi\5;
rhs=lsh;

The program does not run and give correct answer. List all the errors and fix
them.
3. The following program is written to find the results of the following equation
(x3+x2+1) for and x using user defined functions. Find the errors and fix them
to achieve the right answer.

User program

Input (‘enter x)
Y=eq(x);

User function

Function eq=funx(x)
Eq= x3+x2+1;

4.

11
12
Experiment (6)
Looping Instructions (do.. while, for ..next)
1.

2.

3.

13
4.

14
Experiment (7):
Manipulating polynomials and finding polynomials roots

1.

2.

3.

4.

5.

6. Find the roots of the following

7.

8.

15
9.

16
Experiment (8):
Graphic Plots (Two dimension plots, log-log and semi- log plots)
1.

2.

3.

4.

17
5.

6. Find the locations of the roots of the following equation using plot

7. Find the location of the roots of the following equation using plot

In the period [-8,8]


8. Find the location of the roots of the following equation using plot

In the period [0,1]


9.

18
Experiment (9): Solve System of Linear Equations by using backslash and Cramer
method

Note: make your own Cramer method function to use it in all following tasks

1. Write a program to solve linear equation using Cramer rule and backslash division
5x= 3y-2z+10
8y+4z= 3x+20
2x+4y-9z=9
2. Write a program to solve linear equation using Cramer rule and backslash division
5x= 5y-8z
9y+3z= x+6
10x+4y-z=7

3. Solve the following system using Cramer rule and backslash division
x+y=1
x-y+z=0
x+y+z=2
4. Solve the following system using Cramer rule and backslash division
x + y - 2z = 3
2x + y = 7
x+y-z=4
5. Solve the linear equations system using Cramer rule and backslash division
6x+2y+8z-26=0
3x-8+2z=-5y
8y+2z=-7
6. Solve the linear equations system using Cramer rule and backslash division

19
7. Solve the linear equations system using Cramer rule and backslash division

8. Solve the linear equations system using Cramer rule and backslash division

9. Solve the linear equations system using Cramer rule and backslash division

20
Experiment (10):

Solve System of Linear Equations by Gauss-Jordan Elimination Method

1. Solve the following system using Gauss-Jordan Method


x-2y+z=0

2x+y-3z=5

4x-7y+z=-1

2. Solve the following system using Gauss-Jordan Method


2x-2y=-6

x-y+z=1

3y-2z=-5

3. Solve the following system using Gauss-Jordan Method


x+y-3z=4

2x+y-z=2

3x+2y-4z=7

4. Solve the following system using Gauss-Jordan Method

5. Solve the following system using Gauss-Jordan Method

21
6. Solve the following system using Gauss-Jordan Method

7. Solve the following system using Gauss-Jordan Method

8. Solve the following system using Gauss-Jordan Method

22
Experiment (11):
Graphic Plots (Linear Regression and Curve fitting)

1.

2. Find the straight line, quadratic and cubic equations that fit to the following data

3.

4.

23
5.

6.

24
7.

8.

25
Experiment 12: One- and Two-Dimensions Interpolation

1.

2.

26
3.

4.

27
28
Experiment 13: Roots Finding of Nonlinear Equations (Newton Raphson and Bisection
methods)
1. Using Newton Raphson method

2.

3.

Using both Newton Raphson and Bisection methods

4.

29
5.

6. Using both Newton Raphson and Bisection methods find the roots of the equation

In any suitable interval.

7. Find the roots of f in the interval [-8,8] using both Newton Raphson and Bisection
methods

8. Find the roots of the following expression in the interval [0,1] using both Newton
Raphson and Bisection methods

9. Find the roots of f using Bisection methods do not exceed the tolerance (0.001)

10. Find the roots of the following expression in the interval [1,2] using both Newton
Raphson and Bisection methods

30
Experiment 14: Numerical Integration by Trapezoidal, Simpson and Lobato Rules
1.

What is the difference if you use Simpson’s rule? Find the error between them
2.

Find the error between the Lobato rule and the Trapezoidal rule

3.

Find the error between the results of Simpson rule and the trapezoidal rule
4.

Find the result analytically. What is the difference between the results?

31
5.

6.

7.

32
8.

9.

10.

11.

12. Using Matlab find the result of the integral

33
Experiment 15: Ordinary Differential Equations (Numerical Solutions using Modified
Euler method)

1.

2.

3.

34
35
Experiment 16: Ordinary Differential Equations (Numerical Solutions using Runge Kutta
method)

1. Solve the ODE @


= 1/ 0 + " using Runge-Kutta method for the interval [0 2]
with h=0.5 where y(0)=1. Your solution should be by hand and with MATLAB
code.
2. Solve the ODE @
= 0− " using Runge-Kutta method for the interval [0 1] with
h=0.2 where y(0)=1. Your solution should be by hand and with MATLAB code.
3. Solve the ODE @
= * 0+ + 1" using Runge-Kutta method for the interval [0
1] with h=0.2 where y(0)=0. Your solution should be by hand and with MATLAB
code.
4. Solve the ODE @
= cos 0" + sin " using Runge-Kutta method find the values
of y(0.2),y(0.4) and y(0.6) where y(0)=1. Your solution should be by hand and with
MATLAB code.

36
Experiment 17: Ordinary Differential Equations systems (Numerical Solutions using
Runge Kutta and Euler methods)

1. Solve the ODE system 1@ = 1 + B" 1 + 2 )*+ 2@ = 1 − B" 1 − 2 using


4th order Runge-Kutta and Euler methods for the interval [0 0.2] with h=0.1 and
h=0.05 where y1(0)=1 and y2(0)=0. Your solution should be by hand and with
MATLAB codes.
2.

3.

37
Experiment 18: The actual solutions of ODE in Matlab
1. Find the actual solution using dsolve function

2. Find the actual solutions using Laplace transform

38
Experiment 19: Applications
1. Water flows inside a horizontal circular pipe at 25°C. Determine the average
velocity and plot the velocity distribution inside the pipe. The length and the inside
radius of the pipe are 15 and 0.009 m, respectively, the viscosity of water is 8.937
× 10-4 kg/(m · sec), and the pressure drop is ΔP = 520 Pa.

2. Water flows inside a smooth pipeline at 21°C with a flow rate of Q=0.0085 m3/sec. The
length of the pipeline is L=250 m, and the elevation of the upstream point is high
enough to overcome the friction loss Ff= 27.5 J/kg. The Fanning friction factor is given
byf =0.0468 NRe-0.2, and the density and the viscosity of water at 21°C are ρ =
997.91 kg/m3and µ = 0.000982 kg/m/sec, respectively. Determine the diameter of the
pipeline. Note : gc=1, use fzero function to find D.

3. Determine y at x = 0.45 using the benzene–toluene equilibrium data shown in the


table below. Try various interpolation methods.

4. The Figure below shows a series of three well-mixed tanks. From mass balance
equations, we have

where xi (i = 1, 2, 3) is the concentration (mol/liter) of the solution contained in the


tank i. Under normal steady-state operation, m is maintained at 0 and qi (1, 2, 3) is
kept constant. At a certain time (t = 0), m is suddenly increased to 12 liter/min. Plot
the concentrations in the three tanks from t = 0 to 2. The initial conditions are x0 =
0.15 mol/liter and q0 = 15 liter/min, and the initial volume of each tank is 20 liter.

39
5. The variation of heat transfer per unit area (q) during the boiling of water under
pressure (p) has been found to be as follows:

Develop a suitable polynomial relation between q and p by trying out curve fitting.
6. A biological process involves the growth of biomass from substrate. The material
balances on this batch process yield

where B and S are the respective biomass and substrate concentrations. The reaction
kinetics are such that k = 0:3 and K = 10-6 in consistent units. Solve this set of
deferential equations starting at t0 = 0, when S = 5 and B = 0:05, to a final time
given by Tf = 20 using Euler method (h=0.2). Assume consistent units. Plot S and
B vs. time.

40
Experiment 23: Linear Programming Optimization

1. Solve the following using Optimization application in matlab

41
2.

42
43
44
Experiment 24 : Multi-objective Optimization

45
Experiment 25: Large-scale Algorithms

46

You might also like