0% found this document useful (0 votes)
4 views

MATLAB

The document is a summer internship report submitted by Abhishek Kumar for the Bachelor of Technology in Electrical Engineering, detailing his experiences and learning outcomes during the internship. The report covers objectives, course content, experiments conducted using MATLAB, and acknowledgments to faculty and organizations that supported him. It includes practical applications of MATLAB in electrical engineering, particularly in simulating various types of rectifiers and converters.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

MATLAB

The document is a summer internship report submitted by Abhishek Kumar for the Bachelor of Technology in Electrical Engineering, detailing his experiences and learning outcomes during the internship. The report covers objectives, course content, experiments conducted using MATLAB, and acknowledgments to faculty and organizations that supported him. It includes practical applications of MATLAB in electrical engineering, particularly in simulating various types of rectifiers and converters.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

SUMMER INTERNSHIP SUMMARY REPORT

A report submitted in partial fulfilment of the requirements of the Award of


Degree of

BACHELOR OF TECHNOLOGY
in

Electrical Engineering

By

ABHISHEK KUMAR

Roll No. 21328 Reg No. 21103108028

Under Supervision of

AICTE VIRTUAL INTERNSHIP PROGRAM 2024

DEPARTMENT OF ELECTRICAL ENGINEERING


BHAGALPUR COLLEGE OF ENGINEERING
BHAGALPUR, BIHAR-813210
2021-25
DEPARTMENT OF ELECTRICAL ENGINEERING
BHAGALPUR COLLEGE OF ENGINEERING
BHAGALPUR, BIHAR-813210

CERTIFICATE
This is to certify that the “Internship Report” submitted by ABHISHEK KUMAR
(Reg. No.-21103108028) is work done by him and submitted during 2023-24
academic year, in partial fulfilment of the requirements for the award of
the degree of BACHELOR OF TECHNOLOGY in ELECTRICAL ENGINEERING

Prof. DIVYA BHARTI


Internship Coordinator

Assistant Professor , EE

Prof. KN RAM

HOD: ELECTRICAL ENGINEERING


UNDERTAKING

I, ABHISHEK KUMAR son of RAM MANOJ KUMAR SINGH, studying in


Bhagalpur College of Engineering studying in 5th semester , branch
Electrical Engineering having Registration No 21103108028
undertake that I am solely responsible for validity and originality of the
certificate submitted for fulfilment of Summer Enterpreneurship-II

Signature Of the Student


ABHISKEK KUMAR
(21103108028)
ACKNOWLEDGEMENTS

First I would like to thank UDEMY & AICTE for giving me the opportunity to
do an internship within the organisation.

I would also like to thanks all the people who worked along with me and helped
me in the learning of MATLAB SIMULATION at Udemy in learning and
working Environment.

It is indeed with a great sense of pleasure and immense sense of gratitude that I
acknowledge the help of these individuals and Faculty members.

I am highly indebted to Principal Dr. Om Prakash Roy, for the facilities provided
to accomplish this internship.

I would like to thank my Head of the Department Prof. KN Ram Sir for his
constructive criticism throughout my internship.

I would like to thank Prof. Divya Bharti Ma’am Internship Coordinator


Department of EE for their support and advices to get and complete internship in
above said organization.

I am extremely great full to my department staff members and friends who helped
me in successful completion of this internship.

ABHISHEK KUMAR
(21103108028)
Introduction:
Objective of Internship

1) Understanding the MATLAB environment


2) Being able to do simple calculations using MATLAB
3) Being able to carry out simple numerical computations and analyses using
MATLAB
4) The primary objectives of this MATLAB Tools and its Application Internship was
to improve student’s IT/Programming Logic skills and thus improves their
employment potential. Electrical students specially get benefit of this software to
work on Industry.
5) The Internship aims at deliberation, sharing knowledge to the target audience
about the significance of MATLAB Tools and its application and present current
research opportunities in this field.
6) The participants will learn about matrix manipulations, plotting of functions and
data, implementation of algorithms, creation of user interfaces, and interfacing
with programs written in other languages.

Upon successful completion of this Internship, the student should be able to:

1) Understand the main features of the MATLAB development environment


2) Use the MATLAB GUI effectively
3) Design simple algorithms to solve problems
4) Write simple programs in MATLAB to solve scientific and mathematical
problems
5) Know where to find help

Course Content
 Introduction to MATLAB
 Working with Matrices
 Expressions
 Relational and Logical Operations
 Complex and Statistical Functions
 Input / Output of Variables
 Plotting Function
 Flow Control
 MATLAB Simulink Basic
 Applications of MATLAB in Power Electronics and Machine Drive
 Applications of MATLAB in Power and Control System

1
Experiment :
Study of Single Phase Half Wave Controlled Rectifier with R and RL Load
Objective: To Study the simulation of Single Phase Half Wave Controlled Rectifier with R and
RL Load using Simulink.
Software Needed: Matlab R2013a.

Circuit Diagram:

R and RL load

Single Phase Half Wave Controlled Rectifier with R and RL Load

