Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

LAB 04

Introduction to Programming in MATLAB


Objective

To practice developing of Programs, finding solution of ODE, Polynomial roots and Partial
Fractions.

1.0. Introduction to Programming in MATLAB

An Engineer's Introduction to Programming with MATLAB 2018 uses an approachable tone to


take you from simple variables to complex examples of data visualization and curve fitting. Each
chapter builds on the last, presenting an in-depth tutorial on a focused concept central to
programming, while using the MATLAB language. Readers will perform short exercises as they
work through each chapter, followed by more end-to-end exercises and mental challenges at the
chapter's end. As the complexity of the concepts increases, the exercises present increasingly real-
world engineering challenges to match.

It is easy to learn, versatile and very useful for engineers and other professionals. MATLAB is a
special-purpose language that is an excellent choice for writing moderate-size programs that solve
problems involving the manipulation of numbers. The design of the language makes it possible to
write a powerful program in a few lines. The problems may be relatively complex, while the
MATLAB programs that solve them are relatively simple: relative that is, to the equivalent
program written in a general-purpose language, such as C++ or Java. As a result, MATLAB is
being used in a wide variety of domains from the natural sciences, through all disciplines of
engineering, to finance, and beyond, and it is heavily used in industry. Hence, a solid background
in MATLAB is an indispensable skill in today’s job market.

Applications of MATLAB

 Math and computation, Algorithm development.


 Modeling, simulation, and prototyping.
 Data analysis, exploration, and visualization. It involves mechanical engineering, Robotics
researchers and engineers use MATLAB to design and tune algorithms, model real-
world systems, and automatically generate code – all from one software environment.

Control Engineering Lab


LAB 04

1.1. Conditional Statements (if statement and else-if statement)


 If statement

An if statement is a programming conditional statement that, if proved true, performs a function


or displays information. Below is a general example of an if statement.

 Else-if statement

Control Engineering Lab


LAB 04

1.2. Loop functions (for loop and while loop)


 For loop

A "For" Loop is used to repeat a specific block of code a known number of times. For example,
if we want to check the grade of every student in the class, we loop from 1 to that number. When
the number of times is not known before hand, we use a "While" loop.

 While loop

While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to
be executed multiple times depending upon a given Boolean condition. It can be viewed as a
repeating if statement. The while loop is mostly used in the case where the number of iterations is
not known in advance.

Control Engineering Lab


LAB 04

2.0. Ordinary Differential Equations


An ordinary differential equation (ODE) is an equation that involves some ordinary derivatives (as
opposed to partial derivatives) of a function. Often, our goal is to solve an ODE, i.e., determine
what function or functions satisfy the equation.

E.g. ODE. Y' = x+1 is an example of ODE.

Applications

a) Classical mechanics:
b) Electrodynamics:
c) General relativity
d) Quantum mechanics:
e) Cooling/Warming law
f) Population Growth and Decay
g) Radio-active Decay and Carbon Dating

Control Engineering Lab


LAB 04

2.1. Defining an ODE in MATLAB Editor and finding its solution

3.0. Polynomials

The word “Polynomial” is originated from 2 word – “Poly” and “Nomial”. Poly means “many”,
nominal refer to “terms”. The meaning of polynomial is associated expression that has several
terms. It is defined as a single term or a sum of the finite number of the term. Polynomial can be
operated for addition, subtraction, multiplication, and non-negative number exponents.
Polynomials seem during a wide range of areas of arithmetic and science.

A polynomial is a finite expression constructed from variables and constants, using the operations
of addition, subtraction, multiplication, and taking non-negative integer powers. A polynomial can
be written as the sum of a finite number of terms.

For example; 8x + 7 = 5.

Where,

x = variable

8, 7 & 5 are constant.

Control Engineering Lab


LAB 04

3.1. Define a polynomial in MATLAB and finding its roots

3.2. Multiplications of two polynomials


Create vectors u and v containing the coefficients of the polynomials x2+1 and 2x+7.

Control Engineering Lab


LAB 04

3.3. Finding the factors of polynomial

3.4. Defining a Partial fraction using polynomials


Find the partial fraction expansion of the following ratio of polynomials F(s) using residue.

Control Engineering Lab


LAB 04

3.5. Finding the residue of a P.F. using RESIDUE function


[R,P,K] = RESIDUE(B,A) finds the residues, poles and direct term of a partial fraction expansion
of the ratio of two polynomials B(s)/A(s). If there are no multiple roots,

Vectors B and A specify the coefficients of the numerator and denominator polynomials in
descending powers of s. The residues are returned in the column vector R, the pole locations in
column vector P, and the direct terms in row vector K. The number of poles is n = length(A)-1 =
length(R) = length(P). The direct term coefficient vector is empty if length(B) < length(A),
otherwise length(K) = length(B)-length(A)+1.

Control Engineering Lab


LAB 04

4.0. Exercise

Solution

Control Engineering Lab

You might also like