Theory: Single-phase half wave controlled rectifier means that the single SCR is used to convert
the ac to dc. During the positive half cycle of the input voltage, thyristor T1 is forward biased
and current flows through the load when the thyristor is fired, at ԝt=α. The thyristor conducts
only when the anode is positive with respect to cathode and a positive gate signal is applied,
otherwise, it remains in the forward blocking state and blocks the flow of the load current. In the
negative half cycle, i.e., at wt =π, the thyristor is in the reverse biased condition and no current
flows through the load. Thus, varying the firing angle at which the thyristor starts conducting in
positive half controls the average dc output voltage cycle. The waveforms of the above circuit
are shown in fig the output load voltage and current is positive, i.e., they are one quadrant; it is
called a half –wave semi converter.

Procedure:

1. Connect the Circuit as shown in circuit diagram.


2. Give the firing angle from pulse generator to generate suitable pulses.
3. Observe output voltage, current and thyristor voltages from the simuink model.
4. From display values note down the average and rms values of voltage and current .

2
Experimental Matlab
ab Model and Wavforms
Wavforms:

Single Phase Half Wave Controlled Rectifier R load model and waveform for α=30:
α=30

3
Single Phase Half Wave Controlled Rectifier R
Rl load model and waveform for α=30:
α=30

Result: Simulation of Single Phase Half Wave Controlled Rectifier with R and RL load is done
successfully and the theoretical and practical results are verified.

4
Experiment :
Study of Single Phase semi converter with R and RL Load
Objective: To Study the simulation of Single Phase semi converter with R and RL Load using
Simulink.
Software Needed: Matlab R2013a.

Circuit Diagram:

R and RL load

Single Phase semi converter with R and RL Load

Theory: In this configuration two thyristors are replaced by power diodes and can be connected
in either arm of the bridge. Depending on the connections, these are further classified as
1. Symmetrical
2. Asymmetrical
Symmetrical configuration is in two types
1. Common cathode
2. Common anode
Out of these configurations, the common cathode symmetrical configuration is the most
commonly used configuration, because a single trigger can be used to fire both thyristors without
any electrically isolation. During the positive half cycle, when thyristor T1 is triggered, the load
currents flows through T1 and the diode D2 in the circuit shown in figure. During the negative
half –cycle, the thyristor T2 and the diode D1 constitute the load current.

5
Experimental Mat lab Model and Waveforms
Waveforms:

Single Phase Half Wave Controlled Rectifier R load model and waveform for α=30:
α=30

6
Single Phase Half Wave Controlled Rectifier R
Rl load model and waveform for α=30:
α=30

7
Experiment :
Study of Single Phase Bridge Controlled Rectifier with R and RL load
Objective: To Study the simulation of Single Phase Bridge Controlled Rectifier with R and RL
Load using Simulink.

Software Needed: Matlab R2013a.

Circuit Diagram:

R and RL load

Single Phase Bridge Controlled Rectifier with R and RL load

Theory: A single-phase fully controlled bridge circuit consists of four thyristor as shown in
figure, with a resistive load. During the positive half cycle thyristors T1and T2 are in the
forward blocking state and when these thyristors fire simultaneously at wt =α , the load is
connected to the input through T1 and T2. During negative half cycle i.e., after wt =π , thyristor
T3 and T4 are in the forward blocking state, and simultaneous firing of these thyristors reverse
biases the previously conducting thyristors T1 and T2. These reverse biased thyristors turn off
due to line or natural commutation and the load current transfers from T1and T2 to T3 and T4.
Procedure:

1. Connect the Circuit as shown in circuit diagram.


2. Give the firing angle from pulse generator to generate suitable pulses.
3. Observe output voltage, current and thyristor voltages from the simuink model.
4. From display values note down the average and rms values of voltage and current .
5. Compare theoretical and practical values where both calculated and theoretical values
must be same.

8
Experimental Mat lab Model and Waveforms
Waveforms:

Single Phase Bridge Controlled Rectifier R load model and waveform for α=30:
α=30

9
Single Phase Bridge Controlled Rectifier R
RL load model and waveform for α=30:
α=30

10
Result: Simulation of Single Phase Bridge Controlled Rectifier with R and RL load is done
successfully and the theoretical and practical results are verified.

11
Experiment :
Study of Three Phase Semi-Converter with R and RL load
Objective: To Study the simulation of Three Phase Semi-Converter with R and RL Load using
Simulink.

Software Needed: Matlab R2013a.

Circuit Diagram:

R and RL load

Three Phase Semi-Converter with R and RL Load

Theory: The circuit of three phase semi-converter consists of three SCRs and three diodes. The
circuit diagram of three phase semi converter is shown in Figure. The output load voltage Vo
across the load terminals is controlled by varying the firing angles of SCRs T1, T2 and T3. The
diodes D1, D2 and D3 provide merely a return path for the current to the most negative line
terminal. For a firing angle delay of α=0 thyristors T1, T2, T3 would behave as diodes and the
output voltage of semi-converter would be symmetrical six pulse per cycle .The output voltage
consisting of pulses Vcb, Vab, Vac, Vbc. When the firing angle is delayed to α=15 SCRs T1, T2,
T3 is delayed but return diodes D1, D2, D3 remain unaffected so that only alternate pulses are
altered. The load current is continuous and has little ripple. The FD does not come into play for
α=15. Each SCR and diode conduct for 120 degree. Vcb is the load voltage from wt =0 to 60 .
As the first subscript indicates conducting element in the positive group, Vcb shows that T3 is
already conducting through diode B2 of negative group. Voltages Vab, Vac indicate that,

12
Experimental Mat lab Model and Waveforms
Waveforms:

Single Phase Bridge Controlled Rectifier R load model and waveform for α=15:
α=15

13
Single Phase Bridge Controlled Rectifier R
RL load model and waveform for α=15:
α=15

Result: Simulation of Three phase semi semi-converter with R and RL load is done successfully and
the theoretical and practical results are

14
Experiment :
Study of Three Phase Full Bridge Converter with R and RL load
Objective: To Study the simulation of Three Phase Full Bridge Converter with R and RL Load
using Simulink.

Software Needed: Matlab R2013a.

Circuit Diagram:

Three Phase Full Bridge Converter with R and RL load

Theory: For any current to flow in the load at least one device from the top group (T1, T3, T5)
and one from the bottom group (T2, T4, T6) must conduct. It can be argued as in the case of an
uncontrolled converter only one device from these two groups will conduct. Then from
symmetry consideration it can be argued that each thyristor conducts for 120° of the input cycle.
Now the thyristors are fired in the sequence T1 → T2 → T3 → T4 → T5 → T6 → T1 with 60°
interval between each firing. Therefore thyristors on the same phase leg are fired at an interval of
180° and hence can not conduct simultaneously. This leaves only six possible conduction mode
for the converter in the continuous conduction mode of operation. These are T1T2, T2T3, T3T4,

15
16
Three Phase Bridge Controlled Rectifier R
Rl load model and waveform:

17
Experiment :
Study of Three Phase Inverter With 1800 Conduction Mode By Using Matlab
Programming
Objective: To analyze the matlab programming of Three Phase Inverter With 180 0 Conduction
mode

Software Needed: Matlab R2013a.

Circuit Diagram:

Three Phase Inverter With 1800 Conduction Mode

Theory:

In this mode of conduction, every device is in conduction state for 180° where they are switched
ON at 60° intervals. The terminals A, B and C are the output terminals of the bridge that are
connected to the three-phase delta or star connection of the load.

The operation of a balanced star connected load is explained in the diagram below. For the
period 0° − 60° the points S1, S5 and S6 are in conduction mode. The terminals A and C of the
load are connected to the source at its positive point. The terminal B is connected to the source
at its negative point. In addition, resistances R/2 is between the neutral and the positive end
while resistance R is between the neutral and the negative terminal.

18
title('vca')
grid

Output Waveform for 1800 conduction mode:

Result: Three Phase inverter with 1800 Conduction mode by using matlab programming done
successfully and output waveforms obtained.

19
Design of Operational Amplifier Simulator

Abstract

Operational amplifier (op-amp) is widely used in electronic engineering. Therefore their analysis and
synthesis must be thoroughly comprehended. The study, which was carried out .NET and MATLAB
based simulator, is designed for operational amplifier application circuit. Besides basic amplification
operations (phase inverting, phase non-inverting, buffer), the simulator can perform mathematical
operations (summing, subtraction, multiplication, division, differentiation, integration, exponentiation,
logarithm etc) and filtering operation. Due to the interactive interface of modules, simulator can also
be used for educational purposes, analysis and synthesis of op-amp circuits can be realized easily and
greater accuracy.

Key words: Operational amplifier, simulator, .NET, MATLAB

1. Introduction

Operational amplifiers (op-amps) have a very wide range of use which consists of the most
simple amplification operations, mathematical operations, filtering operations, analog-to-digital,
digital-to-analog conversion operations, and signal generators in industrial and scientific fields.
Therefore, main principles of such structures must be completely comprehended.

In the technical field, theoretical information must be used in practical applications for
reinforcing knowledge. But insufficient opportunities for laboratory experiments, absence of
application fields and complexity of the circuits make it difficult. For this reason, instead of
practical implementation, simulations on computers are preferred. In the literature many
simulator studies were performed on different topics in the field of electrical and electronics[1-3].

In this study, a simulator is designed for operational amplifiers. While interface of the simulator
is created using Visual Studio based on .NET [4], analysis are generated using MATLAB [5].
Basic applications of operational amplifiers (amplification, mathematical operations, filtering)
can be interactively performed with the simulator. So the effects of circuit parameters can be
simultaneously observed.

2. Materials and Method

2.1. Operational amplifier

Operational amplifiers are differential amplifiers which have very high input and very low output
impedance (Table 1). They have also very high open loop gain. Operational amplifiers can be
used for different purposes with connecting appropriate components their terminals [6-8].

20
1040

Table 1. Ideal and Non-ideal Op-amp and Some Parameters

Some parameters Ideal op-amp Non-ideal (real) op-amp


Input impedance Infinite Finite
Output impedance Zero Non-zero
Open-loop gain Infinite Finite
Input current Zero Non-zero
Input offset voltage Zero Non-zero
Bandwidth Infinite Finite
Noise Zero Non-zero
CMRR Infinite Finite
Slew rate Infinite Finite

2.2. The designed simulator

In design stage of this simulator, two programming language was used. Main attributes of
operational amplifiers were established in MATLAB. After operational amplifiers basic methods
were written in MATLAB, due to creating interactive interface, the methods were placed in a
class named as “OpampClass” and using deploytool command, the class passed .dll file named
as opamp_lib.dll This file was attend references in the WPF project which was supplied by .NET.
The other .dll file named as MWArray.dll which has directory MATLAB was also joined WPF
project’s reference. In order to disposing “OpampClass” and Matlab data types, required
statements attend to the .NET project’s code. We used two data types in C# for invoking Matlab.
The first and widely used is MWNumericArray which holds matrix information and the second is
MWCharArray which holds string array used for selecting wave shape. The interactive part of
program was designed in .NET using events. For developing interactive interface, necessary
events were declared such as ValueChanged belongs to scrollbar’s event. Whole program was
built with following this programming technique.

Main window of designed simulator is shown in Fig. 1. The following operations can be
performed using these screen menus,
 File: Creating new simulation, Opening a previously saved simulation, saving
simulation which was performed and printing operations.
 Basic Circuits: Phase inverting, phase non-inverting, buffer, current to voltage – voltage
to current conversion operational amplifier applications.
 Mathematical Circuits: Summing, subtraction, multiplication, division, multiplication
by a constant, differentiation, integration, logarithm, antilogarithm (exponentiation)
operational amplifier applications.
 Filter Circuits: Low pass, high pass, band pass and band stop operational amplifier
applications.
 Help: Information about program and operational amplifiers.

21
1041

Figure 1. Main screen of designed simulator

3. Simulations

As first simulation, the steps of summing two AC signals and are


shown in Fig. 2. In program screen, resistors and supply voltages parameters of summing
operational amplifier circuit are entered, the voltage types which will be summed is selected and
the input voltage values are arranged or typed with scroll bar. The simulation is initialized with
clicking start button and input - output voltages can be seen on same screen.

(1)

(a) Main screen of summing amplifier (b) Select voltage type, input amplitudes of voltages and
circuit’s parameters

(c) Result (output) screen of the first simulation


Figure 2. Steps for summing amplifier simulation

As second simulation, derivation of AC signal is shown in Fig. 3.

22
1042

(2)

(a) Main screen of derivative amplifier (b) Select voltage type, input amplitude of voltage and
circuit’s parameters

(c) Result (output) screen of the second simulation


Figure 3. Steps for derivative amplifier simulation

As third simulation, the integration of unit step which has +1V amplitude is shown in Fig. 4.

(3)

(a) Main screen of integrator amplifier (b) Select voltage type, input amplitude of voltage and
circuit’s parameters

23
1043

(c) Result (output) screen of the third simulation


Figure 4. Steps for integrator amplifier simulation

As the last simulation, band pass filter application is given. Signal which comprises 200, 400,
600, 800 and 1000 Hz frequencies and conveys above 60 V amplitude for per frequency
component is supplied to input. According to input the 400 Hz signal component was passed the
filter effectively, the 200 Hz was passed partially and the other components were suppressed
efficiently [9]. The simulator results can be plotted both time domain and frequency domain.

(a) Main screen of band-pass filter simulation (b) Select frequencies and amplitudes of input voltage
and circuit’s parameters

(c) Time domain (d) Frequency domain

24
1044

(e) Bode diagram


Figure 5. Steps for band-pass filter simulation

4. Conclusions

In the study which was carried out, a simulator for basic applications of operational amplifiers
was designed. The analysis and synthesis of operational amplifier circuits can be performed
effectively, efficiently and quickly with this program which can be easily used by people who
study or realizing projects in electric – electronic field. In future work, extending content of the
simulator and making the simulator accessible through network is aimed.

References

[1] Feisel L.D. and Rosa A.J. The role of laboratory in undergraduate engineering education.
Journal of Engineering Education 2005; 121-130.
[2] Balamuralithara B. and Woods P. C. Virtual laboratories in engineering education: The
simulation lab and remote lab. Computer Applications in Engineering Education 2009;
17:108-118.
[3] Sevgi L. Electrical and Computer Engineering Education in the 21st Century: Issues,
Perspectives and Challenges. Turk. J. Elec. Engin. 2006; 14 (1): 1-5.
[4] Microsoft Corporation (http://www.microsoft.com/visualstudio)
[5] MATLAB, The MathWorks, Inc.
[6] http://en.wikipedia.org/wiki/Operational_amplifier
[7] Nilsson J.W. Electric Circuits. Fourth ed. Addison-Wesley Publishing Company; 1993.
[8] Boylestad R. and Nashelsky L. Electronic Devices and Circuit Theory. 5th ed. Prentice Hall;
1992.
[9] Dorf R.C. and Svoboda J.A. Introduction to Electric Circuits. John Wiley & Sons; 2004.

25
Chapter 1

Tutorial lessons 1

1.1 Introduction
The tutorials are independent of the rest of the document. The primarily objective is to help
you learn quickly the first steps. The emphasis here is “learning by doing”. Therefore, the
best way to learn is by trying it yourself. Working through the examples will give you a feel
for the way that MATLAB operates. In this introduction we will describe how MATLAB
handles simple numerical expressions and mathematical formulas.
The name MATLAB stands for MATrix LABoratory. MATLAB was written originally
to provide easy access to matrix software developed by the LINPACK (linear system package)
and EISPACK (Eigen system package) projects.
MATLAB [1] is a high-performance language for technical computing. It integrates
computation, visualization, and programming environment. Furthermore, MATLAB is a
modern programming language environment: it has sophisticated data structures, contains
built-in editing and debugging tools, and supports object-oriented programming. These factors
make MATLAB an excellent tool for teaching and research.
MATLAB has many advantages compared to conventional computer languages (e.g.,
C, FORTRAN) for solving technical problems. MATLAB is an interactive system whose
basic data element is an array that does not require dimensioning. The software package
has been commercially available since 1984 and is now considered as a standard tool at most
universities and industries worldwide.
It has powerful built-in routines that enable a very wide variety of computations. It
also has easy to use graphics commands that make the visualization of results immediately
available. Specific applications are collected in packages referred to as toolbox. There are
toolboxes for signal processing, symbolic computation, control theory, simulation, optimiza-
tion, and several other fields of applied science and engineering.
In addition to the MATLAB documentation which is mostly available on-line, we would

26
recommend the following books: [2], [3], [4], [5], [6], [7], [8], and [9]. They are excellent in
their specific applications.

1.2 Basic features


As we mentioned earlier, the following tutorial lessons are designed to get you started
quickly in MATLAB. The lessons are intended to make you familiar with the basics of
MATLAB. We urge you to complete the exercises given at the end of each lesson.

1.3 A minimum MATLAB session


The goal of this minimum session (also called starting and exiting sessions) is to learn the
first steps:

• How to log on

• Invoke MATLAB

• Do a few simple calculations

• How to quit MATLAB

1.3.1 Starting MATLAB


After logging into your account, you can enter MATLAB by double-clicking on the MATLAB
shortcut icon (MATLAB 7.0.4) on your Windows desktop. When you start MATLAB, a
special window called the MATLAB desktop appears. The desktop is a window that contains
other windows. The major tools within or accessible from the desktop are:

• The Command Window

• The Command History

• The Workspace

• The Current Directory

• The Help Browser

• The Start button

27
Figure 1.1: The graphical interface to the MATLAB workspace

28
Table 1.1: Basic arithmetic operators
Symbol Operation Example
+ Addition 2+3
− Subtraction 2−3
∗ Multiplication 2∗3
/ Division 2/3

1.3.3 Quitting MATLAB


To end your MATLAB session, type quit in the Command Window, or select File −→ Exit
MATLAB in the desktop main menu.

1.4 Getting started


After learning the minimum MATLAB session, we will now learn to use some additional
operations.

1.4.1 Creating MATLAB variables


MATLAB variables are created with an assignment statement. The syntax of variable as-
signment is

variable name = a value (or an expression)

For example,

>> x = expression

where expression is a combination of numerical values, mathematical operators, variables,


and function calls. On other words, expression can involve:

• manual entry

• built-in functions

• user-defined functions

29
Chapter 2

Tutorial lessons 2

2.1 Mathematical functions


MATLAB offers many predefined mathematical functions for technical computing which
contains a large set of mathematical functions.
Typing help elfun and help specfun calls up full lists of elementary and special
functions respectively.
There is a long list of mathematical functions that are built into MATLAB. These
functions are called built-ins. Many standard mathematical functions, such as sin(x), cos(x),
tan(x), ex , ln(x), are evaluated by the functions sin, cos, tan, exp, and log respectively in
MATLAB.
Table 2.1 lists some commonly used functions, where variables x and y can be numbers,
vectors, or matrices.

Table 2.1: Elementary functions


cos(x) Cosine abs(x) Absolute value
sin(x) Sine sign(x) Signum function
tan(x) Tangent max(x) Maximum value
acos(x) Arc cosine min(x) Minimum value
asin(x) Arc sine ceil(x) Round towards +∞
atan(x) Arc tangent floor(x) Round towards −∞
exp(x) Exponential round(x) Round to nearest integer
sqrt(x) Square root rem(x) Remainder after division
log(x) Natural logarithm angle(x) Phase angle
log10(x) Common logarithm conj(x) Complex conjugate

In addition to the elementary functions, MATLAB includes a number of predefined

12

30
2.4 Introduction
Matrices are the basic elements of the MATLAB environment. A matrix is a two-dimensional
array consisting of m rows and n columns. Special cases are column vectors (n = 1) and row
vectors (m = 1).
In this section we will illustrate how to apply different operations on matrices. The following
topics are discussed: vectors and matrices in MATLAB, the inverse of a matrix, determinants,
and matrix manipulation.
MATLAB supports two types of operations, known as matrix operations and array opera-
tions. Matrix operations will be discussed first.

2.5 Matrix generation


Matrices are fundamental to MATLAB. Therefore, we need to become familiar with matrix
generation and manipulation. Matrices can be generated in several ways.

2.5.1 Entering a vector


A vector is a special case of a matrix. The purpose of this section is to show how to create
vectors and matrices in MATLAB. As discussed earlier, an array of dimension 1 × n is called
a row vector, whereas an array of dimension m × 1 is called a column vector. The elements
of vectors in MATLAB are enclosed by square brackets and are separated by spaces or by
commas. For example, to enter a row vector, v, type

>> v = [1 4 7 10 13]
v =
1 4 7 10 13

Column vectors are created in a similar way, however, semicolon (;) must separate the
components of a column vector,

>> w = [1;4;7;10;13]
w =
1
4
7
10
13

On the other hand, a row vector is converted to a column vector using the transpose operator.
The transpose operation is denoted by an apostrophe or a single quote (’).

19

31
>> w = v’
w =
1
4
7
10
13

Thus, v(1) is the first element of vector v, v(2) its second element, and so forth.
Furthermore, to access blocks of elements, we use MATLAB’s colon notation (:). For exam-
ple, to access the first three elements of v, we write,

>> v(1:3)
ans =
1 4 7

Or, all elements from the third through the last elements,

>> v(3,end)
ans =
7 10 13

where end signifies the last element in the vector. If v is a vector, writing

>> v(:)

produces a column vector, whereas writing

>> v(1:end)

produces a row vector.

2.5.2 Entering a matrix


A matrix is an array of numbers. To type a matrix into MATLAB you must

• begin with a square bracket, [

• separate elements in a row with spaces or commas (,)

• use a semicolon (;) to separate rows

• end the matrix with another square bracket, ].

20

32
Here is a typical example. To enter a matrix A, such as,
 
1 2 3
A= 4 5 6  (2.1)
7 8 9
type,

>> A = [1 2 3; 4 5 6; 7 8 9]

MATLAB then displays the 3 × 3 matrix as follows,

A =
1 2 3
4 5 6
7 8 9

Note that the use of semicolons (;) here is different from their use mentioned earlier to
suppress output or to write multiple commands in a single line.
Once we have entered the matrix, it is automatically stored and remembered in the
Workspace. We can refer to it simply as matrix A. We can then view a particular element in
a matrix by specifying its location. We write,

>> A(2,1)
ans =
4

A(2,1) is an element located in the second row and first column. Its value is 4.

2.5.3 Matrix indexing


We select elements in a matrix just as we did for vectors, but now we need two indices.
The element of row i and column j of the matrix A is denoted by A(i,j). Thus, A(i,j)
in MATLAB refers to the element Aij of matrix A. The first index is the row number and
the second index is the column number. For example, A(1,3) is an element of first row and
third column. Here, A(1,3)=3.
Correcting any entry is easy through indexing. Here we substitute A(3,3)=9 by
A(3,3)=0. The result is

>> A(3,3) = 0
A =
1 2 3
4 5 6
7 8 0

21

33
Single elements of a matrix are accessed as A(i,j), where i ≥ 1 and j ≥ 1. Zero or negative
subscripts are not supported in MATLAB.

2.5.4 Colon operator


The colon operator will prove very useful and understanding how it works is the key to
efficient and convenient usage of MATLAB. It occurs in several different forms.
Often we must deal with matrices or vectors that are too large to enter one ele-
ment at a time. For example, suppose we want to enter a vector x consisting of points
(0, 0.1, 0.2, 0.3, · · · , 5). We can use the command

>> x = 0:0.1:5;

The row vector has 51 elements.

2.5.5 Linear spacing


On the other hand, there is a command to generate linearly spaced vectors: linspace. It
is similar to the colon operator (:), but gives direct control over the number of points. For
example,

y = linspace(a,b)

generates a row vector y of 100 points linearly spaced between and including a and b.

y = linspace(a,b,n)

generates a row vector y of n points linearly spaced between and including a and b. This is
useful when we want to divide an interval into a number of subintervals of the same length.
For example,

>> theta = linspace(0,2*pi,101)

divides the interval [0, 2π] into 100 equal subintervals, then creating a vector of 101 elements.

2.5.6 Colon operator in a matrix


The colon operator can also be used to pick out a certain row or column. For example, the
statement A(m:n,k:l specifies rows m to n and column k to l. Subscript expressions refer
to portions of a matrix. For example,

22

34
>> A(2,:)
ans =
4 5 6

is the second row elements of A.


The colon operator can also be used to extract a sub-matrix from a matrix A.

>> A(:,2:3)
ans =
2 3
5 6
8 0

A(:,2:3) is a sub-matrix with the last two columns of A.


A row or a column of a matrix can be deleted by setting it to a null vector, [ ].

>> A(:,2)=[]
ans =
1 3
4 6
7 0

2.5.7 Creating a sub-matrix


To extract a submatrix B consisting of rows 2 and 3 and columns 1 and 2 of the matrix A,
do the following

>> B = A([2 3],[1 2])


B =
4 5
7 8

To interchange rows 1 and 2 of A, use the vector of row indices together with the colon
operator.

>> C = A([2 1 3],:)


C =
4 5 6
1 2 3
7 8 0

It is important to note that the colon operator (:) stands for all columns or all rows. To
create a vector version of matrix A, do the following

23

35
>> A(:)
ans =
1
2
3
4
5
6
7
8
0

The submatrix comprising the intersection of rows p to q and columns r to s is denoted by


A(p:q,r:s).
As a special case, a colon (:) as the row or column specifier covers all entries in that row or
column; thus

• A(:,j) is the jth column of A, while

• A(i,:) is the ith row, and

• A(end,:) picks out the last row of A.

The keyword end, used in A(end,:), denotes the last index in the specified dimension. Here
are some examples.

>> A
A =
1 2 3
4 5 6
7 8 9

>> A(2:3,2:3)
ans =
5 6
8 9

>> A(end:-1:1,end)
ans =
9
6
3

24

36
>> A([1 3],[2 3])
ans =
2 3
8 9

2.5.8 Deleting row or column


To delete a row or column of a matrix, use the empty vector operator, [ ].

>> A(3,:) = []
A =
1 2 3
4 5 6

Third row of matrix A is now deleted. To restore the third row, we use a technique for
creating a matrix

>> A = [A(1,:);A(2,:);[7 8 0]]


A =
1 2 3
4 5 6
7 8 0

Matrix A is now restored to its original form.

2.5.9 Dimension
To determine the dimensions of a matrix or vector, use the command size. For example,

>> size(A)
ans =
3 3

means 3 rows and 3 columns.


Or more explicitly with,

>> [m,n]=size(A)

25

37
Chapter 3

Array operations and Linear


equations

3.1 Array operations


MATLAB has two different types of arithmetic operations: matrix arithmetic operations
and array arithmetic operations. We have seen matrix arithmetic operations in the previous
lab. Now, we are interested in array operations.

3.1.1 Matrix arithmetic operations


As we mentioned earlier, MATLAB allows arithmetic operations: +, −, ∗, and ˆ to be
carried out on matrices. Thus,

A+B or B+A is valid if A and B are of the same size


A*B is valid if A’s number of column equals B’s number of rows
A^2 is valid if A is square and equals A*A
α*A or A*α multiplies each element of A by α

3.1.2 Array arithmetic operations


On the other hand, array arithmetic operations or array operations for short, are done
element-by-element. The period character, ., distinguishes the array operations from the
matrix operations. However, since the matrix and array operations are the same for addition
(+) and subtraction (−), the character pairs (.+) and (.−) are not used. The list of array
operators is shown below in Table 3.2. If A and B are two matrices of the same size with
elements A = [aij ] and B = [bij ], then the command

30

38
.* Element-by-element multiplication
./ Element-by-element division
.^ Element-by-element exponentiation

Table 3.1: Array operators

>> C = A.*B

produces another matrix C of the same size with elements cij = aij bij . For example, using
the same 3 × 3 matrices,
   
1 2 3 10 20 30

A= 4 5 6 ,  
B = 40 50 60 
7 8 9 70 80 90

we have,

>> C = A.*B
C =
10 40 90
160 250 360
490 640 810

To raise a scalar to a power, we use for example the command 10^2. If we want the
operation to be applied to each element of a matrix, we use .^2. For example, if we want
to produce a new matrix whose elements are the square of the elements of the matrix A, we
enter

>> A.^2
ans =
1 4 9
16 25 36
49 64 81

The relations below summarize the above operations. To simplify, let’s consider two
vectors U and V with elements U = [ui ] and V = [vj ].

U. ∗ V produces [u1 v1 u2 v2 . . . un vn ]
U./V produces [u1 /v1 u2 /v2 . . . un /vn ]
U.ˆV produces [uv11 uv22 . . . uvnn ]

31

39
Operation Matrix Array

Addition + +
Subtraction − −
Multiplication ∗ .∗
Division / ./
Left division \ .\
Exponentiation ˆ .ˆ

Table 3.2: Summary of matrix and array operations

3.2 Solving linear equations


One of the problems encountered most frequently in scientific computation is the solution of
systems of simultaneous linear equations. With matrix notation, a system of simultaneous
linear equations is written
Ax = b (3.1)
where there are as many equations as unknown. A is a given square matrix of order n, b is a
given column vector of n components, and x is an unknown column vector of n components.
In linear algebra we learn that the solution to Ax = b can be written as x = A−1 b, where
A−1 is the inverse of A.
For example, consider the following system of linear equations

 x + 2y + 3z = 1
4x + 5y + 6z = 1

7x + 8y = 1
The coefficient matrix A is
   
1 2 3 1
A= 4 5 6  and the vector b =  1 
7 8 9 1

With matrix notation, a system of simultaneous linear equations is written

Ax = b (3.2)

This equation can be solved for x using linear algebra. The result is x = A−1 b.
There are typically two ways to solve for x in MATLAB:

1. The first one is to use the matrix inverse, inv.

32

40
>> A = [1 2 3; 4 5 6; 7 8 0];
>> b = [1; 1; 1];
>> x = inv(A)*b
x =
-1.0000
1.0000
-0.0000

2. The second one is to use the backslash (\)operator. The numerical algorithm behind
this operator is computationally efficient. This is a numerically reliable way of solving
system of linear equations by using a well-known process of Gaussian elimination.

>> A = [1 2 3; 4 5 6; 7 8 0];
>> b = [1; 1; 1];
>> x = A\b
x =
-1.0000
1.0000
-0.0000

This problem is at the heart of many problems in scientific computation. Hence it is impor-
tant that we know how to solve this type of problem efficiently.
Now, we know how to solve a system of linear equations. In addition to this, we will
see some additional details which relate to this particular topic.

3.2.1 Matrix inverse


Let’s consider the same matrix A.
 
1 2 3
A= 4 5 6 
7 8 0

Calculating the inverse of A manually is probably not a pleasant work. Here the hand-
calculation of A−1 gives as a final result:
 
−16 8 −1
1
A−1 =  14 −7 2 
9
−1 2 −1

In MATLAB, however, it becomes as simple as the following commands:

33

41
>> A = [1 2 3; 4 5 6; 7 8 0];
>> inv(A)
ans =
-1.7778 0.8889 -0.1111
1.5556 -0.7778 0.2222
-0.1111 0.2222 -0.1111

which is similar to:  


−16 8 −1
1
A−1 =  14 −7 2 
9
−1 2 −1
and the determinant of A is

>> det(A)
ans =
27

For further details on applied numerical linear algebra, see [10] and [11].

3.2.2 Matrix functions


MATLAB provides many matrix functions for various matrix/vector manipulations; see
Table 3.3 for some of these functions. Use the online help of MATLAB to find how to use
these functions.

det Determinant
diag Diagonal matrices and diagonals of a matrix
eig Eigenvalues and eigenvectors
inv Matrix inverse
norm Matrix and vector norms
rank Number of linearly independent rows or columns

Table 3.3: Matrix functions

3.3 Exercises
Note: Due to the teaching class during this Fall Quarter 2005, the problems are temporarily
removed from this section.

34

42
Appendix A

Summary of commands

Table A.1: Arithmetic operators and special characters


Character Description

+ Addition
− Subtraction
∗ Multiplication (scalar and array)
/ Division (right)
ˆ Power or exponentiation
: Colon; creates vectors with equally spaced elements
; Semi-colon; suppresses display; ends row in array
, Comma; separates array subscripts
... Continuation of lines
% Percent; denotes a comment; specifies output format
0
Single quote; creates string; specifies matrix transpose
= Assignment operator
() Parentheses; encloses elements of arrays and input arguments
[] Brackets; encloses matrix elements and output arguments

53

43
Table A.2: Array operators
Character Description

.∗ Array multiplication
./ Array (right) division
.ˆ Array power
.\ Array (left) division
.0 Array (nonconjugated) transpose

Table A.3: Relational and logical operators


Character Description

< Less than


≤ Less than or equal to
> Greater than
≥ Greater than or equal to
== Equal to
∼= Not equal to
& Logical or element-wise AND
| Logical or element-wise OR
&& Short-circuit AND
|| Short-circuit OR

54

44
Table A.4: Managing workspace and file commands
Command Description

cd Change current directory


clc Clear the Command Window
clear (all) Removes all variables from the workspace
clear x Remove x from the workspace
copyfile Copy file or directory
delete Delete files
dir Display directory listing
exist Check if variables or functions are defined
help Display help for MATLAB functions
lookfor Search for specified word in all help entries
mkdir Make new directory
movefile Move file or directory
pwd Identify current directory
rmdir Remove directory
type Display contents of file
what List MATLAB files in current directory
which Locate functions and files
who Display variables currently in the workspace
whos Display information on variables in the workspace

Table A.5: Predefined variables and math constants


Variable Description

ans Value of last variable (answer)


eps Floating-point relative accuracy
i Imaginary unit of a complex number
Inf Infinity (∞)
eps Floating-point relative accuracy
j Imaginary unit of a complex number
NaN Not a number
pi The number π (3.14159 . . .)

55

45
Bibliography

[1] The MathWorks Inc. MATLAB 7.0 (R14SP2). The MathWorks Inc., 2005.

[2] S. J. Chapman. MATLAB Programming for Engineers. Thomson, 2004.

[3] C. B. Moler. Numerical Computing with MATLAB. Siam, 2004.

[4] C. F. Van Loan. Introduction to Scientific Computing. Prentice Hall, 1997.

[5] D. J. Higham and N. J. Higham. MATLAB Guide. Siam, second edition edition, 2005.

[6] K. R. Coombes, B. R. Hunt, R. L. Lipsman, J. E. Osborn, and G. J. Stuck. Differential


Equations with MATLAB. John Wiley and Sons, 2000.

[7] A. Gilat. MATLAB: An introduction with Applications. John Wiley and Sons, 2004.

[8] J. Cooper. A MATLAB Companion for Multivariable Calculus. Academic Press, 2001.

[9] J. C. Polking and D. Arnold. ODE using MATLAB. Prentice Hall, 2004.

[10] D. Kahaner, C. Moler, and S. Nash. Numerical Methods and Software. Prentice-Hall,
1989.

[11] J. W. Demmel. Applied Numerical Linear Algebra. Siam, 1997.

[12] D. Houcque. Applications of MATLAB: Ordinary Differential Equations. Internal


communication, Northwestern University, pages 1–12, 2005.

64

46

You might also like