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

INDEX

S. No Topic Page No.


Week 1
1 Course Introduction 01
2 Lecture 1.1: Basics of Programming using MATLAB 06
Week 2
3 Lec 1.2: Array Operations in MATLAB 21
4 Lec 1.3: Loops and Execution Control 31
5 Tutorial: Using Arrays 39
6 Lec 1.4: MATLAB Files -- Scripts and Functions 49
7 Lec 1.5: Plotting and Output 61
Week 3
8 How to submit MATLAB Assignment 73
9 Lec 2.1: Errors in Numerical Computation 77
10 Lec 2.2: Truncation Errors and Taylors Series 86
11 Lec 2.3: Round-Off Errors; and Iterative Methods 93
12 Lec 2.4: Step-wise Methods and Error Propagation 102
Week 4
How to get MATLAB Online access (for all enrolled students of
13 111
this course)
14 Numerical Differentiation – Differentiation in Single variable 113
15 Lec 3.2: Higher Order Differentiation Formulae 121
16 Lec 3.3: Partial Differentials (Bonus) 127
17 Lec 3.4: Numerical Integration 134
18 Lec 3.5: Multiple Applications of Integration Formulae 144

19 Lec 3.6: In-Build MATLAB Integration Functions 151

Week 5
20 Lec 4.1: Basics of Linear Algebra 158
21 Lec 4.2: Gauss Elimination and Back-Substitution 167
22 Lec 4.3: LU Decomposition and Partial Pivoting 176
23 Lec 4.4: Gauss Siedel Method 183
24 Lec 4.4 (Tutorial) 190
25 Lec 4.5: Tri-Diagonal Matrix Algorithm 193
Week 6
26 Lec 5.1: Nonlinear Equations in Single Variable 204
27 Lec 5.2: Using MATLAB command fzero 213
28 Lec 5.3: Fixed Point Iteration in Single Variable 220
29 Lec 5.4: Newton-Raphson (single variable) 228

30 Lec 5.5: Using MATLAB command fsolve (multi-variable) 235

31 Lec 5.6: Newton-Raphson (multi Variable) 242


Week 7
32 Lec 6.1: Introduction 247

33 Lec 6.2: Linear Least Squares Regression 255

34 Lec 6.3: Nonlinear and Functional Regression 262


35 Lec 6.4: Interpolation Functions in MATLAB 271
Week 8
36 Lecture 7.1: Introduction and Euler\'s Method 279
37 Lecture 7.2: Runge-Kutta (RK-2) method 288

38 Lecture 7.3: MATLAB ode45 algorithm 293

39 Lecture 7.4: Higher order Runge-Kutta Methods 299


40 Lecture 7.5: Error Analysis 304
Week 9
41 Lecture 8.1: Multi-Variable ODE 310
42 Lecture 8.2: Stiff Systems & Solution using ode15s 318
43 Lecture 8.3: Method of Lines for transient PDEs 325
44 Lecture 8.4: A Final Example 330
45 Tutorial: How to do linear and nonlinear regression 338
Matlab Programming For Numerical Computations
Dr.Niket Kaisare
Department of Chemical engineering
Indian Institute of Technology, Madras

Introduction to MATLAB programming

Hello and welcome to this course on Matlab Programming for Numerical Computations. My name is Dr.
Niket Kaisare, I am from Department of Chemical Engineering at IIT, Madras. This course primarily
focuses on using Matlab for Numerical Programming basically for Numerical Programming that is of
interest to engineers.
(Refer Slide Time 00:35)

The learning objectives for this course are listed over here. Primarily, this is a Matlab programming
course, we are going to start off with basics of Matlab programming.
So, we are going to use this course to understand the numerical methods in context of how we are going
to use them in Matlab. And, we are then going to use Matlab to solve some of these computation
problems.

1
(Refer Slide Time 00:58)

About Matlab, Matlab is a high level programming language, it is widely used for computation analysis
and controls, signal processing, communications and other fields. Matlab is a combination of the words
matrix and laboratory. The link for Matlab website is given over here it is called mathworks.com. Matlab
is one of the more popular computational software, commercial computational software out there.

(Refer Slide Time 01:27)

2
What are the prerequisites for this course, since this course is an introductory course on Matlab
programming there are not too many prerequisites. We will basically assume some basics of linear
algebra and calculus, that is usually covered in your first year under graduate. Besides, that are no other
prerequisites for the course.

However, some of the other things which are good to have would be some background or some
knowledge of computational or numerical techniques would be useful. Some prior background in
programming will be useful. But, again in this course we are not going to make any assumptions with
respect to prior background in programming.

(Refer Slide Time 02:13)

The software that will be used in this course is primarily, Matlab again the website for Matlab is given
over here. If Matlab is not available in your college you can alternatively use Scilab. Scilab is an open
source software that is somewhat similar to Matlab. Most of the things that we are going to cover in this
course are also available in Scilab.

And, if Matlab is not available to you I highly recommend downloading the free software Scilab and
trying out the examples using Scilab. There are some minor differences between Matlab and Scilab. But

3
the primary use of Scilab is similar to Matlab. Assignment problems will be set using Matlabs own
course work website. The link for the website will be given to you in the due time. We will use this
website in order to have assignment problems given to registered student on this course.

(Refer Slide Time 3:16)

The book that I highly recommend for this course is the book by Professor, Laurene Fausett called
Numerical Methods using Matlab. It is a book that we are primarily going to follow in this course. Other
recommended text books are the, text books on Numerical Methods by Professor, Chapra and Professor,
Canale. And, the Numerical Methods for Chemical Engineering text book by Professor, S. K. Gupta.

This course is general course, general purpose course for a people with Engineering and Science
background. So we are going to take up examples, which are general in nature. A related course to this
course is the Computational Techniques Course where I have covered theoretical aspects of numerical
methods that we are going to cover in this Matlab programming course.

However, we are not going to use any of the theoretical aspects of this computational methods. We are
primarily going to study, how to implement this computational methods in Matlab. With this I come to

4
the end of this introduction. I hope you will follow this course in order to learn Matlab programming.
Thank you, and see you in module one. Bye.

5
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department Of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #01


Lecture No. #1.1
Introduction to MATLAB programming – Basics of MATLAB
Hello and welcome to module 1 of introduction to MATLAB Programming course. In this
module we are primarily going to go over basics of MATLAB and introduction to MATLAB
programming, how the MATLAB is laid out and so on ok. So, this is the first lecture in this
module.
(Refer Slide Time 00:30)

We will cover this module in approximately 5 lectures. The first lecture is on MATLAB Basics
(Refer Slide Time 00:37)

6
Now this module overall is going to cover after MATLAB Basics, we are going to cover arrays.
So that is the main aspect of working in MATLAB, basically arrays and matrices. So that is the
second lecture in this particular module that we are going to cover. There after we are going to
look at loops and how to use loops, for loops and while loops in order to do various
computations.

That will be followed by talking about MATLAB files, MATLAB scripts and functions and how
to use MATLAB files which will require a lot in the rest of the course and finally will talk about
plotting and outputs in using MATLAB ok.
(Refer Slide Time 01:22)

7
Alright so we will go over how to start a MATLAB session, the layout of the MATLAB window,
MATLAB editor and so on and so forth. All of these is also available on MATLAB’s website.
The link for which is given right over here. So you can also, in addition to this video lecture I
encourage you to also go to MATLAB website and look at the video “Getting started with
MATLAB”. And that is what we are going to cover in this particular lecture. So let us go and
open the MATLAB.

(Video Starts: 01:56)


So, I have just created a short cut over here for MATLAB in your case you may have to open it
using programming files ok. So, this is how my MATLAB window looks like when I open
MATLAB. Your MATLAB window may look slightly different and however the main
components of the window will still remain the same. So, this window has several components.
The first is the most important one is what is known as the command window.

So, that is, so lots of times people just display command window and nothing else that is also
fine. I like to display my current folder as well as the workspace, workspace is nothing but the
variables that you currently have in the system, defined in system ok. You can move all these
things around by just clicking and dragging and as you can see, you can drag it at various
location. So, if you drag it and put it on the current folder itself.

You will basically have the 2 current folders and workspace as 2 tabs, ok. And again you can
drag this workspace back over here and you can place it next to the command window then we
can have current folder workspace command window and so on. Again as I said I like to place
the workspace variables and my current folder next to each other or below each other to the left
hand side and that is what we will do, again you can resize all these windows as you find fit ok.

So, let us, now, this is the overall layout of any MATLAB window that you might have. What
will do, go and do is will start a MATLAB editor as well in order to do that you give a command
edit space followed by the file name, so you will call this my first file and I press enter and we
will be able to open my first file and this a blank file in MATLAB editor. This is where you can

8
put in all your commands all your functions and expressions assignments so on and you can use
MATLAB.
So that is what I just wanted to give an introduction to MATLAB now let us look at the various
MATLAB files that I have already created in my folder. So currently in my folder I have 3
MATLAB files ball animation, ball trajectory and ball trajectory fun that is ball trajectory
function. If I go to MATLAB window, I cannot see all these 3 files and that is because am in a
different directory ok.

So, I can change my directory using the cd command change directory command or I can go and
because I have used to this particular, been to this particular directory, before I can look at that in
my history. So I will just select from the history and now I am able to see all these files ok. So let
me just go and open ball trajectory file by double click on it and this is how my file looks like I
will go over the various basics of this particular file in the PowerPoint again ok.
(Video Ends 05:38)
(Refer Slide Time 05:40)

Ok this particular file uses the following example. This is an example of the famous cricketer,
current captain of Indian team Mahendra Singh Dhoni trying to hit a ball for a six, Dhoni hits
ball with an initial velocity of 35 meters per second at an angle of 45 degrees. And the question
that we are asking ourselves, I have, which will solve in MATLAB is whether the ball is able to

9
cross the boundary rope that is 75 meters from where Dhoni is standing in order for this to be
registered as a 6.
So, we want to find out whether the ball lands before 75 meters or after the 75 meter line and we
are going to solve this particular problem using the simple equations that we know of and those
equations are shown over here. Distance dx / dt depends on the horizontal velocity, dy by dt
depends on the vertical velocity. The acceleration in the horizontal direction is the one by the air
drag and that in the vertical direction is the one by the gravitational force ok. So these are the
equations that we have ok.
(Refer Slide Time 06:54)

And when we put this in MATLAB and we plot we are going to get a plot of this nature this is
how ball trajectory looks like at the end of every second or at the end of every .1 seconds, I am
sorry and the ball lands approximately 80 meters from where Dhoni is standing. So, indeed this
ends up being a six.

10
(Refer Slide Time 07:16)

And the MATLAB code that a quickly shown you earlier. We will go over that in a minute. And I
will show.

(Video Starts 07:26). How this MATLAB code runs. In order to run this MATLAB code, what
we need to do is type the file name ball trajectory over here and press enter. And this is going to
run and what have done is also done as small animation of how the ball moves and lands at
approximately 81 meters ok.

Once we have run this file, this is what we are able to see in the workspace. All the workspace
variables we are now able to see over here. And what we see over here is that the function has
existed and we have come back to the command prompt ok. (Video Ends 08:12)

Let us go back to what the MATLAB code looks like. And this is how the MATLAB code looks
like the green, green things that you see over here are basically comments that we have entered
in MATLAB and am going to go over the various parts of the MATLAB.

11
(Refer Slide Time 08:27)

The over, overall structure of this code looks something like this. We first have this input block
where we are defining parameters and initial conditions. We have the computation block where
we are setting up the ordinary differential equations and solving them. And finally we have the
output block where we are displaying the results and where we are showing the animation.

The animation is not going to be covered in this particular course we are going to cover rest of
this stuff in this course we will go over the things in the input block computation block and
output block the name parts of it
(Refer Slide Time 09:04)

12
So, what are the main parts been highlighted over here?
(Refer Slide Time 09:10)

The first one is a comment. A comment starts with a percentage sign and it is colored green in
MATLAB editor window. So, the comment that starts with 2 percentage signs are basically they
mark start and end of a section in MATLAB. This is nothing but sectioning just to identify for
humans MATLAB ignores this particular command it is just for us to identified the code or
structure the code in a better way.
(Refer Slide Time 09:39)

13
The next thing is that we see over here is an assignment. Assignment is basically you have a
variable and constant is been assigned to the variable instead of a constant we can assign any
expression to the variable also.
(Refer Slide Time 09:52)

This is a mathematical expression 35*cos (pi/4). That is the mathematical expression over here.
We will go over what variable declarations and mathematical expressions are in this particular
module in lecture 1.2.
(Refer Slide Time 10:09)

Over here, what we see is a function. This function is a plot function and we are calling this
function with certain set of arguments. Again I am not going to go over this in this particular

14
lecture, we will go over that later on in this module. In this particular example we have called
function and we are not worried about the outputs of that functions. So, we are just calling this
function as is, if you want to capture the outputs of this function, we are going to call the
function and capture the output as shown over here.
(Refer Slide Time 10:36)

Again these are things that we will go over the MATLAB basics and introduction to MATLAB in
this particular module in the subsequent lectures.
(Refer Slide Time 10:52)

Ok. So, this again was the overall MATLAB code we are going to cover various things that are
involve in coding of this MATLAB in this particular course.

15
(Refer Slide Time 11:03)

Ok so, now let us go over what the basic data types of MATLAB are. Now the 2 main data types
in MATLAB are going to be scalars and matrices or arrays or vectors. So, when we talk about
scalars, scalars are nothing but a single valued variable where as arrays or matrices are going to
be of size m by n where m is the number of rows and n is the number of columns.
So, we will go over some of these basic things in this particular lecture and in the subsequent
lectures in this module.

Ok. The most powerful thing about MATLAB is that it works with matrices and that is what mat
in MATLAB stands for MATLAB. Again we are going to look at how to assign variables, how to
assign scalars, how to assign vectors, how to assign arrays and matrices and that is shown over
here. a is the scalar, b is what is known as row vector, c is the matrix over here. And d is what is
known as the column vector. So, let us go and type these things in MATLAB and see how that
works.

(Video Starts 12:17) So, let us say an equal to square bracket 1, 2, 5. If we type this, close this
square bracket and press enter. We will now have a vector a. This vector a has 1 row and 3
columns over here and that is shown as a row vector as seen here ok. If we type b =1; 2; 5, we
are now instead going to get a column vector. So, that has 3 rows and 1 single column as shown
over here.

16
So, the commas are going to separate various elements in a particular row where are semicolon is
going to separate various rows. So, that way we can build a matrix as shown over here 1, 2 that
means thus this is the first row 3, 5 that is the second row, minus 1, 0 that becomes the third row
so we have 3 rows and 2 columns in this particular matrix.

And this is what we can see over here. So and now let us say we wanted to define a scalar. So let
us say q = 5 that becomes a scalar ok. As you can see over here, every time I type a command the
results of the command get displayed in MATLAB window. I will just clear this MATLAB
window, so if we do not want to display, then we can basically type sorry, we can basically type
this a.

And we can end it with a semicolon. So if line is ended with a semicolon, the echo is been
suppressed. As you can see the a, the variable gets assigned to the matrix a, however we do not
see the result on the screen. If you want to see the result on the screen, we can just type a press
enter and we will be able to see result. Another thing we can do is go to this workspace, click on,
double click on a and we will be able to see the contents of this particular variable a ok.

And you can again close this particular window, so that we can get back our command window
as before ok. So, this is how to work with your MATLAB window. (Video Ends 14:49)

17
(Refer Slide Time 14:53)

Let us go over the basic mathematical expressions. So, we want to look at mathematical
expressions that work with scalars as well as mathematical expressions that are going to work
with vectors and matrices. So, first we will go over the various scalar operations.
So, let us look at the scalar operations over here. The standard scalar operations +, -,*, / and ^
that is caret. In addition to that, logarithm and exponent are the one shown in the second line,
power, square root, sin, cos, tan, asin, acos, atan, remainder, round, ceiling, floor these are the
various scalar operations and we have special variables that are shown to the right hand side. So,
let us look at how these variable operations works.

(Video Starts: 15:43) So, let us say we have so, 3 + 4 and that is 7. So, MATLAB has computed
value 7 and is sight, it has assign that value to a special variable called ans. So, if an expression
is calculated and you do not assign it to any variable, MATLAB automatically assigns it to a
special variable called ans ok.

Let us now look at the matrix that we had a. So, if we were to multiply a with a scalar, let us say
2 multiplied by a, what is going to happen is that each individual element of a will get multiplied
with by that particular scalar we have over here. And this is going to be the result 2, 4 and 10 as
we expect the result to be.

18
We had shown certain other commands that was so exp that is exponential. So, if we give exp of
a, this is what we will get as you can see. This is an exponent has acted upon each and individual
element of a. So, we get exp or e ^ 1, e ^ 2 and e ^ 5 that because a was 1, 2, 5 and we get back a
row vector as seen in the result over here ok.

If we were to do exp of b, note that b was a column vector, so if you were to do exp of b, we will
get a column vector as a result of this. So we have this. The result is going to be a column vector
with 3 rows and 1 single column and because we had not assigned it to any variable. The special
variable ans gets assigned that particular variable ok.

So, talking about special variables there are certain other special variables. For example, the
value of pi, if you type pi and press enter, we will get the value of pi. Likewise, there are special
other variables also that we have in this particular power point slide. (Video Ends 17:51)

So, eps is the machine precision I is the imaginary unit that is square root of minus 1, inf is an
infinity nan is not a number, ans, we have already seen before and the other couple of. There is
one more thing that you need to know.

(Video Starts 18:15) So, let us say if I assign the value pi = 4, if I do that what is going to happen
is, from this point onwards the standard value of pi is going to be overridden by my expression.
So, next time when I calculate say pi/ 4, I am not going to get pi/ 4 but I am going to get 4/ 4 that
is 1.

That is because I have overridden the value of pi by assigning the value over here. If you want to
cancel this all, I have to do is clear that variable by using the command clear space pi and I am
now able to recover the value of the standard value of the pi again that is 3. 1416 ok. Now if I
want to clear all the variables in the workspace, I have to give the command clear or I have to
give the command clear all.

That means everything in the workspace is going to be cleared. When I give this command;
everything in the workspace gets cleared as you can see over here. And I can give the command

19
clc to clean the screen and my command prompt comes back to the top with completely clean
screen as you can see over here ok. (Video Ends 19:28)

(Refer Slide Time 19:29) So, let us go back to the basic mathematical expressions. We have seen
mathematical expressions for scalars and with that we will come to the end of this particular
lecture ok. As I said earlier, one of the most powerful features of MATLAB is that it is going to
be able work with matrices and arrays and that is what we are going to cover in the next lecture
in this module that is lecture 1.2 ok. Thank you and see you in the lecture 1.2.

20
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. # 01
Lecture No. #1.2
Introduction to MATLAB programming – Arrays and Matrix Operations In MATLAB

Hello and welcome to the MATLAB programming NPTEL course, we are in module 1 which is
introduction to MATLAB. Lecture 1.2 is going to cover arrays and matrix operations in
MATLAB. In the previous lecture we had given an overview of what MATLAB is.
(Refer Slide Time 00:29)

In this particular lecture, we will learn how to build arrays which we had started off in the
previous lecture itself. We will go over that again. We will talk about colon notations and we will
talk about array operations and array functions. As before there is video in, on the MATLAB
website that you can watch the link for which is provided over here.

The video is called working with arrays in MATLAB. And I suggest that you also look at this
video in addition to this NPTEL lecture okay. So let us look at building of MATLAB arrays and
vectors okay.

21
(Video Starts: 01:09) In the previous lecture, we had look at how to build row vector. If we have
row separated, elements of the row separated by commas, we are going to have a row vector and
if instead we were to separate by semicolon, we are going to have a column vector as shown over
here okay. So, I am getting an error the reason why I get an error is, because I forgot to put a
semicolon over here.

So, it is trying to build a vector by having a 1 in the first row and in the second row, we have 2
elements and we cannot build a vector of that size, so instead if we were to have semicolons as
shown over here we are going to get a column vector as seen over here. That leads me to next
thing and that is, that we do not have to build vectors and arrays by having each and individual
elements we can use previously defined vectors and arrays to build a particular matrix also.
So, let us say we were to define c as part, as let say identity matrix of size 3 okay. So, we will get
c as eye 3, eye is command in MATLAB that will build an identity matrix okay. So, what we
have over here is a vector a that is a 3/1 sorry, 1/ 3 vector and a vector c or a matrix c that is a
3/3. So, if we write say d=a;c. This is the result that we are going to get. So the first row is what
our vector a was. And next 3 rows are going to be what our c the array c was.

So, this is how we have built our matrix d. Let us say we try to do this a, c what is going to
happen is, we are trying to build a matrix which by combining an array which has 1 row with a
matrix that has 3 rows which is going to result in an error, the same similar type of error as we
had seen earlier. This is an error in horizontal catenation that means we are combining 2 arrays
horizontally and there is an error in combining that.

Recall earlier that we had what is known as a vertical catenation error. However instead if we
were to give e = b, c, that is going to work because this is our vector b which has 3 rows and we
are horizontally catenating it with another vector, that has another matrix sorry, that has 3 rows
and we will be able to do that.

The next function is transpose of a vector or a matrix. And for transpose you have to use
apostrophe, so or dash. So, if you we say a dash we are going to get instead of a column vector

22
instead of a row vector we are going to get a column vector. And just as we did before we can
construct a matrix now saying a’, c.

Now a dash what is going to happen is first we are going to have transposed. So, first MATLAB
is going to calculate the transpose of a. a recall was a 1/3 array. So if we transpose the 1/3 array
we are going to get a 3 /1 array, so now 3/1 array can be horizontally catenated with a 3/3 array
why because, (Video End 5:16)

We have the same number of rows in a prime as well as in c. So, if we are giving this particular
command as you will see later.

(Video Starts 05:24) We will see that we are able to construct the matrix f. So, f = a`,c enclose in
square brackets, press enter and we are going to get our matrix f okay. Now let us say we wanted
to get any particular element of this matrix.
So let us say we wanted to get this particular guide 2 over here. This is row number 2 and
column number 1 so we can get this by saying f not square but round brackets f (2, 1). So, that is
row number 2, column 1 and if we press enter, we are going to get this particular element.

Likewise, let us say we wanted to get this element, that is now in f, it is in row number 3 and
column number 2 and press enter and we are sorry, row number 2 column number 3. Row
number 2 and column number 3 and we are going to get this particular element okay. So when
we give this command it is row number 2 the first guy is the row and the third column over here
that is column number 3. When we give this command that is the row number that is going to be
row number 3 and column number 2 okay. So that is what we are going to get.

Now if we were to give let us say f (5, 5), now as you know that f (5,5) does not exist because we
do not have 5th row nor we have 5th column, if we give this, we get an error it says index
exceeds matrix dimension. So that is because that the size of the matrix was 3 /4 and we were
trying to query f (5, 5) that does not exist okay.

23
So, I will clear the screen over here. Now if I wanted to find the size of that matrix, there is a
very simple command called size. If I give size f, it is going to return to me the size okay. The
number of rows is 3 and number of columns is 4. (Video Ends 07:40)

(Refer Slide Time 07:41) Okay so, that is about building the arrays. I will next talk about colon
notations. So, colon notations is so colon in MATLAB is nothing but 2 or say want to go from
say 1 to 5 we use a colon 1:5 and let us look that here.

(Video Starts 7:59) If I give 1:5, I have 1, 2, 3, 4 and 5. Note that when we use colon notations
MATLAB creates a row vector and not a column vector. We can always assign this to any other
variable, so let us say we were to say z = 1 to 5 this is what we are going to get. Now let us say
we wanted to have 1 to 5 in steps of 3 we will give this as y = 1:3:5 okay.

So, in steps of 3 means the first guy is 1, second guy is 1+3 that is 4 and the third guy would be
1+ 4 + 3 that is 7 ,however 7 goes beyond the value 5 so 7 will not be admissible over here. So,
if I press enter, I will get 1 and 4 as the 2 elements in this particular vector. Now if I were to do y
= let us say 2: 1, what is going to happen ok.

Now 2:1 means we want to go from 2 to 1 in steps of 1 the 1st guy itself 2 is greater than 1 and
therefore we are not going to get anything we are going to get a blank matrix. Okay so, y
becomes an empty matrix which has 1 row and 0 columns. Note that MATLAB is not giving an
error; MATLAB is giving an empty matrix okay.

Likewise if you were to have y = [], we are going to get an empty matrix. So that is the 0 / 0
matrix it is an empty matrix okay. So, empty matrices are also matrices in MATLAB of size 0.
(Video Ends 09:59)

Keep that in mind because that is an important information okay. (Refer Slide Time 10:04). So,
we have kind of covered what colon notations are just as we had seen earlier we can query any
part of a vector or an array using elements of that array, likewise we can query.

24
(Video Starts 10:20). Multiple parts of that array. So let us look at the array f over here. And let
us say we wanted to get second row and column number 2 and 3. How we can do that is, f row
number 2 and column number 2 and 3 that is 2:3. And we will be able to get 0 and 1 over here.
Likewise if we wanted row number 2 and 3 in column 2, we will write f row number 2 to 3 and
column number 2.

So, 2:3 means row numbers 2 to 3 and column number 2. Press enter this is what we get that is 0
and 0. Okay, let me clear this screen again okay. In order to query any of the elements or any of
the sub matrix, the command that we are going to use is f (p, q), where p represents all the
elements that we want to query and in a row and q all the elements that we want query in a
column.

So, if you were to say (3, 3) that going to f (3, 3) which is 0. Likewise say p = 0, sorry p = 3, q =
3 and if you were to query f (p, q), we are also going to get f (3, 3) okay. Now instead if you
were to say p is 1,2 and if you say f is p, q what is that going to do is, we are going to query the
first and second rows of f and the first column of f because p is 1,2 and q is 3 okay.

And this is what we are going to get. So f what we queried is because p was 1, 2. So first and
second row q was equal to 3, so column number 3. So, first and second row of column number 3
is first and second row of column number 3 is what we get okay. Likewise we can say f 1 to 2
and 3 to 4 and this is what we will get. So, again let us type f over here so, what we have is f
rows number 1 and 2 and columns number 3 and 4. So, we are going to get this particular sub
matrix as our ans okay. (Video Ends: 13:01)

So, what now we have covered so far is, how to build an array and how to use colon notations
both to build an array as well as to get parts of an array or sub vectors in an array or a sub matrix
or the sub array in that particular array okay.

These are going to be extremely powerful tools that we are going to use and leverage in the rest
of this particular course. And I encourage you to look at the videos that are there on the

25
MATLAB website as well. As well as try these things out yourself at the end of this particular
module. We will have a certain set of examples that you can try.

These examples will be split based on the various lectures and I would encourage you to start
going through this particular examples right away in parallel to doing this video lecture okay. So
let us go to the next slide Okay.
(Refer Slide Time: 14:00)

This is what we have use in order to build the arrays. We use the comma and semicolon in order
to build an array using numbers as we are shown over here. In addition to that, we can also use
existing arrays if there are of an, of a correct size in order to build arrays as well okay. In
addition we have also seen in this function eye that is to create n/n identity matrix. In addition to
that there are several other array building functions such as ones, zeros, diag, rand, randn, and
magic, Hilbert and so on and so forth.

(Video Starts 14:42) So, I will quickly show you, how to build, how use ones. If we give the
command ones 2, 4, we are going to get an array containing all ones of 2 rows and 4 columns.
This is what we see over here and that will be assign to a variable ans okay. If I give the
command say x, capital x equal to zeros 2, 1, we are going to get 2 rows and a single column
with the el all of the elements getting the value 0, so that is what we matrix functions are going
to do. (Video Ends 15:23)

26
So let us look at another function called rand. Rand is basically uniformly distributed random
numbers.

(Video Starts: 15:29) So, if I give rand without any arguments, I am going to a single random
number. If I give rand with 2 arguments 2, 3, I am going to get a 2/3 matrix of random numbers,
these are the uniformly distributed random numbers that means you will get values between 0
and 1 with equal probability okay. (Video Ends: 15:59).

So these are the various array building functions.


(Refer Slide Time 16:06)

We also saw in the previous lecture some of the basic mathematical expressions. Scalar
expressions where a logarithm, exponent, power, sin, cos and so on. There are matrix operations
+,-,*, /. These are not scalar operations but matrix operations. So what I am mean by that is,

(Video Starts: 16:29) that we need our 2 vectors to be commutated. So let us bring out our vector
a okay. If I do a * a, we are going to get an error that is because a 1/3 matrix cannot be
multiplied by a 1/3 matrix. The number of columns of a has to be equal to the number of rows of
the one that you are multiplying that a with. So if you give a, we get an error saying that inner
matrix dimension must agree. The reason why that happens is, a is a 1/3 vector.

27
Let us say we have a vector b. Now b was a 3/1 vector. So, we can indeed multiply a 1/ 3 vector
with a 3/ 1 vector. So, you can indeed do a*b and that is going to be a matrix multiplication okay.
Let us look at the vector the matrix e. e was a 3 / 4 matrix where as a is 1/3 matrix. So, indeed we
can multiply a with e. So, if we do a multiply by e, we are going to get 1/ 4 vector okay. And this
is what we get. When we do a multiplied by e, again the rules of matrix multiplications are
indeed available or applicable over here okay. (Video Ends: 17:52).

The slash, we will not go over this slash currently that is because a little bit more advanced at
this stage. This slash is going to be either depending on what slash you use either it is going to be
left division or a right division. What left division and right division means something that we
will go in one of the later modules.

We are not going to cover that in this particular module. If you are going to use a slash use that
with care and wait until the next module that is going to cover the slash operation. The next one
is a power operation and that operation is the caret sign.

(Video Starts: 18:39). The caret sign is again a matrix operand. So we cannot use that caret for a
non square matrix that has to be use on a square matrix itself. So, if we do a caret 2, it is same as
a * a. These are the same commands and as we had seen a is not commutative towards self under
multiplication. However instead if we had a 3/ 3 matrix let us say r = rand (3, 3) okay. That
would be commutative in under power as well. So, r to the power 2 is just going to be r*r. So it is
a matrix multiplication and this is what we will get okay. (Video Ends: 19:28)

What I am showing over here also are the scalar operations and equivalent vector operations. So
here if we do exp of an array, we are going to get element by element exponential. If we want a
matrix exponential, we want to do expm.

(Video Starts: 19:50). Okay let us look at that. Let us say if we had say a matrix d = 1, 0, 2, 1 and
if we were to do, let me just clear this clc, okay 1 0 2 1. If was to do exp of d I am going to get
each element going to get exp its exponent. If instead if I were to do exp m of d, this is what I

28
going to get and this is a matrix exponent. And matrix exponent has its own rules something that
we are not going to go over in this particular module. But this is an individual element by
element exponent and this is a matrix exponent. And as you can see that the matrix exponent is
different than element by element exponent. (Video Ends: 20:48).

Okay in addition to that there are other commands sum, prod, cumsum and cumprod. Let us look
at those commands also. (Video Starts: 20:58). So our matrix a was 1, 2 and 5, sum is going to be
just sum of the elements of that particular matrix so 1 +2 +5. Cumulative sum is cumsum. Here if
you give cumsum of a, what we are going to get is, first element is going to itself the second
element is going to be 1+2 the third element is going to be 1 +2 +5 okay. So we get 1, 3 and 8 as
the result of cumsum.

Likewise we have prod, prod is just multiplication of all the elements of a and cumprod is
multiplication of all the elements until that particular. so cumprod of a is 1 then 1 multiply by 2
and then 1 multiply by 2 multiply by 5 and this is a result that we get. (Video Ends: 21:52)

Okay, this command size we have already seen. Length is a length of a particular array. So, if we
have an array which is say 5/1 and if we give command length, it will return the value of 5 okay.
Again these are things that you should try out in MATLAB okay.

Now in addition to the matrix operands we also have an element by element operand. Recall the
difference between log and logm. If we wanted to do this with multiplication, division and power
sign, we have an operand called dot star, dot slash and dot caret which will operate on each
element. And let us look at that (Video Starts: 22:37)

Let us define our variable a as 1, 2, 3, 1 okay. If I do a*a, we are going to get a which is going to
be multiplied by itself. That is going to be matrix multiplication. However, if I am going to do
a .*a, it is going to be each element of a multiplied by itself. So we are going to get 1 multiplied
by 1 that is 1, 2 multiplied by 2 that is 4, 9 and 1 this is what we will see when we do the result
over here okay.

29
Exact same thing is what you can expect with a c ^ 2 okay. And that is going to be same as a * a
squaring of a okay. And likewise if you want this similar to a.*a, that is going be a. ^. So, a. ^ 2 is
going to give us 1, 4, 9, 1 that means each individual element of the array has been squared okay.
(Video Ends: 23:44)

So these are the basic mathematical expressions. Again for people starting out with MATLAB,
some of these operations are little bit difficult to grasp. So, I would strongly suggest that you go
and try out these operations, take a pen and paper along with you and see what the matrices that
you are forming and what results you are going to get. With that I come to the end of this
particular lecture and I will see you in lecture 1.3 thank you.

30
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras
Module No. #01
Lecture No. #1.3
Introduction to MATLAB programming –Loops and Execution Control

Hello and welcome to the MATLAB programming for computation course. We are in module 1
introduction to MATLAB programming. We are going to do lecture 1-3 over here, where we are
going to cover loop and execution control. Specifically we are going to talk about for and while
loops.
(Refer Slide Time: 00:31)

Okay the loops, the 2 main loops in MATLABs are shown over here. The for loop and the while
loop. The command for for loop is of the form for i =1:10, a bunch of statements followed by the
command end okay. So, these number of these statements are going to be repeated 10 times. Let
us say we wanted to repeated for values of i =1, 3, 5 so on and so forth until 10 okay. We will use
the same column notations as before, we will say 1: 2:10 and that should work for i : 1 to 10 in
steps of 2 okay.

So, this is something again what we have seen one of the earlier lectures we can use the same
concepts in order to use it in the for loop the other loop is called the while loop. The while loop
commands will be executed as long as the condition that we state over here is true. So, let say we

31
start with i =1 and we keep repeating as long as the value of i < 10. So, we will repeat it for the
value of i =1, i =2, i = 3 so on and so forth until i =9.

When the value after executing this for the ninth time, when we come across this particular
statement, value of i is going to be equal to 10. So, i = i +1 will be 10. When we then go for this
particular statement with the execution going back to the first line, right now i =10 which
violates the command, which violates the condition i < 10 because i = 10 is not less than 10 and
it will exit this while loops. So if I write this particular loop with, starting with i = 1 this loop is
going to be executed 9 times okay.
(Refer Slide Time: 02:31)

So where to use for loops? For loop should be used when a set of operations are to be repeated a
specific number of times. If we know approximately how many number of times the particular
operation is going to repeat, we should use for loops. For example if we want to find the first 10
terms of Fibonacci series, we can use for loop for doing this. Find a factorial of a number n, we
can use a for loop because we know that we need to use this, do this for a specific number of
times.
(Refer Slide Time: 03:05)

32
Now when to use while loop? While loop is used when a set of operations is to be repeated until
a certain condition is met. For example, if we want to find Fibonacci series not the first 10 terms
of the series but the while find the values till the value of the last term becomes grey, until that
value of the last term is less than 200. Then we will use a while loop because what we want to do
is not repeat a particular statement for a specific number of times. But we want to repeat it until a
condition is met.

(Video Starts: 03:40) So let us go to MATLAB and do this Fibonacci series example using a for
loop. So let us say edit a fibo using for okay. And it is going to prompt me yes okay. I am going
to write a comment over here. Fibonacci series using for loop, let say n = 10.

We wanted to repeated 10 times fibo = 1, 1 okay. why 1, 1 we need 2 elements in order to start
the Fibonacci series. If you recall Fibonacci series is any nth element of the Fibonacci series is
just the sum of n - 1 and n - 2th element. So the first guy is 1, second guy is 1, the third 1 is 1 +
1 that is 2, the fourth 1 is 2 + 1 that is 3 the next guy is 3 + 2 that is 5. The guy after that is 5+ 3
= 8 so on and so forth okay.

So, now we will use for loop for i = 3 to n. Why 3 because we already have the first 2 guys okay.
So we are not going from 1 to n, we are going from 3 to n and we will say fibo i that is the next

33
element is going to be equal to fibo i -1 + fibo i -2 and end okay. Save this and run this fibo using
for, fibo using for and press enter and I am going to get the Fibonacci series.

You can see this in the work space over here. I will type this out by typing fibo and pressing
enter. So, we now have the first 10 terms of the Fibonacci series okay. So, let us say now we
wanted to convert it into a Fibonacci series using a while loop and we wanted to do this until the
element becomes equal to or greater than 200. The last element has to be less than 200 and that is
the condition that we will use. So edit fibo using while okay.

Okay, so we have fibo = 1, 1 and let me put comment over here. Fibonacci series until 200 using
while loop okay. So, fibo 1, 1 okay. While fibo end which is the last element of fibo is less than
200, so as long as the last element of fibo is less than 200. I am going to repeat the steps. Fibo
new = fibo end. That is the last term from the previous series and fibo end -1 okay. Now next
what we are going to do is, we are going to expand the array fibo with the fibo new term.

And i will say this as fibo, fibo new okay. Square brackets not circular brackets over here and
end okay. And once I do this, I am going to be able to run this particular code until the value of
the Fibonacci series reaches up to 200 or below 200 okay. So, this is the same thing that did
earlier initializing fibo as 1, 1 until the last element of the vector fibo < 200 the while loop is
going to be repeated the new element which has to be appended at the end of the fibo is going to
be equal to the sum of the last 2 elements.

The last element is extracted using the code word called end. End extracts the last element of any
vector or an array, so, fibo end is going to extract the last element of a fibo and fibo end - 1 is
going to extract the second last element. So, I am sorry it should be + and not - over here okay.
And then fibo is going to be fibo and the last element that has been extracted. And I save this and
run this fibo using while and press enter get my fibo, I type fibo and this is what I get.

So 1, 1, 2, 3, 5 and so on up to 233. And the reason why I actually got that element 233 is
because this loop was repeated. When the element 233 was already present in the fibo loop and if
I did not want that particular element in the fibo array I just need to do fibo end = blank. What

34
this is going to do is, it is going to replace the last element with null which basically means that
is nothing but killing the last element. And I can do that and let me now try that over here using
fibo using while okay. And now when I type fibo I am going to get 1 to 144. The 233 which is 89
+ 144 is no longer present the other way to do this instead of using fibo end = blank.

The other way to do this is, to do type fibo = fibo 1 : end - 1 okay. That is the other way to type
this. This is the type of format that will have been using so far. Is this is the standard colon
notations and colons notations is going not from 1 to end but not 1 to end – 1. So, all the
elements except the last element is going to be extracted out of the array fibo and reassigned with
the same array fibo.

Okay let us try this out again. I will give the command clear all to clear all the works space
arrays okay. And type fibo using while. Note that I am not typing the command; I know that I
have recently used that command. So, I am just going to use up array in order to select the
previous command and type enter and type fibo and I am going to get the Fibonacci series okay.

What we have get computed in Fibonacci series until 233 and when it comes across this
particular command it going to extract the first 12 elements not the last element from that
particular array. So we will have the array up to 233 and then the last element is going to be
removed and the first 12 elements are going to be reassigned to the array fibo okay. (Video ends:
11:20)
(Refer Slide Time: 11:21)

35
(Refer slide 11:27)

So that is how we have calculated first 10 terms of an fibonacci series or all the terms of
fibonacci series less than 200 okay. Now let us where think about the example that we saw in the
first lecture that was of Dhoni hitting a 6. In that example what we did was Dhoni has hit the ball
and we were trying to compute how the ball trajectory is going to be until the ball reaches the
ground. Let us create another example, where the ball is hit vertically instead of at an angle.

Let us say that the ball is hit vertically with a velocity of v0 okay. So, when it hit vertically with
a velocity of v0 the gravity that is acting against it in the downward direction. So, the ball is
going to go vertically upwards, going to reach a certain height which velocity is going to 0 and

36
then it is going to fall downwards under gravity. So let us say we wanted to find location of the
trajectory, of the ball that is thrown upwards at the end of every 0.1 seconds. We are going to use
this particular calculation in order to get y at every 0.1 seconds.

(Video Starts: 12:40) So let us do that using a while loop okay. So, we will say edit ball vertical
okay and I will say display location of ball hit vertically every at every 0 .1 seconds okay. So let
say v0 = 20 meters per second, gravity was equal to 9.8 meter square per second okay. So we
will do while y >= 0.

We are going to repeat this => sorry >= 0.1 because we want to compute at the end of every 0.1
seconds. y is going to be equal to a v0 multiplied by t - g multiplied by t square divided by 2
okay.

And we want to display this at t = num2str t, location = y okay. We have not yet covered how to
work with a string this is something that we are going to cover in next lecture okay. How so just
bare with me I will take this particular example again in the fifth lecture 1 -5 lecture and we will
go over it. And we will say end okay. What I am doing over here is, I am at each step. Whenever
we are repeating this particular array okay.

I am calculating the new time, I am calculating the location y at that particular time and I am
displaying the time and location y okay. I am going to repeat that so long as y >= 0 that means
the ball is in the air and has not reached the ground okay. So let me do that I will say ball vertical
and press enter and this is what I am going to get okay. So, I started at 0.1 at time 0.1, we have
reached 1.9 meters at 0.2 seconds, we have reached 3.8 meters so on and so forth.

And at time 4.1, we have reached below the surface okay. The equation is valid only until the
ball reaches the surface is no longer valid beyond that so we are getting an incorrect result over
here. Just as before what has happened over here is that particular loop was executed until the
value reached a value before a negative value. If we do not want that to happen what simply we
should we should be doing is we should be displaying before and not after computation of that
value.

37
And if we do that and we execute I will just clear the screen and I will execute the ball vertical.
What is going to happen is, the location will be displayed until the ball is in the air, not when the
ball has reached the surface and again you will see that it is starting at t = 0 location in is equal to
0 instead of starting at t = 0.1 okay. (Video Ends: 17:01)

So this is the thing that I wanted to cover with respect to for loops and while loops. So just to
recap, when to use for loops is, for loops is to be used when a set of operations is to be repeated a
specific number of times. Whereas the while loops is to be used when a set of operations is to be
repeated until a certain as long as sorry, certain condition is met okay. Okay so, with this we
come to an end of this lecture. In this lecture we have covered how to use for and while loop in
MATLAB. Thank you and see you in the next lecture.

38
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #01


Lecture No. #1.2b
Introduction to MATLAB programming – Arrays operations revisited

Hello and welcome to MATLAB programming for numerical computations course. We are in
module 1 which is introduction to MATLAB programming. Today we are going to revisit lecture
1.2 where we had discussed some of the array operation in MATLAB we are going to take one
simple example to look at how the array operations can be fairly powerful tool in MATLAB.
(Refer Slide Time 00:36)

So, the example that we are going to take basically on marks that are earned by 6 students in 4
different courses. The courses are mathematics, programming, thermodynamics and mechanics.
And the marks that are obtained by the 6 students are listed over here. We are going to undergo
certain computations that we did in the previous lectures. We are going to revisit them with this
specific example.
(Refer Slide Time 01:01)

39
So, the computation that we are going to do is, first we will create a matrix called all marks
which will contain the marks for the first 3 courses. The 3 courses are math programming and
thermodynamics for all of the 6 students and then when the mechanics marks come in from the
teachers for mechanics we are going to add those marks or append those marks to the matrix that
we are formed.

If you recall what we said in the in the previous lecture, basically this is going to be a matrix
which has 6 rows and 4 columns and therefore is going to be 6/4 matrix after we have appended
the mechanics marks. Once we have done at appending the marks for the mechanic’s course, we
are going to do several computations.

First is going to be the scaling the marks for the various for the mechanic’s course, we are going
to extract the marks of Chetan and give it to him as well as calculate his total marks. Two of our
students Deepak and Farah are the best students. So, we are going to extract the marks for the
first 3 courses. For these 2 students, we are going to calculate average marks and we are going to
scale all the marks out of 10.
(Refer Slide Time 02:11)

40
And we are going to use one of the matrix fundaes for this in matrix multiplication. So, if you
have matrix of this sort a, b, c, d, e, f. so, we have 3 rows and 2 columns. And we multiply this
with a diagonal matrix of this form. We are post multiplying with the diagonal matrix what is
going to happen over here is that the number 2 is going to multiply with the first row and the
number 0.1, sorry first column and the number 0.1 is going to multiply with the second column
and this is the result that we are going to get.

You can just simply do this matrix multiplication and see this for yourself. So, a * 2 + b * 0 is
going to be 2a. a multiply by 0 + b multiply by 0.1 is going to be 0.1b, so on and so forth okay.
So, if we want to scale all the marks out of 10, this is the kind of computation that we need to do
remember that at this stage the marks for the 3 lectures are going to be out of 100, whereas
eventually the marks for mechanics is going to be out of 50 so that is what we are going to do in
MATLAB so let go to MATLAB to do this array operations okay.

(Video Starts: 03:33) So rather than typing out all the stuff I have already created a file which
calculates the marks for 3 courses for all the 6 students. And we also given mechanics marks in
another vector called mechMarks okay. So, let us type allMarks sorry let us type markList and
when we execute this we are going to get allMarks and mechMarks as the 2 vectors.

So, let us type allMarks okay. So these are the marks for the 6 students in the 3 courses and
mechMarks are the less, are the marks for all the 6 students in mechanic’s course. Okay now

41
what want we to do is, we want to append mechanics marks to allMarks array. So, I will do this
all marks = allMarks, mechMarks okay. I am going to get an error over here. But let us try this
out and let us see what happens.

The error that we get is the error using horizontal catenation that means what we are trying to do
is, we are trying to put 1/6 array besides a 6/3 array which is something that we cannot do so we
have this particular box over here. And we want to put this box next to it that is something that is
not possible in MATLAB. What we need to do is, we need to transpose this particular array and
once we transpose that array we are going to put it next to the allMarks array.

So, let us try and do that. And in order to do that I will just clear the screen first okay.
Transposing mechMarks is the command for that is mechMarks dash, that is going to transpose
the mechMarks which is going to result in a 6/1 array 6/1 array we can catenated with a 6/3 array
and that is going to result in a 6/4 array okay. So, we can try do that we are now going to get our
allMarks as the marks for 6 students in the 4 courses and this is what we wanted. (Videos Ends:
06:05).

As the result of this first computation step, at the end of the first computation step we wanted to
append the marks for the mechanics course whenever to the allMarks array whenever we receive
okay. (Video Starts: 06:16) The next thing that we want to do is, we want to scale the marks for
mechanics by two marks out of 50 instead of marks out of 100. So, in that case what we need to
do is mechMarks = mechMarks multiplied by 0.5.

That is going to basically scale the marks out of 50 instead of out of 100 okay. Because this is the
scalar multiplication with the vector. We can actually do this and this is not going to be a
problem. And we are going to get mechanic. The marks in mechanics out of 50. So this is going
to be the marks in mechanics out of 50 okay. And now what we want to do is, we want to now
replace the last column with the actual scaled marks okay. So how do we access the last column
allMarks last column is going to be the fourth column okay.

42
So, it is going to be the 1 to 6. All the rows comma all comma fourth column okay. So this is
going to access basically the last column and all the rows in that column, so the rows is going to
be the first element in the reference and the column is going to be the second element in that
reference okay.

If we type this let us see what we are going to get. We are just going to extract that particular
column okay. So, when we type this is what is we are going to extract okay. What we want to do
is not extract but assign the mechMarks that were shown over here. That is what we want to
assign to that particular array.

So, what we are going to do is, allMarks colon comma 4. Remember if we just use this colon
without any numbers on either side in an array notation that means the entire column. All the
rows in a particular column, so that is going to be equal to mechMarks prime. Because we want
to transpose that multiplied by 0.5 okay. And when we do that, we are going to get allMarks as
seen over here okay. Now the reason why we are getting this in not an integer form but a real
number form is because we have two marks over here 36.5 and 45.5 which are real numbers.

If this was a programming language such as mat as such as c++, we would have to define
allMarks as a real number vector or real number array which is something we do not need to do
in MATLAB. So, MATLAB is a higher level programming language that itself takes care of all
this conversion from integers into double precession, double precession into integer and so on
and so forth of all these numbers are by default defined as doubled precession numbers in
MATLAB.

So, we do not really worry about defining or predefining any of the arrays in MATLAB okay.
The next thing was to extract the marks of Chetan. So, we have, I have name the 6 students as a ,
b, c, d, e, f so that it is just easier for us to know to extract or to find what particular row that
particular student belongs to. So, Chetan is row number 3, so we wanted to extract the marks for
Chetan. So, I will say Chetan marks = allMarks okay.

43
We want to extract row number 3 now. So row number 3 and all the columns from that row
number 3. So we can say 1: 4 so because we have colon, we have 4 marks or I can just put colon
over here because we want that entire row okay. When I do that I am able to extract the marks
for Chetan okay. So, these are the marks for Chetan. Now I wanted also to calculate the total
marks for Chetan, all I need to do is to use the command sum Chetan Marks okay.

And I am going to get the marks sum of the total marks for Chetan which is 224.5 okay. So that
is what I am going to get over here. Now what I wanted to do is, for Deepak and Farah I wanted
to get their marks okay. For the first 3 courses only the Deepak is in row number 4, Farah is in
row number 6 and I wanted column number 1, 2 and 3 okay. How do I do that is I will clear the
screen over here allMarks okay. I wanted row numbers 4 and 6 so in [], I will put rows number 4
and 6 okay.

Then separated by comma and I will put all the columns that I need. And the columns that I
needed are 1, 2 and 3 when I put that I and type enter. I am going to get the marks of the Deepak
and Farah in the 3 courses. So, I will just type all marks again so that we can confirm what we
have gotten. So the marks for Deepak are over here which we have gotten in the first row marks,
for Farah are over here which we have gotten in the second okay.

So, this is how we are able to extract a sub array from any array or matrix in MATLAB okay. So,
now let us next step that we wanted to do let us go over and find out okay. (Videos Ends: 12:07)
Next what we wanted to do is to calculate average marks obtained in each of the 4 courses okay.
(Video Starts: 12:17) And the way to compute average is in MATLAB is using the command
mean, so let us do help mean okay. And it is let us read s = mean x is the mean value of element
x. If x is a vector okay, for matrices s is a row vector containing mean values of each column

So what that means is that if we have the vector for example Chetan Marks, Chetan Marks was a
vector. If we do mean chetanMarks, we are going to get the average of marks of Chetan ok. In all
the 4 courses ,instead what we wanted to do is, we wanted to get the mean in each column of all
the 6 rows in each columns of all the 6 students in column number 1 likewise all the 6 students in
column no 2, 3 and 4.

44
That is exactly what the command mean does. If it operates on an array which means that it is
going to mean over the first dimension. The first dimension is the row dimension is going to
mean the first guy with the second guy with the third guy with the fourth guy with the fifth guy
and the sixth guy in the column number 1 it is going to repeat the same operation for column
number 2 and repeat the same operation for column number 3 and repeating the same operation
for column number 4 also.

Let us try that mean of allMarks. When I type that and I press enter, this is what I am going to get
so this is the mean of all the students in the first course that is math course. So the mean the
average marks in mathematics is 53.5, average marks in computer is 52.2, average marks in the
third course is 61, thermodynamics and the average marks in mechanics is 31.3 for all the 6
students.

Now let us say we wanted to get the sum of marks for each student. We can do sum, let us do
help sum over here okay. And what we see over here in one of the things in the help section is
s=sum x, dimension sums along the dimension dim okay. So, let us go back to the allMarks array
over here. So what we wanted to do is we wanted to sum over all the columns sum over column
1+ column 2 + column 3 + column 4 okay. So column is this second dimension row is the first
dimension column is the second dimension.

Recall when we said allMarks i, j; i was the row j was the column. So i is the first dimension.
Row is the first dimension; j is the second dimension, column is the second dimension. So we
wanted to sum over each and every column. So we want to use the command sum allMarks, 2
okay. That is going to sum over the columns for all marks, so we will get the sum of marks.

The total marks each and every students okay and when we do that, we are going to get. So, first
guy is in total marks for amith and the second guy is the total marks for Bhavna the third one is
total marks for Chetan so on and so forth. So, we can see these are the total marks for each
student okay. So, this is what we obtained the final thing that we wanted to do was to scale the
marks out of 10 okay. (Video Ends : 16:08)
(Refer Slide Time 16:09)

45
Okay recall the first 3 guys are marks out of 100 and the last guy is marks out of 50.
So, remember the matrix operation that we wanted to do the same matrix operations, now we
need to do with a 6/4 matrix. That is matrix of 6 students and 4 lectures. We wanted to scale it to
1/10, 1/10, 1/10 and 1/5 okay. Because the mechanics marks is out of 50 and not out of 100. So,
we what we want to do is, to create a diagonal matrix of 4 rows and 4 columns. First guy is 0.1,
second guy is 0.1, third guy is 0.1 and the last guy is 0.2. Because we want to scale out of 50 to
out of 10 okay so how do we create that lets go back to our MATLAB.

(Video Starts 17:03) And we will create this using a command called diag which creates a
diagonal matrix. So 0. 1, 0. 1, 0. 1, 0. 2 when I give this, okay and press enter, this is the matrix
that I am going to get and what I need to do is to post multiply by all marks with this particular
matrix okay. So, this all marks multiplied by this is going to give me scale.

If I pre multiply, I am trying to multiply a 4/4 array with a 6/4 array, 4/4 and 6/4 are not
commutative under pre multiplication. So let me try this, ans multiplied by allMarks and let us
see what I get. I should get an error okay. Error using star inner matrix dimensions must agree
that means the number of columns of the first guy should be equal to number of rows of the
second guy number of columns are 4 for our ans.

But number of rows for all marks was 6. So, this is not commutative under multiplication but
anyway this is not what we wanted. What we wanted to do was a post multiplication. I just
showed you what type of errors you can expect and what that error really means okay. So if I do
all marks multiplied by ans, this is what I am going to get.

46
Now the marks are scaled out of 10 instead of out of 100, 100, 100, 50 they are now each of
them are out of 10 okay. Again if you we combine the 2 commands, we can combine them saying
allMarks multiplied by diag okay. 0.1, 0.1, 0.1, 0.2 square bracket closed round bracket closed
okay diag has 1 vector.

That is an argument, so there is only 1 single argument to the command diag and that argument is
a vector which has 4 elements okay. So note this that within the circular brackets I have created a
square bracket okay. So, this is going to create a vector containing .1, .1, .1, .2 and that vector is
a single argument to my diag command when I do that, I am going to get the same result okay.

And if I wanted to store this in a vector called let us say scaled marks, all I have to do is, I go up
key. When I press up key the previous command is visible to me and now I say scaled marks =
all marks multiplied by diag okay. When I do that and press enter, I am to going to get scaled
Marks okay. So with this we come to the end of the recap for lecture 1.2. (Video Ends 20:14)
(Refer Slide Time 20:16)

So, what I have tried to do in this particular lecture is, take a simple example and go over some
of the array operations that we did in the previous lecture okay. So as you can see the column
notations, how to refer to a particular row to a particular column is something that we have seen
and the final thing that we have seen is how to use some of the array operations such as sum

47
mean so on and so forth okay. So, thank you for listening to this lecture 1. 1-2b and I will see you
in the next lecture where we are going to cover how to use for and while loops.Thank you.

48
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #01


Lecture No. #1.4
Introduction to MATLAB programming – Working with files scripts and functions

Hello and welcome to MATLAB programming for numerical computations course. We are in
module 1; introduction to MATLAB programming. This is lecture 1-4 where we are going to
cover using files in MATLAB. So, MATLAB files come in 2 types’ scripts and functions and
that is what we are going to cover in this particular lecture okay.
(Refer Slide Time: 00:33)

We have done this already a few times in the, in the previous lecture. In order to start editor in
MATLAB, we need to type edit followed by the file name. When we do that the editor opens a
file called file name .m, .m is the extension to say state that these are MATLAB code file.
MATLAB code files are return in ASCII. So they are basically readable by any text editor.

MATLAB provides an excellent editor which is what you should use for typing any MATLAB
file, indeed you can use say notepad or any other editor as well which is something that I will not
recommend you to do okay. So do use MATLAB editor as we have done before in the previous

49
lectures. For more help you can go to the MATLAB’s own website for writing a MATLAB
program, the link for which is given over here okay.
(Refer Slide Time: 01:33)

So as I said there are 2 types of MATLAB files, script files and function files. So what we have
done in the previous 2 lectures is, we have written small snippets of codes in order to calculate
multiple things. For example Fibonacci series or the height of the ball at various times, so on and
so forth okay. All that we have done in those 2 lectures, we have done it using what is known as
MATLAB s script files.

So what are the script files? So script files are nothing but files that contain sequence of
MATLAB commands and these sequence, these commands sorry, are executed one after other as
they were typed on the command prompt itself. So let us look at the files that we had created.

(Video Starts: 02:21) So this was the file that we had created for the trajectory of a ball thrown
vertically upwards. So these commands are executed one after the other. When we click on run
or we type the name of the file and press enter in the command prompt. Likewise we can look at
fibo using while or fibo using for. These are what are known as script files.

The difference between script file and a function file is that function file starts with the first line,
starts with a command called function. So that the syntax for a function file is going to be the

50
function space followed by [ ] separated output variables. So say out1 out2 and so on equal to the
name of the function myFunctionName or any name that you choose followed by input variables
in 1, in 2 so on and so forth okay.

Keep in mind that the input variables are in circular brackets, the output variables are in square
brackets. Having an output variable or an input variable is optional. So if you do not have output
variable the function is just going to look like function myFunctionName with input variables
which are comma separated. Input variables are also optional. If you have output variables but no
input variables, you can just say out1 out2 equal to myFunctionName and let us say you do not
have either input variables or output variables. You can say function = myFunctionName okay.

The way MATLAB functions and script work are something that I am going to cover in this
particular lecture. But at any point of time if you feel the need to write a function of this type,
you should really not be using a function but what you really instead need is script okay. When
you save this function okay, MATLAB will ask you to save it with the exact same name as the
name of the function.

So keep this in mind as far as this particular course is concerned the name of your function file
will be the same as the name of the function that you are using in that function file okay. This is
going to work for all the functions that we are going to use in this particular lecture okay. I am
going to close this particular function file. Go back to MATLAB and delete that file name
myfunction file. I will just click right click over here and click on delete and that will delete that
particular file okay. (Video Ends: 05:17)

So function files are file that taken certain inputs, execute a sequence of steps and return the
output at the end okay. The MATLAB statements are executed in functions own variable space
okay. And first we exit that function all the variables used are no longer accessible in MATLAB.

(Video Starts: 05:40) Let us go back to MATLAB and take the example of Fibonacci using for
loop okay. And let us execute that particular command. I will clc and I will clear this screen and
type fibo using for. And where I press enter, let us look at all the variables that were there. We

51
had declared variable n, declared variable fibo and variable i. when I will run that particular
comma run the particular file all the 3 variables that were used in the script file are also available
in the MATLAB workspace as can be seen over here.

The reason is because a script file shares the same workspace as the thing that was used to call
that script file. Because we call that script file from the command prompt the variables used in
the script file are also available in the workspace okay. So script file is nothing but the
commands as if they were executed one aft the other in the command prompt okay. It will do the
exact same thing.

Let me clear this and what I will do this, do now is convert this into a function file. In order to do
that all I need to do is, say function fibo using for and save this okay. I am just doing this to show
you how a function differs from a script, this is something you should not be doing, you should
not be using a function in this particular way because we want in this particular example we want
to execute the commands one after the other and for this purpose we need to be using scripts
okay.

So let us go to MATLAB and now type fibo using for again okay. When we type this and press
enter you see none of the variables are accessible anymore. If I type fibo that variable is not
accessible because that variable has a scope and a scope of that was in that particular function.
So when this function file is called n is defined, fibo is defined, i is defined computations are
done when we exit that function n, fibo and i are completely gone and no longer available in the
workspace. If we want to access that in the workspace, we need to use input and output
arguments with the function okay. (Video Ends: 08:13)

(Refer Slide Time: 08:14)

52
so that comes brings me to the next aspect scoping of the variables. It is a scripts shares the
variables with the workspace okay. Whereas function uses the variables in its own workspace. So
the scoping of the variables used in the function they have the local scope the scoping of the
variables in the script is the scope is the same as the function on the workspace calling that
particular script okay.
(Refer Slide Time: 08:46)

And this functions, I will going to talk to the workspace, to the input and the output variable, so
as I shown earlier the syntax for a function is basically going to be output equal to function name
and inputs that there is an error over here. The function should come before this and not after the
equal to sign okay.

53
(Refer Slide Time: 09:16)

So, we wanted to calculate. Let us say calculate the factorial because this is a single purpose
thing we are going to write a script. In order to calculate the factorial but if you wanted to write a
function in order to calculate a function of this sort, we are going to write a function. So let us go
ahead and write a script for calculating the factorial.

(Video Starts: 09:33) So let us say edit myFact and that is going to be a script okay calculate
factorial of n. So n equal to let us say 6, fact, value equal to 1 that is what we are going to start
with. For i equal to 1:6, factValue = factValue multiplied by I, end okay. So this is a script
because this does not start with function call. I am going to go to MATLAB and type myFact.

And all the variables will be accessible in the workspace as we are shown before and factValue is
going to be the factorial value 70 okay. The other thing that I would want to do is, I want to
make this a bit more, the code to be more better. I do not want to use for loop in doing this
instead what I am going to do is, use the array function called product. So if I give prod 1:6 that
is going to calculate the factorial for me.

So I will go over, what this particular command does in a bit that is, let us execute myFact in
MATLAB and see what let us clear the variables. First clear all and clc okay. So as you can see
in the workspace they are no longer. Any variables I give the script name my fact and press enter,

54
I now have the 2 variables fact value and n. The fact value is 720 as we had seen before okay. So,
let us see what the command product does. Prod, let us say 1 to 6 is what is that going to do is,
calculate the product of this particular vector.

The vector 1 to 6 is going to be nothing but 1, 2 3, 4, 5, 6 so just type that particular thing here.
That is nothing but 1, 2, 3, 4, 5, 6 prod which is operated on. This is going to do 1 multiplied by
2 multiplied by 3 multiplied by 4 and so on up to 6. So that is what exactly I am going to get as
720 and that is what this script myFact is doing over here okay. So that is the script that was the
purpose of this script is to run or execute certain commands in MATLAB command prompt okay.
Now instead if I want to calculate a given function and (Video Ends: 12:32)

The function that I wanted to calculate was c0 +c1x + c2x square up to cnx to the power n. For
that I am going to u use a function file (Video Starts: 12:39) I will call this edit myFunc okay and
function result = myFunc n okay and what I wanted to do, let us go back over here. (Video Ends:
13:01)

I wanted to calculate c0 + c1x + c2x square and so on. For c0 = 1 and c1 = 1 / 1, c2 = 1 /2, c3 = 1
/3 so and so forth okay. (Video Starts: 13:20) So what I will do is, I will create a vector of the
coefficients. Let us call this as a vector C = 1 comma I will put this in square brackets, 1 divided
by and I have element by element division 1 to n okay. So let us go over this once again okay. So
c, I will just say equal to c not which is 1.

And after that I need to append 1 /1, 1 /2, 1/3 and so on. So let us define a vector = 1 to n. So our
c is just going to be equal to c, 1. /vec .You recall in the second lecture of this module, we had
introduced the element by element operation ./.

So, what this is going to do, this is going to be do 1 divided by the first element of the vec, 1
divided by second element of the vec, 1 divided by third element of vec so on and so forth. That
is going to create a vector, so we are going to have a vector 1 followed by 1 / 2 followed by 1 /3
up to 1/ n. So that gives a vector of coefficients. (Video Ends: 14:53)

55
So now I have c0, I have c1, I have c2 and so on ,what are the multiplicands over here is going to
be x to the power 0, x to the power 1, x to the power 2 and so on up to x to the power n.

(Video Starts: 15:11) So forth that let us define a vector a. So a is just going to be equal to x to
the power and for that power, I am going use dot caret vec okay. So that is what I am going to get
over here. What I need is the first guy is nothing but 1, x ^ 0 is nothing but 1. So, I will append
that over here so 1, x to the power vec okay. And my result is going to be result = C. * a okay.

So the vector C multiplied by the vector a, so that is going to be my result and I will end this
with end. The command end is an optional in a function okay. And I am going to save this as the
file myFunc okay. You can see over here that this myFunc is actually highlighted. The reason is
because I have made an error in naming this the file name is my capital func whereas name of
the function is small func okay.

Recall that in MATLAB, all the commands are a case sensitive. So I want to have the function
name same as my file name. So if click on fix it will change the function name to capital f. I will
just save this and go to MATLAB and run myFunc, myFunc n and let us say result = myFunc n
where n was equal to 6 okay. Okay I get error over here that is because of not defined my x. So, I
need myFunc n, x over here.

And I need to call it with 2 arguments n and x. So n was 6 and s let us say x was 0.1 and I call
this particular function and I am going to get the result equal to sum result and that is going to be
sum of so it is going to be (1+ 0.1) / 1, 0.1 square divided by 2, 0.1 cube divided by 3 so on and
so forth. And that is going to be my result okay. So see what are what does happened is while
doing this particular function I have made an error that error was, (Video Ends: 17:58)

I am calculating c 0, I am calculating c1x, I am calculating c2x square and so on up to cnx to the


power of n. But I have not summed at up in order to calculate the function f, okay. (Video Starts
18:11) I will go in over here and just change this to say sum C *. a .Save this and now I will call
this clear clc okay. And call result = myFunc 6, 0.1 and I am going to get the result over here
okay.

56
Inadvertently what I ended up the show, ended up showing over here also is the way we will do
debugging. We recall that we got 2 errors, 1 was an actual error that stem because I had not
defined my input variable x at all which basically meant when it came across this particular
command what happened was, it was not able to calculate because x was not defined.

Let me go back to how I had return earlier and run this again okay. I will run this again and I got
that error undefined function or variable x on line number 6. I went to that function on this
particular line and realize that I had not defined my x, so I went ahead and made that change
defined x and I call that particular function using 2 arguments as shown over here.

The other error that I had done was, I was calculating this particular array C * a, but not
summing that up. When I saw that result which resulted not in a single value which was the sum
of that elements of that array but which was just the 6 elements of that array. I realized that I
needed to add that sum command over here okay. So this is also how we do debugging in
MATLAB. (Video Ends: 20:17)

Ok so let us recap. When to use functions and when to use scripts and for beginners that is for
most of you guys these are the general guidelines that i have for you okay.
(Refer Slide Time: 20:33)

57
So you want to use scripts when you wanted to make small calculations such as factorial or basic
computing as we did in the markList example in the previous lecture or when we wanted to do
plotting of some certain functions which we are going to cover in the next lecture that is where
we want to use scripts.
(Refer Slide Time: 20:53)

When do you want to use functions, let us say we want to calculate the values r, a result as a
function of variables t, y, so on and so forth when we want calculate r as a function of t , y and so
on. That is where we want to use functions.
(Refer Slide Time: 21:09)

58
The second example when we want to use functions is, when we want to pass them as a to
MATLAB functions. For example, in ode solving example that we saw in Dhoni hitting the 6.
We are calculating function using ode 45 in MATLAB and we wanted to pass that f of t, y as a
function to ode 45 that is another example where we are going to use functions.

This is something that we are going to cover in module 3 and thereafter we will not even cover
this in module 2. Just giving you a brief overview of when to use functions, third example, when
to use function is, let us say we want to calculate something as a function of temperature.
(Refer Slide Time: 21:44)

Let us say we want to calculate for example the saturation pressure of steam at various
temperatures or we want to calculate the a let us say diffusivity of something as a function of
concentration or let us say we want to calculate something as a function of voltage and current,
so those properties which we want to calculate as a function of let us say temperature or
concentration current or current so on and so forth.
(Refer Slide Time: 22:20)

59
That is when we will use functions so these are the 3 cases when we use functions for all other
examples for all other purposes we are more likely to use scripts rather than function okay. So,
with that I come to the end of that of this lecture what we covered in this lecture is the examples
of when to use scripts and when to use functions in order to computations.

Scripts is nothing but sequential execution of certain commands as if you wanted to the execute
them in the calling function or in the calling workspace, functions are to be used for achieving a
specific goal when you want to calculate for example r as a function of t, y and so on and so forth
okay.

So these are main uses of functions and script in MATLAB we are going to rely heavily on using
functions and scripts in this particular set of lectures from module 2 onwards okay. So with that I
come to the end of this particular lecture, the next lecture is going to be the last lecture in the
introduction module the next lecture is going to covered plotting and program output thanks and
see you in the next lecture.

60
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #01


Lecture No. #1.5
Introduction to MATLAB programming – Plotting and Output

Hello and welcome to MATLAB programming for numerical computations. We are at the end of
module 1 this module we are covering introduction to MATLAB programming in lecture 1.5. We
are going to cover plotting and output using MATLAB.
(Refer Slide Time: 00:31)

We have seen various forms of output in the first 4 lectures of this particular module. And what
we have seen is that when we do not end of particular line with the semicolon, the variables will
echo on this screen. That is one of the ways of displaying on the screen the other way that we had
used in the previous lecture was, to use the command called disp or display. So that is the
displaying on the screen that we are going to cover in this lecture.

I am also going to cover how to plot the data using what is known as the plot command. For
more help on plotting we can go to MATLAB website called using basic plotting functions the
link for which is given over here.
(Refer Slide Time: 01:21)

61
So let us now focus our attention to displaying on the screen and these are the various examples
that we have taken on how to display on the screen. The first one is when you give a command b
= 1, 2, 7, 1 with a semicolon , there will not be any echo but if you do not give this trailing
semicolon, this particular result will be displayed on the screen. This is what we have seen in
lecture 1.1 as well as multiple times.

The other way to display is using the command disp, display. It is going to display the contents
of this variable b on the screen. The third way is to use display a more advanced version of it.
And display can be used in order to display certain texts such as disp hello world will display this
particular text on the screen. And that idea can be used to make a more beautiful output. So let us
go at MATLAB and look at this.

(Video starts 02:25) We have this b = 1, 7 say 1, 3 close this and press enter. I am going to get
this as an echo. So this is what we have seen multiple times. Same thing if I am going give this b,
if this is going to display the contents of b but it is not going to give the initial part that is b equal
to part that we see what here, when I will click on this b and press enter this is what I am going
to see. Likewise if I give this hello world, it is going to display that is string as it is. So display
the string hello world.

62
If we recall what we had done in the previous lecture using my factorial, this was what I had
shown as a way to display the factorial value. What does that do? Let we just copy this and paste
toward here and let us go over each on this. So this uses basically the same rules as we do of with
respect 2 brackets.

The innermost bracket is what is going to be executed first. So this is the innermost bracket and
let us looks at that. Let us removed rest of the stuff and let us look at this. This is the innermost
bracket. So this particular guy is a string that says factorial value colon space. And this is a
command with says num2str myFact.

So let us look at what numb2str does and for that we will do help num2str. Num2str converts a
number into a string. So let us look at what say num2. Let us say myFact = 6!, so we can do that
using prod 1 to 6 and that gives the value myFact as 720. Because I did not end this command
with semicolon. I got the result myFact = 720.

What happens if I give, let us, we just clear the screen clc num2str myFact. If I give this
command, let us see what happens. What happens is, again ans = 720. So how this ans differ
from myFact, let us look at the work space over here, ans that you see of here is as a string that
we know that it’s a string. Because it has the apostrophe, the slash sign in the front and back
which means this is not a number 720 but it is consists of 3 characters the first character is a 7,
second character is 2 and third character is 0.

So that is the difference between ans and myFact. Let us look again at that particular display
command control c and control v, the display command and let us look at the innermost bracket
of the display command that we had done earlier. So now we have the square bracket which has
one part of the string and then we are catenating the other part of the string. Let us look at the
similar thing again. Let us say part 1 that is the first part and we say part 2 and let us see what
happens.
What we should get is the words part space 1, part space 2. There is no space between 1 and p of
the second part that is what result we expect. Press enter and that is exactly what we get. Now we

63
were replace part 2 with this ans value is 720 and we were to replace part 1 with factorial value
equal to this.

What we are going to get is, factorial value colon space and 720. That is also in slashes because
this is a string. When I press enter, I am going to get factorial value 720. So, what I want to do is,
replace that 720 with myFact. If I do this, I just replace it with myFact, what I am going to get is,
I am trying to take a string and next to string I am trying to put a number.

When I try to do that what I get is, I do not get anything over here because that number is not
converted into ASCII characters 720. I am just getting factorial value colon followed by some
kind of a character that you probably cannot see over here. So instead what we need to do is use
the num2str command to convert 720 into 720 as 3 characters and when I do that I am going to
get factorial value colon 720.

Now I did not want this ans to be displayed like this. In order to do that I am going to give
command with says disp sorry, disp and in circular brackets and when I give this I am just going
to get factorial value 720 without this ans equal to factorial value 720. I will just clear the screen
and type again. When I give this disp factorial value colon num2str myfact and I press enter, this
is just displayed as is on the screen. (Video Ends: 08:52)

So this is what disp function is going to do for us. So, a more beautiful output can be created
using disp and using numb2str. Sometimes we want to type out more complex things, we want to
output more complex things on the screen and for that we want to use the function called fprintf.
The syntax is fprintf is a similar to what we follow in C.

However, in this particular lecture series we are not going to cover fprintf for all our purposes. In
this particular lecture series the disp function is going to be sufficient. However for the sake of
completeness I have given more advanced output using fprintf as well.
(Refer Slide Time: 09:34)

64
So that covers the outputting on the screen part, let us now look at how to plot. So we will
consider the example of the ball thrown vertically upwards, we will plot location versus time, we
will label the axes and we will go the more other features of plotting.

(Video Starts: 09:56) So let us go back to MATLAB and the function that we are going to use for
plot is a function called plot. So let us look at help for this. Help plot and this is what we are
going to get. For now let us ignore the overloaded method part that is not of a concern for us
right now. That is go right at the top and what does plot do? Plot, plots a linear plot x, y, plots
vector y versus vector x. If x or y is a matrix then the vector is plotted against rows or columns of
the matrix whichever line up.

So, plot is a very powerful way of plotting things. So plot x, y is going to plot y as a function of
x. Let us take that example that we had done a couple of lectures earlier, which is of ball being
throwing vertically upwards. This was the script that we had used for the ball that was being
thrown vertically upwards. When run this particular script what we got was, we were getting this
display at multiple times.

Recall what we have just done a couple of minutes back. When we talked about the disp
command and when we used this disp command using multiple num2str, the result that we are
going to get is, as we can show in the screen. At various times we are getting the values of the

65
location where the ball is. So now what we want to do is, we want to capture all of these in 2
vectors. Let us called these 2 vectors time and location.

So time equal to let us make the blank vector location equal to again a blank vector. For let us
actually put as time = 0 and location = 0 because that is what we have starting off with. And at
end of this, we will put t sorry, time = time s; t. What that is going to do is, at each time this
while loops runs, the new value of time t is going to be catenated to this time vector time and
likewise, we will do location = location: y.

And finally, we want to plot this and we will give the command plot time, comma location. Enter
semicolon, save this and we execute this. We will clear the screen first and we will just execute
ball vertical and this is going to plot. So, this is the trajectory of that particular ball as the ball
goes from time 0 to time 4. Now what we want to do is we want to label these two axes the x
axis is time and y axis is location.

We do that the x axis we will label it by using the command x label, time and time was in
seconds enter and y label is location and location was in meters. And let us go to that figure again
and now we this has been label time and it has been label location. Now let us say that what want
to say increase the font size of this. Way to do that, just click on this arrow right click on font and
we will be able to increase the font size to 14. Likewise we will increase the font size again to
14. And we can do this with the access as well.

We click on font and make the font size 14 and we will get this particular figure and now we
have the font size increased to 14. Now, there are options to change the way of this particular line
that was displayed and also. And the way to do that again is using the plot command plot. So
this we can be done again by using plot command and the way to do is plot, our time, location,
give MATLAB how to plot this and for example we wanted a dashed line which is red in color
we will give --r that will plot a red dash line. And let us go and see this.

66
And what we now have is a red dash line as seen over here. Now instead if we wanted to have
dash dot line which is green in color, we will change it to -. g that will plot a line that is green
dash dotted in color. Instead now if we wanted in addition to that we also wanted, let us say to
plot circles at the end of it a blue color line with circles far at the various locations, we will do -
b followed by o and o will tell MATLAB that each data point we want to have a marker that is
circled.

Press this, enter and let us see what happens at each data point. We now this circled plotted as we
seen over here. We can also change the size or thickness of this line again by right clicking this,
line styles we have different styles of lines as well as line width we can change this let us say the
line width of 2.0. Again these markers, we can change the markers to two different types of
markers as we can have plus sign, circle, star, square, diamond and so on. So, if we wanted a star,
we can press this and we will get star and so on.

So, these are multiple ways of one plotting and manipulating the plot. Now let us say we wanted
to plot multiple things on the same plot, how we can do that? So, for that lets us have a value of x
let us say x = 0 to 5 in steps of 1. And let us say now we wanted to took plot sin x and cos x. So
all we need to do plot x, sin x, - b that will be blue color line and x, cos x, - r sorry, let us say -- r
that will be the red dash line.

When we will do that this is what we are going to get. I actually made a mistake. When I type x,
I did x in the steps of 1 that is reason why we got plot in that particular manner if I had x = 0 to
0.1:5. And then I give the plot command and let us see what happens. And we now get a
smoother plot. Now we have this particular blue line and this red line. Both of them are plotted
on the same plot.

So the way to do this is when we have multiple x and y vectors, we can have them comma
separated and we will be getting multiple plots. Another way to do this is also define a matrix let
us say y = sin x: cos x. Let us type size of y. So y has 2 rows and 51 columns that basically
means we have the first row is sin of x and second row is cos of x. And let say when we do plot

67
x, y we are going to get plot of the 2 lines and 2 lines will be plotted using a default way of
plotting.

And default is the first line going to be blue dash and second line is going to be a green dash. So,
this is the default way of plotting using matrices rather than using vectors. So you can plot
multiple lines on the same plot. And let say now we wanted to plot third line over here but if let
say what happens if we do plot x comma say sin x+ cos x what we will see this, we will get only
one single line and that is because when we do that the previous plot gets overwritten.

And that is why we are getting a single line as seen over here. If we do not want the previous plot
to get overwritten, we need to use the command hold on. So, let see how that works of close that
plot. Let me plot x, y again as before. So these are the 2 lines blue and green lines. Now if I do
not want to erase that plot, I will give command hold space on. And now when I plot x, sin x +
cos x this is, what I am going to get. Let me go to this file again.

So these were the original 2 lines in blue and green color and this is third line that got plotted in
blue color. Now let say we actually wanted to plot this in red color what we will do, just click on
this and delete this particular line. And plot this say using -- or that will give me a red dash line
on this figure. So, this is the way you can use the plotting commands. (Refer Slide Time: 20.50)

What we have done is, we have look plotting location versus time, we have looked at way to
label the axis, we have to look certain other plotting options. And we have seen how to plot
multiple lines using matrices as well as how to plot multiple lines using hold on. The final thing
is how to plot log logplot.

So for that again we have to go to MATLAB and we do not know how to plot a log logplot. So
let us just do help log and see whether we can find out how to plot log logplot. At the end close
to the end of the help we will see see also. And see also will give you commands that are related
to plot. So, there are plot tools semi log x, semi log y, loglog and so on so forth. So, loglog is
probably the one that we need to use.

68
So let us just click on loglog and look at that help loglog is nothing but a loglog scale plot log log
you works in exact same way as plot except the scales are logarithmic. So let say I do loglog x, y
and I am going to have x and sin x and, x and cos x on loglog plot and this is how I am going to
get.

So what is the funny thing that is happened over here? Remember sin x and cos x have negative
values what we happened when we plotted using a loglog is that the negative data that was
ignored during the plotting. So only the positive data were plotted in the loglog plot. So what we
have covered is the basic plotting functions? (Video Ends: 22:39)

We have covered how to label the plot, we have covered how to plot multiple lines, how to use
hold on in order to keep the previous lines, we have looked at how to change the fonts for axis
labels as well as the axis titles, we have also look at plotting log log plot. In the same way we can
plot semi log plot using either semi log x or semi log y command.

With that we come to the end of this particular lecture and also to the end of this module. Before
I end, I will quickly recap in the next minute and show what we covered in this module. So, what
we covered in this module was, introduction to MATLAB programming.
(Refer Slide Time: 23:22)

69
We looked at MATLAB basics familiarized ourselves with MATLAB command window and
MATLAB editor. We looked at variables scalars, vectors and arrays. And became comfortable
using array operations. We also looked at both scalar as well as matrix operation a multiplied by
b using a * b as well as a.* b, where that was an element by element multiplication. So both of
these types of mathematical operations we covered. We also covered trigonometric operations,
covered operations such as sum, prod, cumsum, cumprod and so on so forth. So wide range of
operations we have covered in lecture 1.2.
(Refer Slide Time: 24:07)

We have also look at arrays and use arrays to unlock the potential of MATLAB compared array
operations versus elemental operations. And use arrays for more efficiently using MATLAB. Let
me finally go to quick example for doing this.

(Video Starts: 24:34) Let us consider the example of factorial. A better way of calculating the
factorial as we had seen rather than having all these number of lines, we can place all of this with
just a prod command myFact = prod 1: n. That is exactly going to do what the entire function
with the loop was going to do. So I have replaced that entire loop with this prod command and
when I execute this that is myfactorial, I get the exact same result but in a more efficient way.
(Video Ends: 25:13)

70
So, this was something that is one of the most important features of MATLAB. That it was able
to work with arrays and we will need to be comfortably working with arrays to unlock the full
potential of MATLAB. Of course this is going to come with lot of practices to get more and more
using the MATLAB you will find we are using arrays in much more efficient way.
(Refer Slide Time: 25:36)

We then used look at execution control looked at for loop and while loop if then statements is
something that we have not exactly covered but essentially if then statements are used when we
want to compare certain condition, if that condition is met certain commands or executive, if it is
not met then certain other commands and if there is statement or executed. Again, if you do help
if we will get how to use them, if then commands
(Refer Slide Time: 26:09)

71
And then we looked at MATLAB scripts and functions.
(Refer Slide Time: 26:13)

We also in detail covered when to use scripts versus when to use functions and finally we will
add plotting in outputting in MATLAB. So that we come to the end of the first week we come to
the end of module 1. And the next module we are going to cover errors, error analysis and
approximations and using numerical techniques so thank you and see you next week bye.

72
MATLAB Programming For Numerical Computations
Dr.Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Introduction to MATLAB programming – Help video for assignment submission


Hello and welcome to the official first day of this course on MATLAB programming for
numerical computations. Today I am going to give you a small help video on how to submit
assignments. The first assignment, the assignment for module 1 has been posted on the web. And
we will see that there are 2 assignments that are posted.

The first one is the place where I have posted the assignment problem and where you will also
submit your MATLAB codes. Over here some of the results that we generate in the MATLAB
code are going to be inserted in this spaced that are provided. So first let us go and look at
problem statement okay. So here this is going to be the format in which we will put all our
assignments. First one is we will have a problem statement. So we can click over here to take a
look at the problem statement.

Now we have 4 assignment problems, the text in red tells you what should be the name of the
file, these are the files that you need to upload, I will come to that in a few minutes okay. So for
the first problem you have to upload a file called demo problem.m. Save it as demo problem.m
itself not with any other name. Likewise you will have to do the same thing for each and every of
the assignment problem.

So the first problem is something that I will solve over here and I will show you how to do this.
The first problem is for x in the range of 0 to 1 in steps of 0.1 we have to calculate x multiplied
by e to the power - 2x. We then, have to find out what value of f(x) is the maximum value. We
then find out the x value at which f(x) is maximum. This x value is reported in the variable result.

So let us go to MATLAB and take look at the file. What I have done already is created all the 4
files that I required to solve the 4 problems. The first problem is demo problem. So, let me done
this and show you okay. So if I type result, I will see that the result is 0.5 that is the result the
time need to provide in the quiz assignment 1 section that the second link.

73
We will come to that in a minute. Now we are using MATLAB online this is what you meet
instead. And that I will create a file. So, let say edit, demo problem, assignment 1, problem 1. x =
0, 0.1, 1, f(x) is x multiplied by -2 multiplied by x. So, x is a vector and these guys also vector so
the product would be in 2 vector should be element by limit multiplication.

So we need to have a.* y. So, we can say max f idx = max f .That will give us the max value of f
of the vector f as well as the index f as which that occurs. Variable result equal to x idx .So idx is
the index at which f is maximum and xidx will extract the value of x at which that maxima in f
occurs we run this. So when we done again we can see that the result we get is 0.5 okay.

So, now that we have completed this problem. We need to download it on our local drive. I will
highlight this and click on this download link. Once I click on that the file will get downloaded. I
can click and showing file and we can look at the files. So these were recall the files that I had
generated using my desktop version of MATLAB. And this was the file from the online version.

So let me just rename this as desktop, we will not submit that file and this I will rename it as
demo problem. So this is the file that we want to submit. And create a zip file okay. Now so that
zip file created I need to go to the next step okay. So now we have the zip file that is looks at
what we need to do the next. what we need to the next is upload the zip file on Google drive and
then share this file and post the file id in the text box below.

What that means is I will go to drive.Google.com; create a new directory called MATLAB
assignments. In this new directory, I will drag and drop this assignment. zip, assignment1. zip.
Once that is get uploaded I will right click on this and click on share okay. When this comes up,
click on this advance button and you need to make this not private but it should be sharable with
anyone who has a link. So click on share with anyone who has the link. We will get access, no
signing required okay. And save this.
Now click as done over here. Right click and click on get link. Once we can get link, we can
copy the link that is provided and open a text editor and paste this link over here. And what we

74
need is that part that comes after the equal sign. So we need to copy that part and paste in the text
box that is provided over here. So let us go and do that so, we have copied it.

We will just look enroll and we will click on submit okay. Once we click on submit, we will get
something like this submission succeeded. So we have completed the first 2 steps that is to do
assignment and to upload this is a file in the Google drive okay. These 2 steps are not enough.
What we also need to do is, to submit the results that we get in the results section the way we can
do that is, we can click on the link over here.

Or there is another way to do on this side okay. You can click on this quiz assignment 1 submit
MATLAB results link over here both these links are one on the same. So you will see that for
each of the problems that we have we are expected to report the results. So for this particular
problem what we needed to submit was the value of result. So let we just type this and get this
value. I will just copy this control c and paste it over here okay.

And I will just go to the bottom and once I have put all the values all the values of the results that
I want, I can go and click on and submit answers okay. For this particular assignment, just to
help you guys get acquainted with the way we submit. We also has to check answers option that
we have allowed. So if you click on check answers, you will see that score out is 1 out 4.

We have submitted only 1 problem and we have got 1 out of 1 point okay, which means that this
answer is correct. You can click on submit answers. Once you to do that, your answer is
submitted. You can then go back to course page and continue viewing the course okay. So, to
summarize what is needed is first you go on and click assignment link, download the problem
statement, do the problems, create as a zip file and uploaded on Google drive.

And finally the most important step that you need to follow is, to submit your results in this
results section. So that will be able to grade you appropriately without the step of submitting the
MATLAB results. In the results section you would not get credit a grade for the work that you
done in the assignment. Thank you and I hope you will have a lot of fun doing the assignments in
this MATLAB course. Bye. (Video Ends: 10:07)

75
MATLAB Programming for Numerical Computation
Dr.Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology – Madras

Module No. #02


Lecture No. #2.1
Errors and Approximations – Errors in Numerical Methods

Hello and welcome to this week to of MATLAB programming for numerical computation
course. In this module we are going to cover errors and approximations. This is lecture 2.1 and
in this lecture we are going to look at where do errors originate in numerical methods. One of the
things that happen always, when we talk about numerical methods.

When you use computers for using numerical techniques, you will always encounter errors of
various forms. And these errors are going to have 2 basic origins. 1 is what is known as
truncation errors and the other is known as the round of errors. Where do this error come from?
And how to they affect the overall numerical methods is, something that we will introduce to
ourselves in this particular lecture.
(Refer Slide Time 01:06)

Ok so, let us look at the first term type of an error that is called the truncation error. And what are
listed over here is, the Maclaurin series for calculating e to the power x. The Maclaurin series of

77
e to the power x up to nth order term is return as 1 e to the power a. I am sorry, is return as 1 + a
+ a2 / 2! + a3 / 3! and so on.

So what we are going to do is, we are going to compute e to the power 0.1 using multiple number
of terms in this Maclaurin series. And we will compare it with the actual value. The error we
have, we will define as the difference between the true value and the approximate value. We do
not care whether this difference is positive or negative. The error is therefore just the absolute
value of the difference.

So it does not matter whether the approximate value is greater than or lesser than the true value
as long as it is different than the true value. We want to capture how much that difference is and
that is how we define the absolute error epsilon (02:08) okay.

And what we will show using MATLAB is, that if we introduce more number of terms in this
Maclaurin series, we are going to get lower error okay. So let us head on over to MATLAB in
order to compute this using the Maclaurin series approximation.

(Video Starts: 02:23) So, let us open script file. Let us call it maclaurinExp okay. We will create
the file Maclaurin series for exp of 0.1 okay. So let us look at Maclaurin series with different
number of terms. So let us say we want to look at terms up to n = 5. So we will say n = 5, our
value of a was 0.1 okay and the result that is called as expVal = 1.0 why 1.0 is, the first term over
here is 1. (Video Ends: 03:13)

The first order term that is a to the power 1 term is a, a to the power 2 term is a square divided by
2 factorial so on and so forth. So if you look at the first term and compare it with the second
term, it is the first term is just multiplied by a/2. If you compare these 2 terms, is just multiplied
by a/3 so on and so forth.

(Video Starts: 03:40) So that is what we are going to do. We will call currentTerm as 1.0 and we
will use a for loop for i = 1 to n okay. And currentTerm = currentTerm multiplied by a/i okay. So
let us see how exactly this works. (Video Ends: 04:06)

78
So we have e to the power a, that we need to compute approximately. When i is going to be equal
to 1, at that time our value of currentTerm is going to be 1 multiplied by a/1. That is a, in the next
loop it is going to be a multiplied by a /2. That is a square by 2 factorial. The next term will be a
multiplied by a2 / 2 multiplied by a / 3 that would be a3 / 3!, so on and so forth okay.

(Video Starts: 04:41) And our expVal = expVal + currentTerm, okay. And I will end this loop
over here. So that is going to be the value of expVal. Let us say trueVal = exp (0.1) and error =
abs (trueVal – expVal) okay. So we save this and will run. Let us go to MATLAB and run it and
we get this result. So, we actually see that the error is 1.4 multiplied by e to the power 10 minus
9. That is the error expVal is 1.1052 and trueVal is also 1.1052 because the error is of the order of
n ^ - 9 okay.

Now what let us do is, let us change the particular code. So, that we store all the various values
of expVal. So let us say expVal(i) = expVal(i – 1). Let us put it in this way expVal(i +1) =
expVal(i + currentTerm). So, what does the first expVal contain? It is it is the 0 ordered
approximation (Video Ends: 06:34).

That is just 1. So, that is not really an approximation, expVal 2 is going to have 1 + a, expVal 3 is
going to have 1 + a + a square by 2 factorial so on and so forth. When we calculate this error,
trueVal is a scalar and expVal is a vector so, we can subtract a scalar from a vector or a vector
from a scalar. And we will get a result in vector so this should not be any problem for us. The
error will actually end of the 6 dimensional vector, a 1 /6 vector okay.

(Video Starts: 07:06) So let us go and clear okay. And let us now run Maclaurin exp okay. So
now we have the error and as you can see the error is 10 ^ -3, 10 ^ -4 and as we go to higher
order terms the error keeps decreasing quite significantly.
So what we see over here is, that as we introducing more and more terms, our error keeps
decreasing quite significantly okay. Let us look at error 4 which is basically when n = 3, our error
is 10 ^ -6. When n = 4 our error is going to be 10 ^ -8. And we have seen already that when we

79
introduce all 5 terms of the Maclaurin series, our error drops even further okay. (Video Ends:
08:04)

So this is how we have computed, the e to the power a, approximation using Maclaurin series. So
what we have seen over here is that as we truncate the overall series to nth order terms we
realized that the error keeps on decreasing. So more the number of terms of Maclaurin series we
introduce, the smaller is going to be the error okay. Why does the error keep changing or why the
error in the first place?

The reason why there is an error in the first place is, because we are truncating this Maclaurin
series. So this series is being truncated at the first term or the second term or the third term and
so on and so forth. If we truncated just the first order term, we are going to get a greater amount
of error. The error was of the order of 10 ^ -3. When we truncated it to second ordered term, we
got error of 10 ^ -4.

When we truncated it to the third ordered term, we got the error of the order of 10 ^ -6 so on and
so forth. So what we saw over here is that once we introduce greater number of terms, we are
going to reduce the error. This is going to be something that we are going to see consistently in
this particular course. So if I can summarize these results. What these results are? When you
have an infinite series the greater number of terms that you retain in the infinite series, the
greater is the accuracy or conversely smaller is the error okay.

In the next lecture we will expand this particular idea to Taylor’s series expansion. Taylor’s series
expansion is indeed where we get this Maclaurin series from. So that particular idea of
introducing more number of terms in order to reduce the overall truncation error is going to be a
common motive that we are going to follow in the rest of this particular lecture series. Ok so this
was the first type of an error which is the truncation error.

(Refer Slide Time: 10:06)

80
The second type of an error turns out because of what is known as machine precision. We like to
think of a computer as having what is known as an infinite precision. However that is not really
true. The machine, the computer also has a machine precision which in some way is like a least
count. So let us consider a ruler that I have shown over here. The least count of this ruler is 1
millimeter or 0.1 centimeter.

So that is kind of the minimum resolution of that ruler. You cannot measure length that is less
than 1 millimeter. Ok if you want to measure, then with the precision greater than 1 millimeter or
a precision value of sub millimeter.
(Refer Slide Time: 10:52)

81
We can for example, use a vernier caliper. This particular example of vernier calipers has the
least count of 0.01centimeter, so we can say that this vernier calipers has a better precision than a
ruler okay. So precision is basically a term which we can approximately, we can use it in view of
the term least count okay. So it is kind of like least count. Now in the same manner in which
these 2 devices have a least count our computer is also has a least count.
(Refer Slide Time: 11:27)

Just like this devices the real numbers in a computer representation have a least count. So
between a number a, and a+epsilon, that does not exist any number and that is what the machine
precision is about. It is about the least count of that particular computer okay and that least count
is dependent on the number of bytes that we used in order to store a real number. So for example,
a standard real number is, uses 4 bytes which is basically 32 bits so that particular real number
has a certain precision. A double precision real number which uses 8 bytes or 64 bits has a better
machine precision okay.
(Refer Slide Time: 12:16)

82
And that representation is based on what is known as the floating point representation of a
number for more information on this. You can go to the computational techniques course the link
for which is given over here and look at the machine precision and floating point representation.
So let us look at floating point representation of a decimal number. Let us say we have 5 boxes in
order to store a number and 1 box in order to store an exponent.

The number is always going to be represented as 0 point x x x x x multiplied by 10 ^ n okay. So


this is where you we put in those 5 digits of our decimal number so let us see.
(Refer Slide Time: 13:02)

83
How we represent a number of 23.217. The number 23.217 becomes 0.23217 * 10 to the power
2. So what is the next number? That comes in the next number is when the lowest digit over here
is incremented. So that becomes the next number becomes 23218 * 10 ^ 2 that is the next
number okay. So what if we had a number of the form.

23.2172, the problem is the number 23.2172 cannot be represented with 5 digits of what is
known as mantissa and 1 digit exponent. The reason is that number will be represented as
0.23217 multiplied by 10 ^ 2. There is no space to store this trailing digit. As a result this number
and this particular number have the exact same representation.

The reason why we are not able to represent a number with a 6 significant digit is, because we do
not have enough precision in our decimal machine okay. So the least count of this particular
decimal machine turns out to be 10 ^ -5 multiplied by the absolute value of x. So that is the 10 ^
-5 is what is known as the machine precision.

(Video Starts: 14:32) In case of MATLAB, the machine precision is given by a keyword eps and
the machine precision in MATLAB is 2 multiplied by 10 ^ -16 okay. This machine precision is 2
^ -52. (Video Ends: 14:54). The reason is because just the way we had 5 digit representation of
this mantissa in this decimal number. Likewise the double precision real number that MATLAB
uses which is the IBM standard uses a 52 bit mantissa. So the mantissa has 52 bits in it. So the
precision is 2 ^ -52.

Note over here that the precision was 10 ^ -5 okay. So let us look at the case where we have
(Video Starts: 15:25) let us call a = 1+ 2 ^ - 52. That is the value of our a and if you subtract a -
1, we are going to extract this particular value okay. So what is happening over here is when we
use the value of this precision, that particular number a 1 + 2 * 10 ^ -6 is indeed represented as
1.00015 times followed by a2.

However what if we write a = 1-2 ^ -53, this number is going to be indistinguishable from 1. If
we say y a – 1, we are going to get a result of 0 okay. So what does this mean is, this means that

84
our MATLAB has a least count of 2 ^ -16. And that is what is known as the machine precision
okay. (Video Ends: 16:25)

So with this, we come to the end of this particular lecture. In this lecture we covered 2 important
concepts. The 2 concepts that we covered in this lecture were is that the truncation error because
we are going to truncate an infinite series to a finite number of terms. There is going to be an
inherent error that is associated with that truncation, the second thing that we realized was that
there is a machine precision.

We cannot represent any real number in any form that we want there is going to be a least count
with respect to representing those real numbers. So, what does this imply when it comes to
numerical techniques, something we are going to cover in the next lecture. Thank you and see in
the next lecture.

85
MATLAB Programming for Numerical Computation
Dr.Niket Kaisare
Department Of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #02


Lecture No. #2.2
Errors and Approximations – Truncation and round of errors

Hello and welcome to MATLAB programming for numerical computations. We are in module 2.
This is lecture 2.2. In this lecture we are going to cover truncation errors and round off errors. In
the previous lecture we had seen Maclaurin series expansion of e ^ x. And saw that as we retain
more number of terms in that series, the error reduces and we also saw what is known as
machine precision.

So there is a least count that is associated with using real numbers in MATLAB or any other
computer based program okay. We are going to use these concepts to understand what truncation
error is and what round off error is and how that works okay. So, this is the Taylor’s series. The
Taylor’s series is given by f(x + h) = f (x) + hf ‘(x) + h square /2! h’ f ‘’( x ) so on and so forth.

If we retain only nth order terms in the in the Taylor’s series or the Maclaurin series the error is
of the order of h ^ n +1. This is something that we have also seen in the computational
techniques course.
(Refer Slide Time: 01:29)

86
Taylor’s series is some kind of a work horse, when it comes to computational techniques, it is
used in order to derive a large number of computational techniques as you might have seen in a
numerical methods or computational techniques course. The accuracy depends on how many
terms are retained in order to derive that numerical technique.

So, greater number of terms we represent or larger is this value of n greater is going to be the
accuracy of the numerical scheme that you are using for the computation purposes okay. So let
us head back to the Maclaurin series.
(Refer Slide Time: 02:06)

87
So, what happens when we retain the Maclaurin series to the n order term, we are losing h ^ n.
So, that is n+ 1 sorry, so that is the leading term when we comes to the accuracy so, when we are
placing x with 0 and h with a, we are going to get this Maclaurin series for e ^ a .

And when we retain n terms, we are going to get the order of the accuracy to be a ^ n+1. So last
term implies that the last error is of the order of n ^ n, a ^ n + 1, because we have retain only the
finite number of series okay.

(Video Starts: 02:52) So let us head back to MATLAB and let us look at the Maclaurin series
code that we had written earlier okay. So this is how we are written the code again. If you recall
from module 1, this somewhat an inefficient way of writing a code in MATLAB. Because
MATLAB, we can use the power of arrays, in order to calculate this in a more efficient way. So
that is what first, I am going to do.

I am going to use the power of arrays in order to do that. So let us just delete this okay, a was 0.
1. Let us define a vector called vec = 1 to n okay. The various terms are going to be, terms are
going to be equal to a ^ n are going to be the various terms. So a. ^vec are going to be the various
terms divided by the factorial. The factorial we can calculate using the MATLAB command
called cumprod c u m p r o d and cumprod vec okay. Now what is that going to do? So let us
look at this over here. So a = 0 let us say a equal to 0. 1, vec = 1, 2, 3 okay.

So a^ vec is going to be a ^ 1, a ^ 2, a ^ 3 okay and we are using dot caret because it is an


element by element power okay. Now let us see what cumprod of vec gives us. cumprod of vec is
going to give us the first guy is 1, the second guy is 1 * 2 that is 2, third guy is 1 * 2 * 3 that is 6.
Let us say if we were to do cumprod of say 1 to 5 this is what we are going to get.

The first guy is 1!, second is 2!, third is 3!, fourth is 4!, fifth is 5! Okay. So this is a vector which
a ^ i. This is a vector which is i! and if we do an element by element division, we are going to get
each element of the Maclaurin series okay. So we needed to do an element by element division.
So we use the dot slash .And not just the slash. So these are going to be the various terms okay

88
and the expVal is going to equal to cumsum of the various terms. So what is e expVal going to
be? (Video Ends 05:43)

ExpVal is the first guy is going to be a, the second guy is going to be a +a square, the third guy is
going to be a + a square + a cube by 3! So on and so forth okay. So these are going to be
individual term we have not yet added 1 so we will do that over here.

(Video Starts 06:02) So expVal is going to be 1 + cumsum of terms. So that those are going to be
the first guy is going to be (Video Ends: 06:13)

The approximate value 1+ a, the second guy is going to be 1 + a + a 2 / 2! The third one is 1 + a +
a2 / 3!, so on and so forth.

(Video Starts: 06:24) So this is what we are going to get okay. And this is the trueVal and error is
the absolute value of difference between the trueVal and the expVal. So let us go and save this
and run this in MATLAB and see whether we are going to get the same results as before or not.
Clear all and clear the screen and let us run Maclaurin exponential and let us look at expVal. And
we are getting 1.1, 1.105, and 1.1052 so on and so forth.

And error if we see, is similar to what we obtained last time. The only difference is in the last
time when we did that, we had the term 0.1 as well which was a 0th order term. We do not need
that term so we have gotten rid of that already okay. So we have now retained 1 term that is the
first order term, the second term that is the second order term, third term that is the third order
term and so on and so forth okay.

So this is what we did. When we wanted to get the Maclaurin series expansion of 0, e ^ 0.1? So
what if we wanted this as for e ^ 0.01 as well, so in order to do that let us place a with 0.01. And
let us see what we get back okay. And let us run this and we will see error again okay. So when
we use it, I am sorry about that, this also needs to be changed and now let us run this.

89
And let us look at what error we get as we can see the error. When we have h smaller that means
that value of a is smaller, the error has decreased quite significantly as well okay. So let us now
do something is, let us say first, second and that order term for various different values of a. So
let us look at the values of a equal to let us say 0. 1 okay.

And let us plot this particular 1 on plot. So let us say plot error, let us hold on so that particular
plot is held. So let us look at now plotting how the error changes with respect to the various
values of h. So what we are going to do is, for n = 3. Let us take various values of a as 0.1, 0.05,
0.02 and 0. 01 okay. These are the various values of a that we are going to use okay, aAll let us
call back, we need vec, vec is going to be 1 to n.

So for i = 1 to length (aAll). ExpVal is this trueVal. So, these are the changes that we are going to
do and a = aAll i okay. And we are going to plot aAll, error and we are going to name this axis
also x label step size and y label error okay. So, this is what we are going to do over here okay.
So, let us first run this and see that we are able to run this without error or not. If we do get an
error, we will just go back and debug this code.

Yes, we are getting an error. We have an undefined variable err, so let us define err = a blank
vector. And it now run this code and we are getting the error verses a step size okay. What we are
seeing is, as a step size increases the value of error is also keeps increasing. So we do not want to
look at the linear plot, we want to look at a logarithmic plot. So instead of saying plot, am going
to say loglog.

So that will give me a logarithmic plot, a loglog plot over here okay. And this is what I am going
to run okay. So let me run this so, I have gotten this logarithmic plot for the 3 particular values of
n okay. And let me label that label n = 1, n = 2 and n = 3 sorry, it is not label, it is legend and
show graph okay. So this is for n = 1, this line is for n = 2, and this line is for n = 3 okay.

So, the thing that you notice over here is that the as the step size increases, the error increases or
as the step size decreases, the error is decreasing. Other thing that you will notice is, let us put a
marker for this. Other thing that you will notice is that the slopes of the lines are different. They

90
are all approximately linear of straight lines. But the slopes of these lines are different and they
are linear straight lines even when there are 4 points.

So they do look fairly straight to me. So, their slopes as you can see this one is a more gentle
slope for n = 1, for n = 2 we have a steeper slope than n = 1 and for n = 3 we have an even
steeper slope okay. So let us go and plot instead of plotting on a loglog plot, let us actually plot
logarithm of aAll and error.

So let us say figure 2 plotting. Figure 2 will say plot log 10(aAll) and log 10(err) okay. And let us
run this and yes we have gotten this plot also. So this is basically, we are instead of a loglog plot
we have a linear plot. But we are plotting the logarithm of values. So let us click on this and
query the various points that we have.

So the first is x = -1 and y = - 2. 28 okay and the last point over here is x = - 2 and y = - 4. 3. So
let us get that slope so it is - 4.3 -2. .Let us go back and see minus 2.286, so minus and minus is
going to become plus, divided by -2 +1. So, the slope for n equal to 1 is approximately 2. 01. Let
us look at the case where n = 3. Here the y value is -5. 371. so that is minus 5. 371 + this value is
-9. 379.

So, minus and minus will be become plus. Minus 9.379, whole thing is divided by -1 + 2 and the
slope turns out to be 4. Likewise if we were to for this particular line, the slope will turn out to be
3. So this is a line with slope of 2, this is line with slope of 3 and this is a line with slope of 4. So
if you see what we are getting is, slope of the nth order curve is n + 1. (Video Ends 16:36)

And that is what is the meaning of order of a ^ n +1 okay. So when we retained just these terms a
^ n+ 1 where n was equal to 1 becomes order of a ^ 2. So we are getting the accuracy is a square
accuracy. When we retain a square by 2! term also the slope is of the slope is equal to 3 that is
because the order of the accuracy is the step size ^ 4 and when we have a ^ 3 /3 term also
included is a ^ 3 + 1 that is a ^ 4.

91
So the 3 lines have a slope of 2, 3 and 4 that is because the order of accuracy is n+ 1. So we
retaining the first order term has a slope of 2, retaining second order term has a slope of 3,
retaining third order term has a slope of 4. So, that is the meaning of this particular term order of
accuracy. So what we are seen over here is that the order of accuracy increases or improves as we
retain greater number of terms okay.
(Refer Slide Time 17:50)

So, to summarize what we see in truncation error is that we have greater the number of terms we
have lower is the error. Smaller is the value of a, we have lower error and when we plot
logarithm of error verses logarithm of a, we get a linear increase and the slope is n +1. The slope
is nothing but the order of accuracy okay. With that I come to the end of this lecture. In the next
lecture which also continuation of lecture 1. 2 where we are going to cover that round off error.
And we are going to see tradeoff between round off error and truncation error. So thank you and
see you in the next part of this lecture.

92
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department Of Chemical Engineering
Indian Institute of Technology- Madras

Module No. #02


Lecture No. #2.3
Errors and Approximations – Round off errors and Iterative methods

Hello and welcome to MATLAB programming for numerical computations. We are in module 2
errors and approximations. In lecture 2. 1 we introduced ourselves to errors and approximations
and how they arise in numerical computations. Lecture 2. 2 we considered truncation errors. We
have taken the example of Maclaurin series expansion of e ^ a and showed how including the
additional terms form the infinite series improves the accuracy of the approximate method that
we used.

In this method, in this lecture 2. 3 we are going to consider another type of errors which is
known as round off errors and we will see the tradeoff between round off and truncation errors.
There after we will talk about another type of method known as iterative method.
(Refer Slide Time: 01:01)

So, we will take the example from numerical differentiation. So instead of limit h tends to 0 in
the finite precision machine, we can calculate f `(x) approximately as f (x + h) - f (x), the whole
thing divided by h. This also can be derived from Taylor’s series. This is something that you

93
would have done in twelfth grade or your first year math class. We are not going to bother
ourselves with how to derive these equations.

We are going to use these equations in MATLAB. We are going to just remember what this
particular term means, order of accuracy when it comes to numerical methods okay. So let us go
to MATLAB and do this numerical derivative problem.

(Video Starts 01:52) Okay so the example that we will solve it in today’s lecture is going to be
tan inverse of x we want to find d by dx of tan inverse of x at x = 1 okay. So let us look at what
the command in MATLAB is to get tan inverse and that command is atan. So if we do atan of 1,
we will get the result as pi by 4 and that is what we get when we put atan of 1. Let us check this
by typing pi by 4 and as you can see atan of 1 is indeed pi by 4 okay. (Video Ends 02:30)

So now let us look at what we need to do. We need to use this particular approximate method in
order to get the derivative. Let us define our a. (Video Starts: 02:40) Let us clear all, let us define
our a =1. Our true value of the differential trueVal is going to be equal to 1 divided by 1 + a
square okay. Remember the brackets. So the trueVal = 0.5. So, the d / dx of tan -1(x) at x = 1 is
going to be equal to 0.5. Now we want to compute this numerically and the way we will compute
this numerically is, we will first define our h.

Let us start with h = 10 ^ -2, approxVal is going to be atan x + h rather atan a + h - f (h), which is
atan of a now the whole thing in brackets divided by h that is going to be our approxVal, the
error is going to be nothing but abs value of trueVal minus approxVal and the error is 0.0025.

Let us do this again. For a different value of h and that value of h is instead of 10 ^- 2, we will
take that value as 10 ^ -3 okay. We will again calculate our approxVal in the same manner and
we will calculate our error also in the same manner. So, what we see is when we decrease our h
by a factor of 10, our error is also decreasing from 2*10 ^ - 3 to 2 *10 ^ - 4 which is again a
factor of 10.

94
Let us decrease our h further. So we take the h as 10 ^ -4. approxVal calculate in the same
manner, error we calculate in the same manner. What we see is when we further decrease our h
by a factor of 10, the error also further decreases approximately by a factor of 10. (Video Ends
05:02)

Recall that this is the consequence of the fact that the method is h ^ 1 accurate which means that
when we decrease the h by a factor of 10, our error decreases by a factor of 10. When we
decrease h by a factor of 100, our error decreases by a factor of 100 so on and so forth. That is
the meaning of the order of accuracy of any numerical method.

(Video Starts 05:31) Okay so what we have done so far is, made all these computations on the
command prompt. Recall what we had said in the lecture on MATLAB functions and scripts.
MATLAB script is nothing but running all this commands one after the other in a file okay. So
what we can do is, we can copy and paste all of these in a file and that file will be a MATLAB
script.

That will run our commands in a sequential manner. So, let us edit the file will call it new
numDeriv to calculate numerical derivative. So let us go here h = 0. 0001. I am just pressing the
up arrow key. When I keep press f arrow key, will go over the previous commands that were
there in MATLAB. Sorry, okay so these are the numerical derivatives for h = 0.0001.

What we had also done is calculated this for 2 other values of h and we will do that here again.
We just copy pasted these things. This for h = 10 ^ -2, h = 10 ^ -3 and h = 10 ^ -4 okay. And we
will save this, will clear, clear all the variables and clc will clean up this screen for us okay.

So, now what i will do is, run this code numerical derivative a numDerive okay. Now when I run
this code, I expect an error. I will take up a couple of seconds, you can pause this video right now
and can you spot, what error we are going to get when we run this code okay. I will show you
what happens when we run this code and what error we are going to get.
So, let me click on run. So when I run this, I get an error. It says undefined function or variable a.
We did not get this error earlier, we got this error now. Why this is happening? What does this

95
error mean? What does error means is that the value of a has not been defined. Now why do you
thing we got this error but we did not get this error earlier.

The reason why we got this error is because just before typing numDerive we had typed clear all
which meant that all the variables that were there in MATLAB’s memory has been erased. As a
result of this, we were getting this error. We will cycle through that we had input earlier and we
will see that when we started this particular module, we had a command a = 1. We also had a
command trueVal = 1 / a2, 1 / (1+a2). We forgot to add this.

When we wrote this code, so let us go and type this in also and we will also have to type the
trueVal okay. So, now when we run this code, we should not get any error okay. So, remember
what we ended up doing and this is one of the more common errors that I will found. At least
people new to MATLAB main. So, sometimes we forget that some of the variables have not been
defined.

This is what how one should read, whenever we get an error, this states the first line after the
error states that what the error actually is. The error is that the variable a is not defined. What we
realized then is that earlier, all over codes they were running. The reason why they were running
but they stop running now is because we had given the clear all command.

Then we realized that will be started this MATLAB session, we had given a = 1 and as well as
commutated trueVal value. So, those when we insert in our MATLAB script, we have to run this.
We are going to get the result as required. So, let me run this and we will get this result as
required.

Now what I want to do is, why do I need to give this repeatedly in this particular manner, we
want to now put it in a for loop and the for loop will become for i equal to say 2 to 4. In this case
h is nothing but 10 ^ - i, approxVal, I will just copy and paste it over here and error I will just
copy and paste it over here okay and I will say end, I will delete this, saved this.

96
When I run, I am going to get the same results again. Let me run this again for you numDeriv.
And I am getting the same results as I had been getting before okay. So, next, what I want to do
is, I want to see how this error actually varies for a fairly a large number of step sizes that we
have taken. So instead of i going from 2 to 4, let me say i goes from say 2 to 7.

Let us say for example, okay what I am going to do also is, I am going to save my err and h in 2
vectors which I will call as hAll i -1 = h, errAll i - 1 = err. So these are just for storing the results.
And plot error verses step wise, we want to do a loglog plot of hAll in the x axis, errAll in the y
axis okay. And that is what we want to do and we want to see that this is a linear straight line
okay. So now let us run this and we get a straight line as seen. What here, the reason why we get
the straight lining is because. (Video Ends 13:06)

We are f `x. The error term is of the order of h which basically means when we plot the error
against h on a loglog scale, it is going to be a straight line with slope of 1.The straight line, the
slope is going to be the same as the order of the accuracy of the numerical scheme that we are
going to use okay. Next what we will do is.

(Video Starts 13:33) That is instead of carrying this out until 7, let us do this for a larger number
of terms. But will do it in steps of 2. Let us say and let us go to from 10 ^ -2 to 10 ^ – 14. I will
just make small changes in this code over here. Clear all clc, close all and let me run the
numDeriv code okay. And this is the result that we get.

As you see over here what we got when we decrease our h from 10 ^ - 2 to 10 ^ - 7 or in this case
of put in ^ -8. We get the steady decrease in the overall error as the steps wise decreases. But
after a certain point when we decrease the steps wise, error does not decrease any more. But the
error starts to increase.

The reason why this happens is because of round off errors, remember what we said in the first
lecture is that there is a machine precision; there is a least count for that machine at a certain
point. The round off error that dominates and beyond that point the error increases. So, when we
are going to do numerical derivates. We are going to start facing the issues of tradeoff between

97
round off and truncation errors and there is a steps size which is going to best for numerical
derivates.

This is something that we are going to cover in the next module. In module 3, on numerical
derivatives and numerical integration. Here I wanted to demonstrate to you that there is
something called truncation error, there is something called round off error and then that might
be tradeoffs between these 2 errors which will result in some unique properties of some of the
numerical methods. (Video Ends 15:34)

Okay so to go and recap what we did in numerical derivatives f `x, we calculated approximately
as f (x) +h – f(x) divided by h. We saw that the truncation error decreases with h truncation error
on a loglog scale, decreases linearly with h with a slope of 1. The machine precision determines
how small h we can use. We cannot use h to be really small because of machine precision. The
round off errors starts to accumulate. What we have covered so far is.
(Refer Slide Time 16:09)

What is known as direct methods? The first direct method was Maclaurin series expansion of e ^
a which was covered in the previous lecture. In this lecture we covered another direct method
which was computation of numerical derivates. Direct method is nothing but an algorithm which
directly computes a solution through a sequence of operations, a sequence of equations that we
keep executing.

98
But we execute them once through and we are done with it that is how we are going to compute
the solution using direct method. The second type of method that we are going to consider in this
lecture is what is known as iterative methods. Iterative methods we will start with certain initial
guess and we will use certain steps repeatedly or iteratively to improve that initial guess until we
reach a convergence.
(Refer Slide Time: 17:05)

Let us take an example of iterative method. As I mentioned before that example of initial
method that we are going to take is what is known as Heron’s algorithm. Heron’s algorithm is
arguably one of the first iterative numerical algorithms that were known to math. This was
discovered in around in 17th century BC in Alexandria.

It computes square root of 2 using an initial guess. Let us say the initial guess was 0.5. We use
that initial guess and we iteratively use this equation. So x = 1/2 (x + 2) / x is going to us the next
solution and we will keep going with this iterations till we had convinced that we are reached the
solution. So, let us go to MATLAB and do this okay.

(Video Starts 17:59) Will start with an initial value of x = 0.5 and we were going to iteratively
use this equation which is the new guess of x is half of current value + 2 by current value. So x

99
or let me just say xNew = 1/2 multiplied by x+ 2 / x okay. That is going to be xNew now because
this is an iterative method.

We do not need to call it with another name, we can just call it with the same name as before in
this particular example. So x is going to be average of x and 2 /x okay. So next value is 2.25. The
value of that is 1.56, the value of that is 1.4142 okay. And if we repeat it multiple times, we are
not going to get any more changes. So this is how an iterative method works.

We start with an initial guess and we iterate on that initial guess till the solution reaches a desired
accuracy. Let us put this all in MATLAB, edit heron Algorithm, calculate square root of 2 using
heron Algorithm. So let us have our initial guess as x = 0.5 say for i equal to let us say we expect
of to 25 iterations okay. xNew is going to be equal to half multiplied by x +2 / x. Our error is
nothing but abs of x -xNew and our x = xNew okay. So, let us type this of.

Maybe we should not do it by 25. We know that convergence very quickly. So let us just do it for
10 iterations and let us run this okay. So what we start off with the error was, 1.75 that is the
error between or the difference between xNew and xhold is 1.75 once 0.68, 1, 0.14, 0.0007, 2
*10 ^ - 4 so on and so forth.

So this is probably where we can say that we need to stop and that is based on a tolerance value.
So, let us go and change this a little bit. Let us say atol, the absolute tolerance value is 1.0 * e- 5
okay and instead of for, we will have a while loop. While err is greater than atol, we will keep
repeating this these steps. So we will stop these steps when the error falls below the tolerance
value, we also need to initialize the error. So we will just call the error, will just initialize this as
5 okay.

If we do not initialize this error okay, so, let us clear all heron Algorithms okay. So, as you can
see we started with large enough error and we stop at resulting value here okay. And this is where
we have use the while loop in order to compute the Heron’s algorithm. If instead of x equal to
say 0. 5, if we were to give a value of x = 0.1, let us see what happens.

100
Clear all Heron algorithm and again we converge to the desired solution in certain number of
steps. As you can see over here, if the farther we are away from the true solution, greater the
number of steps we are going to rewind okay. (Video Ends 22:38)

So, with that I come to the end this lecture. What we have discussed over here is, we look at
direct method and then we look at iterative method and we look all the criteria when do we stop
and say we have a solution. We will stop and say we have a solution is when the difference xNew
and xold the absolute value of difference falls below a certain tolerance threshold.

What we covered in this lecture was a direct method to compute numerical derivative and saw
the tradeoff between truncation and round off errors and then we saw an iterative method in
Heron’s algorithm okay. That completes our lecture 2 .3 and I will see you in the next lecture.
Thank you and good bye.

101
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, -Madras

Module No. #02


Lecture No. #2.4
Errors and Approximations – Step wise methods and error propagation

Hello and welcome to the MATLAB programming for numerical computations. We are in the
last lecture of module 2. In this module we are covering errors and approximations in this lecture
2 .4. We are going to cover what is known as step-wise methods. The step wise methods we start
with a certain initial condition.

Multiple steps go on from the initial condition and multiple steps go on from the initial condition
to a final point. The errors that come in step-wise method accumulate from one step to another
and we get a certain way in this error which prop again. This is what we are going to cover in
today’s lecture. This is what we are going to cover in today’s lecture.
(Refer Slide Time: 00:53)

Let us consider the Taylor’s series. Taylor’s series itself in which lecture 2.2, we have use to
derived the Lawrence series expansion of e ^ of 0.1, in that lecture we considered approximating
e ^ 0.1 in a single step.

102
In this case we are going to take multiple steps that means we start with e ^ 0 then go to e ^ 0 +
h, e ^ 2h and e ^ 3h and so on up to e ^ nh. We take multiple steps in order to go from 0 to 0.1
For example if we were to use the Taylor’s series expansion 10 times starting with a = 0 and h =
0.01 what we are going to get is, as follows.
(Refer Slide Time: 01:43).

So e ^ 0.01 is going to be 1 multiplied by 1+ h. Why do we get that is because using the Taylor’s
series expansion e ^ x+ h is f (x) which is e ^ x + h multiplied by f’(x). Therefore e ^ 0.01 is 1 * l
+ h. e ^ 0.02 is this value, e ^ 0.01 * 1+ h, e ^ 0.03 is e ^ 0.02 * 1+h and so on so forth. So at
each time the new expValue at the next point is going to be the expValue multiplied by 1+ h.

(Video Start: 02:33) Let us do this in MATLAB. The script called multiStepExp. Calculate e ^
0.1 using multiple steps of Taylor’s series okay. a = 0.1, h = 0.01, N, the number of steps are
going to be nothing but a divided by h okay. And trueVal is nothing but exp of a. Problem set up
multi-step computation okay.

We will have to use for loop, for i going from 1 to n because we are going to go from e ^ 0 to e ^
0.1 in n steps. So therefore we have for i=1 to e expVal is equal to expVal multiplied by 1 + h.
Remembered that is what said when we did in this MATLAB. At each time the new expVal is the
previous expVal * 1+ h okay.

103
And write this end calculate error and display so, let say calculate true error and display err equal
to abs value of trueVal -expVal. We run this we are likely to get in error. So just take a couple of
error and just pause for a second. You can pause this video; you can look at the score and think
about or write down why you think we are going to get an error okay.

So, let me save this and run this and let see what error we get. Okay we got an error. Can you
think of what error it might be undefined function or variable expVal. Remember in the previous
lecture that is the error we have obtained also why is because we have not initialized that. Let go
back to the power point and see. We start with e ^ 0, e ^ 0 at anything to the power 0; we know
is equal to 1.

So let start with expVal = 1. So that is our starting point and with this starting point, when we let
this run, we are going to get our error is 5.5 * 10 ^ -4. That is what we get. Whenever h was 0.01,
if we decrease the h to 0.001 and run this, the error is going to decrease the error goes 5.5*10 ^
-4 to 5.5 * 10 ^ -5.

So, it decreases by a factor of 10. If we were to change this loop that say h=0.1 and run this code,
the error from this point has increased by a factor of 10. When we increase the step size by a
factor of 10, the error increases by a factor of 10. When we decrease the step size by a factor of
10, the error decrease by a factor of 10. What do we mean by all this we mean is that the order of
accuracy of this method is h ^ 1 that we have seen in the previous couple of lectures?

That is the definition that is how we understand the order of accuracy of this method. So if we
want to retain of single term of Taylor’s series, the order of accuracy is going to be equal to h ^ 1.
What we are going to do is, we are going to convert this script that we have obtained into a
function and that function we are going to call multiple times with different step sizes.

What I mean by that is this, is the function usually contains the core of the computations which is
this part, the input and output are often place in a separate script. That is what we usually do in
larger programs and that is what you are very likely to do in the most of the assignment
submission that you are going to do of this particular course.

104
So, let us try and do that so what we are going to do is, we are going to modify multiStepExp
into a function that function will take as inputs. It will take the value of a and the value of h that
means does the final points and the step size going to take the order to x to reach the final point.
And it will retain the expVal at the final point okay. And all the input and output things we are
going to move that to a script. So, that is added edit called that as scriptExpResults okay.

To understand errors in multi-step method is this script to understand scripts and functions are
written in MATLAB projects. What I mean by projects is. If you have a large project that you
need to deliver then how we write scripts and functions. So we will as I said in lecture 1.1, in
large projects we will have one single script that script is going to be the driver script and
everything else will be put a function okay.

So the problem set up is going to be move from here to the script problem setup okay. Now this
we are going to converted in to a function okay. Here the result that we want is expVal equal to
multi step okay. The name of the function should be the same as the file name multiStepExp and
input for this are the final point a and step size in which we are going to reach a, okay.

Now what we need over here is, to calculate n also okay. So n is going to be equal to a/ h as we
have done earlier the output should not be moved over here. The output should be move to
calling function or to be sorry, to be calling script. So we are move this calling script. And now
the final thing that we need to do, we need to call this function that will compute the result for us
the function is going to be expVal is going to be multiStepExp a, h and we do not need the
computation of n over here.

Because n is not use at all computing using multiStepExp method okay. So this is what we have,
so, let save this okay. Run this script exp results with 0.1, we called 0.0052 as the error. If we
were to change this, we will get this 5 * 10 ^ -4 and if we are going to change this further, we are
again now going to get 5 * 10 ^ -5 exactly.

105
As we have got the 4. Okay so now you are going to do is use the script file in order to find out
the overall error varies with step size. So we are going to run this for multiple step size is not just
for a single step size for i=1, 2, 3, h is going to be equal to 10 ^ –i.

Remember this what we had done in the previous lecture also expVal is this guy and we will
move this error also inside the loop, error = abs of trueVal - expVal that is also moved over here.
I put a semi colon in front of it. So we need to display this okay and we need to store these
values, h all, we want to store h, errAll, we use that to store our errors and instead of calculated
true error and display we will say plot error verses step size okay.

And what I will do is loglog (hAll, errAll) as a blue color line okay. So let us see what happens
we actually do not need h because this h is defined over here. So we can delete this. That is clear
all and lets run this script, so this is how the error decreases with step size it is a linear line slope
= 1.
(Refer Slide Time: 13:41)

Now instead of single term if we had two terms of Taylor’s series expansion our results are going
to change little bit. e to the power 0.01 is going to be the initial guy multiplied by 1+ h+ 1/2 h 2.
This additional term comes in now, expVal come to be the old expVal again multiplied by 1 + h
over here is going to be (1 + h) + 0.5 * h 2. In order to do this val, we need to do is to go to the

106
multiStepExp and at this + 0.5 * h 2 and save this okay. Do not forget this to save this because if
you do not save it, we are going to get same results as we had before so now. We run this, we
would have run this for 2 terms either series expansion the first order term as the second order
term. What we want to do is if we want to dash red color line with square symbols, let us save
this.

We need to do one more thing, we need to type hold on. When we type hold on earlier, lot will be
held if we do not type, hold on the earlier plot will be over written in MATLAB okay. So let us
and we got the end results and let us look at the error graph. So, when we see error graph this is
what the results look like when we have 2 terms of the Taylor’s series expansion retain okay.

So let actually look at these error values of errAll, just print errAll at each step decreases by 2
orders magnitude this is 1.7 in to 10 ^ - 4. It goes to 1.8 *10 ^ - 6 and it will go to 1 .8 * 10 ^ – 8.
So let us try this errAll 3 is 1.8 *10 ^ -8.

What we see is every time we decrease the step size by a factor of 10, the error decreases by a
factor of 100 which means that the method is h ^ 2 accurate, when we retain 2 terms in the
Taylor’s series. Let we summarize the results that we have gotten.
(Refer Slide Time: 16:10)

107
So far what we have seen in the multistep Taylor’s series expansion is, it is not a local truncation
error but that is global truncation error. The global truncation error comes in because f of a + 2h
is affected by f of the error that was there f (a+ h) as well as it is affected by the error inherent in
the single step

So there is a accumulation of error that happens when we from 0.01, 0.02, 0.03, so on so forth.
The error keeps adding up at each and every distance and that why the global truncation error is
usually greater than the local truncation but the way the global truncation error behaves is
slightly different slightly different. That’s the way the local truncation error behaves. The local
truncation error if it is n+ 1 order accurate through the global truncation error, it would be the nth
order angle okay.

If you did not really understand the graph this point I would suggest you do not worry about it.
However I am presenting this to you because in the rest of the lecture. I am going to bring up the
term local truncation error and global truncation error and I just want to put a contacts in the
today’s lecture okay.

So again to recap the main aspect of what we have learn in today’s lecture is, how to go how to
use multistep methods and how to converts step in to functions and run that functions multiple

108
times. So that we can get trends how which the error changes with the steps. With that I come to
the end of lecture 2.4. So to summarize the module 2 what we started of it.
(Refer Slide Time: 18:01)

We talk about machine precision is nothing but least count of any computer we defined true
errors. The true error is the difference between x (true) and x (i). The current case of the
numerical techniques we use these true errors to analyze the errors and approximation in this
particular module to understand what we need by truncation errors and round of errors and error
propagation that the contact in which we use the term true error we use the term current
approximation error.

In order to design when to stop the aggregative method, when ei fell below certain threshold,
certain tolerance value as well we said that we exit the while loop, what we did in lecture 2.3.
(Refer Slide Time: 18:54)

109
Finally to summarize the various type of methods that we are going encounter in modules 3 to 8
are the direct methods or the direct methods we directly apply set of formulas or set of
expressions that we have derived and obtained the results in a direct method. In an iterative
method a set of formulae or set of expressions that we will do.

We apply this in a iterative manner again and again till the difference i plus 1 and xi falls below
the certain threshold. And finally today’s lecture we saw step wise method and we took example
of multistep Taylor’s series where we use the multistep Taylor’s series again and again until we
reach the final destination starting from an initial point. So with that I come to the end of lecture
2. 4 and indeed to the end of module 2.I will see you in the next module and thank you, bye
(Video Ends: 19:45)

110
MATLAB Programming For Numerical Computations
Dr.Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Help video to access MATLAB online

Hello and welcome to Matlab Programming for Numerical Computations. Several of you
have passed some questions regarding access to Matlab online. This video shows you
how you can get the access to Matlab online. In order to do that go to the unit that says
get Matlab online access submitting assignments and other help okay. (Video Starts:
00:24)

And click on the link that says how to get Matlab online access. In order to get access to
Matlab online you will have to follow couple of simple steps. In order to set up your
Mathworks account this is what you need to do. You only need to go and click on this
link and you will be prompted the next steps that you need to perform. If you already
have a Mathworks account you can go ahead and use that Mathworks account. If you do
not already have a Mathworks account you will be prompted to create one.

Let us see how this works okay. So go and click on this particular link over here. When
you click on this link this is what you will see. What you need to do is to enter the email
address that you want to register for this course with so in this case. And I will click on
continue okay. Now because I do not have a Mathworks account it is prompting me to
create one.

I am just putting last name as demo because this is a demonstration account okay. Select
your country and click on continue okay. This can be if you are not in India choose your
own country and you can click on continue and i will choose the country over here.

111
So, the next step for me is to verify the Mathworks account. Please note that if you
already have your Mathworks account you do not need to go through the step okay. What
I did in the video is I went on to my Gmail account. The email that came from
Mathworks had gone into my spam folder.

I retrieve that from my spam folder and clicked on the link that verify my email account.
Now that my email is verified I need to login using my email and the password that i had
earlier provided okay. I have put my email and password and I have clicked login okay.
Now at this stage it ask me do you already have Matlab?

Since, I do not already have a Matlab that’s the reason I am using Matlab online. So I will
just click on no thanks and this where i will go okay. So that completes the first part of
the step of the process that we talked about. Now that I have registered I can go back and
login to matlab.mathworks.com. Before that happens, we will have to complete a couple
of steps okay.

First is, I will have to registering login using my Mathworks account okay. Now in order
to rectify this issue lets go back into Matlab NPTEL okay. Click on this and again click
on set up your Mathworks account okay. Now we click on run Matlab for NPTEL classes.
I will be asked for my password and I click login.

Okay, once I click login, click on I agree click next and give your username okay. I have
given demo student as the username please give an appropriate username that is
identifiable with you. I now have access to Matlab online okay. So lets do some quick
code a = 1, b = 2, c = a + b okay that gives us this x = 0: 0.1:3 plot x, sin x and this shows
the plots as well. I can close this plot so now you have Matlab online access. (Video
Ends: 05:04)

112
MATLAB Programming for Numerical Computation
Dr.Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. # 03
Lecture No. #3.1
Numerical Differentiation – Differentiation in Single variable

Hello and welcome to MATLAB programming for numerical computations. We are in module 3
and in this module we are considering numerical differentiation and numerical integration. In the
first 3 lectures of this module, we will go over numerical differentiation and the in the final 3
lectures of this module we will go over numerical integration.

This is lecture 3.1. In this lecture, we are going to cover differentiation in single variable.
Primarily this lecture is going to focus on first differences that is f `x given f (x). In module 2, we
had covered forward difference formula for f(x). So the forward difference for f (x) is given over
here. f `(x) is given by f(x+ h) -f (x) the whole thing divided by h. We saw that this method was
order h ^ 1 accurate. That was the forward difference formula.
(Refer Slide Time: 01:13)

The other formulae for first derivatives are given over here. The forward difference formula as
we just said was f(x+h) - f(x) the whole thing divided by h the accuracy is h ^ 1. Central
difference formula is more accurate than the forward difference formula. And the central

113
difference formula is f(x + h) – f(x – h) divided by 2h. What that means is, if you want to find a
derivative at f(x), we take a 1 point before that particular 1 point, after that particular point and
find a slope using those 2 points. That is what a central difference formula is.

You probably already know from your mathematics course, that central difference formula is
more accurate than the forward difference formula. It is h ^ 2 accurate. What that means is every
time we decrease the h by a factor of 10, the error also decreases by a factor of 100, not factor of
10. Why that is so, that is because it is h ^ 2 accurate and the backward difference formula is
given over here.
(Refer Slide Time: 02:25)

What we are going to do is, we will take the same example that we covered in module 2. That is
tan inverse of x and we will find out the forward, central and backward difference formulae for
that particular problem. We will do that for h ^ – 4. So let us go to MATLAB and do this
problem.

(Video Starts 02:46) So, edit numDiff1 okay. Comparing numerical differentiation formulae for f
` x, f (x) = tan inverse x at x = 1 okay. So a = 1 and trueVal is going to be equal to 1 /1+a 2
squared. And our h is 1.0 * e- 4 okay. The forward difference formula.

114
Okay, forward difference formula is tan inverse of a+ h – tan-1(a) divided by h okay. FwdDiff
equal to a tan (a + h) - a tan (a) divided by h okay. And we need to put brackets over here
because the entire numerator needs to be divided by h. Error in forward difference formula is abs
of trueVal - fwdDiff okay. Central difference ctrDiff equal to let us go over here, a tan (a + h) - a
tan (a- h) divided by 2h okay.

So a tan (a + h) - a tan (a - h) okay. This needs to again go in brackets we remember divided by h


sorry, divided by 2 multiply by h. And again the denominator also goes in brackets, errCtr,
remember these brackets in both the numerator as well as the denominator okay.

Error in central difference formula is abs trueVal - ctrDiff okay and we will just copy and paste
this for backward difference formula. Backward difference formula bkdDiff is, bkdDiff is a tan
(a) - a tan (a –h) divided by h. So let us make that change. So a tan (a) - a tan ( a- h)/h. errBkd,
bkd is absolute value of trueVal- bkdDiff okay. So let us save this and let us run this and see the
results okay. So we have run this and we now have the results over here.

The error in backward difference formula is 2.5 into 10 ^ -5. Central difference formula is 8 *10
^ -10, the forward difference formula is also 2.5 into 10 ^ – 5. If we want we can go over here
and we can display the results using disp. As we had seen earlier, disp error in forward difference
is num2str, errFwd close this and this will display the error.

And likewise disp error in central difference is num2str, errFwd. Let me not change this errFwd
and just run it and see what actually happens okay. You know what to expect over here. I just
want to show that because this is actually a very common error. I mean come to think of it, you
will think that these are small things that we will not make an error.

But these are again small things, its human exercise, writing a code is a human exercise and
these things are ought to happen. So, I am just showing you some of the common things that the
end of doing. In fact, I myself do this type of errors a few times and these errors sometimes are
fairly difficult to detect. So let me run this and see what happens ok.

115
So, what happens when I run this is error in forward difference formula is, 2 .5* 10 ^ - 5 and
error in central difference formula again is 2.5*10 ^ –5. So this should raise a suspicion. So, why
are you getting the same results and we know why we are getting the same results because, not
because we made a mistake in calculating the central difference formula. We forgot to change
this errFwd to errCtr okay.

So, we just do that save and run this. And now we will get the correct results. Error in forward
difference is 2.5*10 ^- 5 and error in central difference is 8.3*10 ^ -10. So we see that the central
difference formula is much more accurate than the forward difference and the backward
difference formula okay. So, we finished this particular example. (Video Ends 09:22)
(Refer Slide Time: 09:26)

So what we have seen is, we have done this particular example for 10 ^ -4. What we want to do
now is, repeat for a range of values. Recall that we are done this in module 2 using a for loop.
Now we are going to do this not using a for loop but using array operations.

(Video Starts 09:49) Ok and let us go over here. What I will do is, I will comment out all of this
so that we only focus ourselves on forward difference formula. You right click and click on
comment. Once you do that the entire thing will become be commented out and this will not run.
I will delete this display also. Because we do not need okay.

116
So, now what we want to do is, we want to do this for h = 10 ^ – 1, 10 ^ -2, 10 ^ – 3, - 4 and - 5
okay. We can do this using array operation. So instead of giving h as a single value, we can give
h as an array 10 ^ – 1 in steps of -1 up to – 5 okay. So this guy is going to give us a vector which
is – 1, - 2, - 3, - 4, - 5.

We are going an element by element power. This is important, we are doing an element by
element power a of 10. So which will lead to our h be equal to 10 ^ - 1 to 2, 3, 4 and 5? Let us
highlight this and evaluate this right click evaluate selection and see what we get indeed we get h
equal to 10 ^ – 1, - 2, - 3, - 4, - 5 okay.

Let us go over here and look at what happens. When we give this numerator, let me highlight just
the numerator. Right click along with parenthesis, let me highlight it right click and click on
evaluate selection okay. Now we have not defined our a, so a = 1 sorry about that okay. And let
us revaluate this particular guy and press enter okay. We again get a vector okay. a tan (a+h) – a.
tan (a) is a vector.

So, now what we are doing is we have a 1/5 vector in the numerator and we have a 1/5 vector in
the denominator. And we are dividing it okay. There is another problem over here. We need to
make another change okay. I will not tell what the change to be made is. Can you spot what other
change need to be made? Okay.

I will pause; you can pause this video and think about what is one more change that we need to
be made in order to compute the forward difference formula over here. I will run this code and
let us see what actually happens. What we are expecting is forward difference formula to be a 1/5
vector. Let us see what we get. FwdDiff, forward diff is not a 1/ 5 vector but instead is just a
single value okay.

And the reason for this is by doing this slash we are not taking an element by element division.
But actually we are doing what is known as least squares. Do not worry about what we are
actually doing over here. This is something that we will probably cover in module 6 of this
course okay.

117
You should not be using slash for element by element division. But you should be using dot slash
because we want to do an element by element division. Let us save this and run this and see what
results we get okay. Let us plot on a loglog plot h verses errFwd. And we will get this straight
line.

Remember this is the result that we had already obtained in module 2. We had done that using a
for loop. Now we have resolved it using array operations in MATLAB okay. (Video Ends 13:58)

So let us now look at this for a range of values of h from 10 ^ - 1 to let us say 10 ^ - 12 okay.
(Video Starts 14:11) But before that we will also do this for central and backward difference
formula. So let me highlight this. Right click and click on uncomment okay. Remember all the
thing that we needed to change is be needed to convert our h into a vector.

And the only thing that we needed to change over here was a dot slash, nothing else was required
and that is because a tan itself takes when it is gets a vector. It will give a vector as a result where
tangent values obtained it for an each element and 2* h. We do not need a dot star because this is
a scalar multiplication. So none of the other things are changed and let us do this.

Let us plot the results. We need to plot at on a loglog plot h. fw sorry, errFwd, blue line h, errCtr.
Let us say a-redline and h errBkd as a . - magenta line okay. Save and run this and see what
results we are getting okay. The magenta line and the blue line lie almost on top of each other
okay. And the errors in forward and backward difference formulas are greater than the error that
we see in central difference formula okay.

Now let us increase this to, let us say 10 ^ - 10 and see what we get okay. If you recall what we
have done in module 2, what we should be getting is, we will be getting minima in error at
certain value of h. The value of h is going to be different for forward difference formula and for
central difference formula. That is what we will see. So let us clear all okay.

118
Let us save this and let us run okay. So what we get is, that the error in forward and central,
backward difference formula. All error in central difference formula also falls. And there is a
minimal. The minima for forward difference and backward difference formula happen at 10 ^ –
8. Whereas the minima for central difference formula happen at 10 ^ - 5 okay.

The actual place where the minima happen is eps ^ 1 /2 for forward and central forward and
backward difference formula and at 1/ 3 for central difference formula. So the minimum in the
error is at 10 ^ –8 for forward difference formula. And approximately around 10 ^ -5 or 10 ^ -6
for the central difference formula okay. (Video Ends 17:16)
(Refer Slide Time: 17:18)

Let us see what we have. So recall module 2, tradeoff between truncation and round off error. We
see similar tradeoffs in forward and central difference formula also okay.
(Refer Slide Time: 17:29)

119
So this the theoretical ideas behind this were covered in computational techniques course module
6 part 2 the link for which is given over here. So if you are interested in getting a theoretical
understanding of why we get minima at a various values for forward and central and backward
difference formula, you can go to this video lectures and view those video lectures okay.

In that lecture, what we have done is, we have taken f(x) = 2 - x + ln x and computed forward,
central and backward difference formula for that particular problem. And the true solution is
given by 1 / x - 1 okay. So this is a problem that we may want to solve for practice okay. So with
that I come to the end of lecture 3.1. In this lecture, we have covered forward, backward and
central difference formula for calculating f `( x). Thank you and see you in the next lecture.

120
MATLAB Programming for Numerical Computation
Dr.Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #03


Lecture No. #3.2
Numerical Differentiation – Differentiation in Single variable – 2

Hello and welcome to MATLAB programming for numerical computations. We are in module 3.
In the first part of this module we are doing numerical differentiation in lecture 3.1. We
considered how to compute f `(x) for a given function. In this particular module we are going to
go forward and take 1 example which is of a practical nature as well as for a single variable
problem find higher derivatives. Specifically, we are going to just look at f ``(x) for a couple of
example problems.
(Refer Slide Time: 00:47)

So, in previous lecture we have seen how to compute the first derivative. Now instead if we want
to compute second derivative using central difference formula, we get f `` x is f(x+ h) - 2 f(x) +
f(x- h) / h2. Instead of writing (x+ h) and (x- h), I have given subscripts i+1, i and i- 1.

This central difference formula just as we had seen in the previous lecture is h ^ 2 accurate. We
recall in the previous lecture f`(x) was given as f(x) i+ 1- f x i- 1 whole thing divided by 2h. That

121
was the first difference, first derivative using the central difference formula. This is the formula
for the second derivative; this is the formula for the third derivative and so on and so forth.

We can compute formulae for higher derivatives, over here this should be h cubed and not h
Squared. In this lecture we are only going to concern our self with the second derivative over
here.
(Refer Slide Time: 01:59)

So, let us look at the problem again f(x) =to tan inverse of x and lets compute f ``(x). (Video
Starts 02:08) Okay let us go on to MATLAB and we wanted to find f ``(x) at x = a and a is 1.
Lets a h was 1 e- 4. (Video Ends: 02:25)

So, we wanted to compute f `` tan inverse f `` is given by this particular formula. (Video Starts
02:32) So trueVal = 2 multiplied by a divided by 1+a 2, the whole squared a negative of that. And
I will just put a negative sign. This is what our trueVal is. So trueVal is -0.5. (Video Ends: 02:53)

Let us compute the numerical derivative using the formula given over here. That is (a+ h) - 2f (a)
+ f (a- h) / h2. f is nothing but a tan. (Video Starts 03:07) So, numDiff = atan (a+ h) - 2 times atan
(a) - 2 * atan (a) + atan (a- h), a- h, let us put this all in brackets divided by h squared.

122
That’s going to be our numDiff. And numDiff is approximately equal to -0.5. The error between
the difference between trueVal and numDiff is absval of sorry, abs (trueVal- numDiff). And the
error is of the order of 1. 4* 10 ^ -8. (Video Ends 04:12)

So now that we have considered f ``(x) for tan inverse of x. Let us go on to the next example.
(Refer Slide Time: 04:23)

This example was taken from the computational techniques course module 6 part 2. For this f(x)
is 2-(x+ ln x). What we are going to do today is, find f `x and f ``x for this particular function
using central derivatives, I recommend what you may probably do at this stage, if you have not
already tried to solve this particular problem from the previous lecture, is perhaps you can pause
this lecture and take a few minutes in order to try solving f `x and f ``x for this particular
example.

You might probably do it for just 1 value of step size h, let us say you take h = 10 ^ - 4 and try to
solve this problem. Meanwhile I will give you a solution for this problem in MATLAB as well.
(Video Starts 05:22). Okay, I have cleared the screen. Let us say edit numDiff2, numerical
differentiation for the second problem 2- (x+ ln x).
Let us say we wanted to find this at, let us perhaps say that we want wanted to compute this
again. At x, a = 1 and h = 1 e- 4. Let us say we call f(x+ h) as f1, f(x) = f2 and f(x- h) = f3. So

123
that is going to be 2-a. Sorry, 2- (a+ h) in fact I write at this way x = a+ h, f1 = 2- (x+ log(x)).
And we copy paste. x = a and f2 = this, x = a- h and f3 = this.

TrueVal, trueVal 1 let us called as for f ` and that is going to be 1 /(x- 1). So 1 / (a- 1). trueVal2 is
– 1/ a2. So this is what we need for numerical derivatives. numDiff1 is nothing but. (Video Ends
04:12)
(Refer Slide Time: 07:46)

Yeah so f` x was f1- f3 divided by 2h. (Video Starts 07:55) f1- f3 divided by 2 multiplied by h.
numDiff2 was f1- 2f2+ f3, f1- 2 multiplied by f2+ f3 divided by h squared. That is our numDiff2.
err1 = abs (trueVal1- numDiff1). Actually, rather than saying numDiff, we will call this as
numVal.

Press shift and enter. So all the values will get changed and the numVal2. So that is err1 and err2
= abs (trueVal1- sorry trueVal2- numVal2).Let save this and run this. So we will get err1 is 3 *10
^- 9, err2 is 5 * 10 ^ 9. If we check numVal1 that is 3*10 ^- 9, when we expect trueVal1 as 0,
numVal2 is- 1.
We are expecting trueVal also to be sorry, trueVal2 also to be equal to - 1. So as we can see that
the errors using central difference formula are for h = 10 ^ - 4 are very small. (Video ends 10:05)

124
So now that we have completed the numerical differentiation for the second example 2-(x + ln
x).Let us now head on over and look at a practical problem of a practical interest.
(Refer Slide Time: 10:17)

This comes from chemistry. As you all know that the rate of reaction can be return in the
Arrhenius form given over here. So, rate r is k multiplied by e ^ - e / rt multiplied by the
concentration to the power 1.25. The rate constant k= 1000 and e/r = 2500. Let us go on
MATLAB and try to solve this problem.

(Video starts 10:46) And edit reaction rate differentiation r = k multiplied by e ^ - e / rt * c ^


1.25. Constants a equal to and e by r equal to, so, k was 1000 and e / r is 2500. Differentiation do
this at c = 1. 0 and t = 600 right. Okay, so now the analytical derivative for this guy. So
temperature is the thing that is vary so because its - e ^ e / rt it is basically going to be e / r t
squared okay multiplied by the rate r itself.

So, let us first calculate the rate r = k * exp - e/ r / t, multiplied by c ^ 1.25. That is the rate,
trueVal is just going to be equal to r multiplied by e / r / t 2. That is our trueVal. Now then for the
numerical differentiation we want to calculate r1 and r1 is going to be nothing but k multiplied
by e to the power this guy. But instead of t we will have (t+ h). So let us just copy this. And we
will put (t+ h) over here using central difference formula. Let us copy and paste this over here.
And r2 is going to be this but it is going to be (t- h).

125
And numVal is going to be equal to nothing but r2- sorry, r1- r2 / 2 * h. Now the question is what
is the value of h that we need to take. So, h that is just take that as we had been doing this before
let us just take h = 1 * 10 ^- 4. And error is going to be equal to abs (trueVal- numVal ).

Let us save and run this problem and we hope that there is no error and this runs and the error,
that is the difference between the trueVal and numVal is 4 * 10 ^ -11. The trueVal is 0. 1077 and
numVal will also look same 0. 1077. (Video ends 14:32)

So, that completes our exercise of finding dr / dt at t = 600 kelvins using the central difference
formula. Where, the r is the rate of chemical reaction of the Arrhenius form. What you can try to
do is, see the effect of other values of h how increasing or decreasing the value of h changes the
errors in dr / dt.

We had seen in the previous lecture is that the optimal value of h is machine precision to the
power 1/3 for central difference formula for f `(x). The machine precision to the power 1/3 is
when the overall temperature or x, the independent variable is of the order 1. If the independent
variable is of a different order, the optimal value of h changes accordingly, prior to look at this
example.

We look at the other example 2- (x+ ln x) and computed f `(x) as well as f ``(x) for central
difference formula of f`(x). The optimum value of h happens to be epsilon ^ 1 /3. Whereas, for
the second derivative central difference formula the optimal value of h is epsilon ^ 1/ 4. This is
also something that you can probably verify as a practice problem.
In the next lecture, we are going to revisit this physical example of this rate of reaction. With this
I come to the end of lecture 3. 2 and I will see you in the next lecture. Thank you and good bye.

126
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #03


Lecture No. #3.3
Numerical Differentiation – Partial Derivatives

Hello and welcome to MATLAB programming for numerical computations. We are in the first
part of module 3, in this part, we are considering numerical differentiation in the first 2 lectures.
We considered numerical differentiation in single variable. We look at obtaining df/dx and d
square f/dx square using forward, backward and central difference formula.

In today’s lecture we are going to extend an idea to finding partial derivatives. Partial derivatives
when the function f is a function of multiple variables, so f is a function of x1, x2 and so on.
Before we do that we will go over the example that we took in the previous lecture. Practical
example of rate of reaction.
(Refer Slide Time: 1:00)

We wanted to find its derivative with respect to temperature t at t = 600. We use the central
difference formula with h=10 ^ - 4. At the end of this lecture I have suggested that as a practice
what you could do is, you could try to solve this problem for various different values of h. In
today’s lecture we will spend a few minutes time to look back at that same example. Modifying

127
the examples to choose different values of h and obtain what is the optimal value of h that we
need to use.

We are not going to look into the theory behind it. We are interested in practical aspects of how
you will apply this theory in order to solve problems using MATLAB. In order to do that I am
going to give you a certain set of steps. What we will find is that the judicious way of choosing h
for central difference formula is, choose h as temperature t * 10 ^ - 6.

If we were using forward or backward difference formulae, then the optimal value of h would be
t * 10 ^ - 8. Where does this come from? This comes from the fact that the optimal value of h is
proportional to the machine precision to the power 1 / 3. The temperature is not of an order 1 but
is of order 100.

We need to proportionately increase h as well ok. So this is the practical tip that we are going to
use and we will see how this problem works out by going to MATLAB and trying to solve this
problem.

(Video starts 02:44) So, let us head on over to MATLAB okay. So this was the code that we used
in the previous lecture. What I am going to do is, now modify the code for different values of h.
Different values of h that I am going to take are as follows 1e- 3 to 1e- 7. I am going to multiply
this with t okay. So, the value of the independent variable temperature at which we want to find
out the first derivative that is the value that we are going to multiply our h with okay.

Now because now our step size is a vector, we have seen this in the previous lecture. What we
need to do in order to obtain all these results at one go. What we need to do is, we need to
replace this slash with dot slash okay. This guy is a scalar. So, scalar when we want to divide that
by a vector, we need a dot slash.

So in the parenthesis what we get is, going to be a vector. So the thing that I have highlighted is a
vector, exp function, exp works on a vector as well and it’s element by element exponent of that

128
vector. So, this is also going to give us a 1/5 vector. We are doing scalar multiplication with k
and c. So we will not need to change this star to dot star at all okay.

Same changes we need to make in this expression as well we change a slash to dot slash and no
other difference we have, no other change we need to make at all. And finally numVal, the
numerator is a vector, the denominator is also a vector. So need an element by element division
and we obtain that by a dot slash over here. None of the other things need to be changed, okay.

So let me save this and run this and let us see after running what are the various errors that we
get for the various values of h. So, when we run this, I am displaying the various errors. We see
that the best case errors are over here okay. For these 2 values of h we have the best case errors.
If I type out h what I get is that I get best case errors for 6*10 ^- 3 and 6*10 ^- 4. So h we do not
choose blindly. (Video ends 05:26)

But we need to choose our optimal h as machine precision to the power 1/3 multiplied by the
absolute value of the independent variable of interest. The take home message is then there is an
optimal value of h for which the central difference or the forward difference formula gives us the
best results. So let us go on to this particular lecture.
(Refer Slide Time: 05:52)

129
In this particular lecture we want to find out df /dx. When f is a function of multiple variables as
a matter of fact, we have done this already in the previous example. In previous example if you
think about it the rate expression is a function of both temperature and concentration.

(Video Starts 06:12) So what we had done in this particular MATLAB code was this is we held
the concentration value constant at 1.0 and varied temperature to t + h, t- h and found the central
derivative. (Video Ends 06:28)

So that is what a definition of partial differentiation is. Partial differentiation means δf/δx1
keeping x2, x3 and so on. Constant δf /δx2 keeping x1, x3, and x4 so on and so forth okay. So
that is the definition of partial derivatives. In order to calculate δf/δx1, we hold x2 constant and
vary our x1 in order to calculate δf/δx1, we hold our x1 constant and vary our x2.
(Refer Slide Time: 07:04)

We will notice that a partial differentiation is kind of like finding the gradient of a scalar. The
gradient of the scalar is nothing but δf/δx1 and δf/δx2 return as a row vector. Let us go to
MATLAB and solve this problem for finding partial derivatives. (Video Starts 07:24). Okay so,
our a, the value of x at which we want to find δf/δx. So we want to find δf/δx at x= a, a is going
to be a vector.

130
Let us say we wanted to find this at 0.5 and 1. So, our a is 0.5 and 1. Our trueVal or our h steps
size should also be a two value vector 1e- 6 and 1e- 6 as well okay. So, trueVal cosx1 * e ^ - x2.
So cos (a1) * exp -a2 and the second guy is -sinx1 e ^ - x2- sin a1 * exp- a2 okay.

Partial derivative with respect to first variable okay, we will choose our x is going to be equal to,
x is going to be equal to a and x1 is going to be equal to (a1 + h1) okay. Now our f1 is going to
be equal to sin of x1 * exp of- x2 okay. So, this 1 is at (x1 + h) and x2. Likewise we need to
calculate our f2 at (x1- h) and x2.

So let us copy and paste this x1 = (a- h) and f2 is sin(x1) - e ^ - x2. And our numDiff 1 is going
to be nothing but f2, sorry, (f1- f2) divided by 2h by the central difference formula. Then copy,
paste this and say that. We want to find partial derivative with respect to second variable is this
function f (x). Rather than we give this function in this particular form, its recommended that we
give our half a separate function file where, we define sin x1 and e ^ - x2 as that function.

So for the second partial derivative, the partial derivative in the second variable that is what I am
going to do. So I am going to create a function. Function fval = myFunM3. My fun for module 3
and my input argument is x. Now x is a vector. x are not 2 values, these are not 2 variables but x
is a single variable okay.

And it is a vector variable and what I need to do is, we need to write fval is going to be nothing
but sin(x1) * exp of -x2; end and save this as myFunM3 okay. What we want to do over here is,
we do it in a slightly different way. What we will do is, we will define our h temp, h temp_h = 0,
h2 okay.

This is a better way of doing things because when we do(x + temp h), the first guy is going to be
just x1; the second guy is going to be (x2 + h2) okay. Our f1 is going to be nothing but our
myFunM3 computed at x1, (x2 + h) which is nothing but (a + temp h) okay. And our f2 is
nothing but myFunM3 (a - temp h) and we will have numDiff2 as (f1 - f2) / 2 * h2 okay.

131
And the same thing that we need to do over here. This also should be 2 * h1 okay. So this is the
partial derivatives that’s display results. So the first thing we want to display is error. err = abs
(trueVal- numDiff). So let us save this and let us run this. Clear all clc, partial differentiation. Let
us hope that this runs without an error okay.

So this the result if you see this particular guy difference between trueVal and numDiff turns out
to be of the order of 10 ^ - 11 and the numerical derivative is 0.32 and – 0.17 okay. So this is
how we take the partial derivatives okay. Now let us move on. (Video ends 14:00)
(Refer Slide Time: 14:06)

So will take the same example for rate of reaction. Where, k, as before was 1000, e/r was 2500.
And we need to find δr / δt and δr / δc at t= 600 and c= 1. That is what we need to do over here.
What I will recommend you do is, you can pause this video for a short period, you can go on to
MATLAB.

(Video starts 14:36). And you have this MATLAB code over here. What I recommend you do is,
you can take this particular part of that code and try to modify it to solve this problem okay.
Okay so, what I will do is create a new script. I will copy this partial differentiation okay. So our
h value is going to be 1e- 6 * t. And the other guy is going to be 1e- 6, that is going to be our h
values okay. numDiff1 is going to be myRate x1- myRate x2 divided by, let me call this as h1
and numDiff2. I will call this as also the same but divided by h2, divided by h2 okay.

132
So what exactly am I doing over here, well let me, will just first save this, reaction partial diff is
the name of this script and the name of that function is function r = myRate x okay. e = x1, c =
x2 and as we had over here, our r is this guy. Let us go over here and remove this from here and
move this to this file, constants, current value of t and c, rate calculation okay.

I will save this as myRate okay. So when I call this myRate with x1, what it should give me?
What it should give me is, r calculated at (t + h) and c and when I use this at x2, it should give
me r calculated at (t- h) and c okay. So that is what I will do. x1 is going to be equal to c sorry, (t
+ h1) and c okay, c is going to be kept constant over here. So (t- h1) and c okay. So as you can
see the value of c is kept constant over here.

And value of t becomes (t + h) and (t- h) that goes in the numerator divided by 2 * h should go in
the denominator over here okay. Likewise what we do over here is, x1 = t. That t remains
constant at our original value and (c + h2) and x2 is nothing but t and (c- h2) okay. And this
becomes numDiff.

Display results, partial derivative num2str, numDiff okay. Let us save this and let us run this
okay. And when we have run this the partial derivatives of r was 0.1077 and with respect to
temperature and with respect to concentration it is 38.76 so these are the partial derivatives of r
with respect to t and c. (Video ends 20:20)

With that I come to the end of this lecture and this also concludes numerical derivative parts of
this module. In the next lecture of this module, I will start with numerical integration. See you in
the next lecture. Thanks and bye.

133
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #03


Lecture No. #3.4
Numerical Integration – Newton Cotes Integration Formulae

Hello and welcome to MATLAB programming for numerical computations. We are in the second
half of module3. In this part we are going to cover numerical integration. In today’s lecture we
are going to cover range of integration formulae known as Newton-cotes integration formulae.
(Refer Slide Time: 00:30)

Numerical integration is primarily just finding area under a curve. For example, if a curve is f(x),
then integral f(x) dx from a to b is nothing but this shaded area that you can see between the
points a and b lie below this curve.
(Refer Slide Time: 00:51)

134
So if we were to be use trapezoidal rule for this particular example, what trapezoidal rule
amounts to is calculating the area under this trapezoid. The trapezoid is formed by joining the
dots f (a) with f (b) as you can see in this cartoon over here, if we apply a single application of
the trapezoidal rule, we are going to be fairly inaccurate the results are not going to match
closely the actual values of integral.

So trapezoidal rule basically as we said is just joining 2 dots with a straight line and finding the
area under them. Instead we can take 3 points, the point over here and the point over here and the
midpoint. Join them 3 points, we can join with the second order curve and the area under that
curve is going to give us what is known as Simpsons 1 /3rd rule.
(Refer Slide Time: 00:54)

135
So Simpsons 1 /3rd rule is obtained by joining point a, point b and the midpoint between a and b.
These 3 points adjoined by a curve. And what we obtained is known as the Simpson’s 1/3rd rule.
So the step size h in Simpson’s 1/3rd rule single application is half of that of the trapezoidal rule.
The next in Newton-cotes formula can be obtained by connecting 4 points within this region.

So if I just go back, so we had in trapezoidal rule this two points and Simpson’s 1/3rd rule. One
more point at the center in Simpson’s 3/8th rule. What we will have is these 2 points and then
two more points that are equally spaced. So if we take this point, if we take the next point at h by
3 and third point at 2h/ 3 and at fourth point at this guy okay.
(Refer Slide Time: 02:52)

136
And if we were to connect all of these through a smooth third order curve, the area under that
curve will be give us Simpson’s 3/8th rule. In a single application of Simpson’s 3/8th rule the
step size h is going to be b- a / 3.

So the step size in Simpson’s single application of Simpson’s 3/8th rule is, one third of the step
size that we used in the trapezoidal rule. We can of course have higher order formulae. So instead
of 4 points if we are have 5 points and connect them with the fourth order curve, 6 points
connecting them with the fifth order curve so on and so forth. We will give us higher and higher
order Newton-cotes formulae.
(Refer Slide Time: 03:28)

So the trapezoidal rule integral from a to (a + h) f(x) dx is going to be h/2 f (a ) + f(a + h). What
this means is, it is an area under this trapezoid, so it is this height multiplied by this height + this
height multiplied by this base divided by 2 that is what the area under the trapezoidal is.
(Refer Slide Time: 03:54)

137
The Simpson’s 1/3rd rule is given over here is, h /3 * function value f (a) + 4*f (a) + h + f (a +
2h). That is what the 1/3rd rule is going to give us, f (a) + 4 *f (a + h) + 1 * f (a + 2h) the whole
thing multiplied by h /3. It will give us Simpson’s 1/3rd rule and Simpson’s 3/8th rule. For the
sake of completeness I am just showing over here. We will not cover Simpson’s 3/8th rule in this
lecture.
(Refer Slide Time: 04:09)

So these are the Newton-cotes integral for integration formulae.


(Refer Slide Time: 04:33)

138
To summarize the formulae and local truncation errors the trapezoidal rule formula is this local
truncation error is the h ^ 3. So the trapezoidal rule from the local truncation error, single
application of trapezoidal rule is h ^ 3 accurate, single application of Simpson’s 1/3rd rule is h ^
5 accurate.

So it is significantly more accurate than the trapezoidal rule. Simpson’s 3/8th rule is also h ^ 5
accurate. As a result of this usually Simpson’s 1/3rd rule is more popular than Simpson’s 3/8th
rule and for the simplicity of application the trapezoidal rule is also very popular.
(Refer Slide Time: 05:17)

139
Let us take up an example and solve that in MATLAB. The theoretical aspects of this work
covered in module 6 of computational techniques specifically in module 6 part 3. What we are
going to focus on is, we are going to calculate integral from 1 to 2 f (x). f (x) dx using the
trapezoidal rule and the Simpson’s 1/3rd rule. Integral for this f(x) is given over here. So let us
go to MATLAB okay and do this problem.

(Video Starts: 05:44) Edit numIntegral of Newton-cotes formula, f(x) equal to okay, so let us say
a = 1, b = 2, trapezoidal rule, 1/3rd rule. trueVal is going to be equal to x- x2 / 2 * x lnx. So b- b2 /
2 + b * log b. If you recall, log is gives the natural logarithm of. So you can do help log. If we
wanted the log to the base of 10 instead we would use the command log10 okay. so this - a- a
squared by 2 + a * log a that is going to be our trueVal.

Okay, so for trapezoidal rule single application, h is going to be nothing but b-a okay. Itrap is
going to be equal to, let me put underscore over here. Itrap is going to be equal to h / 2 * f(a)+ f(a
+ h) = h / 2 * f (a) + f (b) and we need to put inside a bracket okay. Where f (a) is nothing but 2-
(x + lnx) calculated at a. So it is 2 – (a + log a) okay. And f (b) is nothing but 2-(b + log b) okay.

So let us save this err_trap = abs (trueVal- Itrap) okay. Let us uncomment both of these. Save this
and let us run this and see what results we get whether we get an error or we can get results. So
let us run this. It runs properly and the error is of the order of 0.04. So this error is quite
substantial. (Video Ends: 09:11).

As we had seen in the cartoon over here the single application of trapezoidal rule sometimes can
be very inaccurate okay. (Video Starts: 09:25) So the single application of, so as we have seen
over here, the trueVal of the integral is 0.8863. Whereas, the approximate val from trapezoidal
rule is 0.8466. Yeah so let us go over here. What we want to do is, something that we had done
earlier in the previous lecture.

What we want to do is, instead of calculating the functional value is inline, like this we will want
to give a different function to calculate f(x) okay, so let us create a new function called
myFunIntegral. So function fval = myFunInt x. fval = 2-(x + lnx), 2- (x + log(x)) end and save

140
this as myFunInt. And f (a) is nothing but myFunInt a. And this guy is nothing but myFunInt b
okay. Save this and run this and see what we should get.

If everything runs fine, we should get same values as before. And that is exactly what we get the
error in trapezoidal rule is, 0.04 and the integral value is 0.847. That is exactly what we have got
earlier okay. In Simpson’s rule h is (b- a) / 2. That is what h is going to be okay. And I_simp is
going to be h / 3 * f1. So f (a) + 4 * f (a + h) + f (a) + 2 h or we can say f1 + 4 * f2 + f3. So let us
just do that f1 + 4 * f2 + f3 right okay.

What is f1? f1 is nothing but function f calculated at a. What is this guy f calculated at (a + h)
and this guy is f calculated at (a + 2) * h okay. What is the name of the functions that we created,
the name of the function that we created is myFunInt. So let me just replace this with myFunInt
again. F, I will replace with myFunInt and this f also I will replace with myFunInt okay.

That will give me I_simp and err_simp is nothing but abs (trueVal- I_simp). Display results and
we should be able to get the results more easily. So the errors using the trapezoidal rule is 0.04
but the error using Simpson’s 1/3rd rule is about 4.5 *10 ^- 4. So as we can see the Simpson’s
1/3rd rule is significantly more accurate than the trapezoid rule okay. (Video Ends: 13:03).

So this is what we have seen the lte in Simpson’s 1/3rd rule is, h ^ 5 whereas, local truncation
error in trapezoid rule is h ^ 3 okay. And therefore in this particular example it turns out that
Simpson’s 1/3rd rule is gives better results than the trapezoid rule okay. So next what I am going
to do is, and I am going to take this trapezoidal rule.

And I am going to calculate the value of trapezoid rule, the integral using the trapezoid rule for
different values of h. So we will start with f (a) okay and f (a + h). We will calculate this for
different values of h. That means f (b) will be calculated for different values of h.

(Video Starts: 13:49) Again we are restricting ourselves to single application. So let us go to this
numIntegral, drop down, save and click on save as numIntegral2. I will call this okay. We do not

141
want Simpson’s 1/3rd rule, a is going to be this and h is going to be 1 and b is going to be (a + h)
okay. And this is going to be our trueVal.

We do not need this. Now what I am going to show is, let us see we have this particular code
snippet of code over here. How we can make this code bit more streamlined. You have this f (a)
over here okay. We can just delete this f (a) and replace it with what existed over here. So just
highlight it and drag and drop okay. Same thing over here for f (b) also.

Delete f (b) highlight this part drag and drop over here. Delete these 2 guys okay. So let us save
this, let us clear all, clc and let us run this for h = 1 okay. The error err_trap that we get is 4* 10
^- 2. I will uncomment this part okay. Now let us these h by one order of magnitude and take
over h to be 0.1 okay. Our trapezoidal rule is h ^ 3 accurate.

What that means is, decreasing h by 10. We should decrease the error by 1000. So the error
should be decreased by a factor of 10 ^ - 3. So let us run and see what the error we get. Now, by
running this you can see that the error has dropped from 4 e -22, 7 e -5. So this is an almost 3
orders of magnitude. This is actually a 3 orders of magnitude droplet us drop this further 0. 01
and let us run and see what we get.

And we can run this. We further see a 3 order of magnitude decrease in the error. Let us drop,
let’s drop this even further and see what we get. Save and we will see 3 more orders of
magnitude decrease the error. The error went from 8. 2e -8 to 8 .3e -11. So as we decrease the
step size by a factor of 10, the error improves by a factor of 1000. The error goes down by a
factor of 1000.And that is the cause trapezoidal rule is x ^ 3 accurate. (Video Ends: 16:51).

So, this particular slide itself summarizes what we have covered in the lecture today. What we
have covered is trapezoidal rule and Simpson’s 1/3rd rule. What we have seen is trapezoidal rule
is x ^ 3 accurate. What do you may want to test as practice? Problem is verified that Simpson’s
1/3rd rule is h ^ 5 accurate.

142
I am not going to do that in the video lecturers. Because it is simply follows the same procedure
that we have done with the trapezoidal okay. And with that I come to the end of this lecture. This
brings us to the end of lecture 3.4 and i will see you in the next lecture. Thanks, and bye.

143
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No.#02
Lecture No. #3.5
Numerical Integration – Multistep Trapezoid/ Simpson’s Rule

Hello and welcome to MATLAB programming for numerical computations. We are in module 3.
In this module we are covering numerical differentiation and numerical integration. In the second
part of this module we have been covering numerical integration. In the previous lecture 3.4 we
saw application of trapezoidal rule and Simpson’s 1/3 rule. In today’s lecture we are going to
cover multiple applications of trapezoidal rule to find area under a curve.
(Refer Slide Time: 00.44)

If you recall from the previous lecture, integration is nothing but finding area under the curve
f(x) between points a and b. We applied trapezoidal rule in order to calculate this area. And we
found that the errors can be quite high in using a single application of trapezoidal rule. Instead
what we can do is, we can divide this entire region into multiple intervals.

And then calculate area using trapezoidal rule for each of those intervals. Then we sum up all
those areas in order to get net area using trapezoidal rule. How do we do that is as follows. For

144
the first interval that will be that lies between a and (a+ h) the area under this region using the
trapezoidal rule is given by h / 2 * f (a) + f (a+ h).

The second interval was from (a+ h) to (a+ 2h). For this interval our area using the trapezoidal
rule is going to be h / 2 * f. f at (a+ h) + f at (a+ 2h). We keep doing that until the last interval.
Once we get all these areas, our integral i is nothing but summation of all these individual areas
or all these individual integrals that we have obtained using the trapezoidal rule.

The step size h is nothing but (b- a) / n. When we have n intervals we are going to have (n+1)
points in the x axis. How that is, if the entire zone was just 1 single interval that means if we
were doing a single application of trapezoidal rule, we will have 2 points a and b. If we are using
2 applications of the trapezoidal rule, we will have a, the midpoint and b that will result in 2
intervals, a to the midpoint and midpoint to b.

So 2 intervals will have 3 points on the x axis, 3 intervals will have 4 points on the x axis, 4
intervals will have 5 points and so on. And n intervals will have (n+ 1) points on the x axis. What
we will do now is, go on to MATLAB and solve this using multiple applications of the
trapezoidal rule. This is one of the 2 methods that I will show you in today’s lecture for using
multiple applications of the trapezoidal rule.
(Refer Slide Time: 03.27)

145
Let us consider the example that we had solved in the previous lecture. That was to find the
integral of 2- (x+ ln x) okay. So let us go onto MATLAB and do this problem. (Video Starts:
03:39) What we will do is, we will open the file that we had use in the previous lecture. That file
we called as numIntegral. This was to calculate the integral of 2- (x+ ln x) using a single
application.

What we will do is, we will change the 2 multiple applications of trapezoidal rule. I will save this
as a different file. Let us call this as say multiStepIntegral okay. So the problem setup a =1, b =2.
You want to integrate this function from 1 to 2. trueVal is this and let us say the number of sets n
that we want to take initially. Let us say it is 2, right. That is what we said.

We will do okay. So trapezoidal rule multiple applications and let us delete the section about
Simpson’s 1/3 rule and displaying the results. And this is what we are left with okay. So n is 2 for
n = 2 or h was (b-a) / n. So what h is (b-a) / n and we had used myFunInt to calculate all the
functions.

What we will instead do is, we will again keep using myFunInt but we want to do this for all the
data points that means for all the x values x1, x2, x3 up to x n+ 1. So let us define this xVecor.
xVec = a. To a in steps of h 2 b. So this we will result in (n+ 1) dimensional xVector okay. What
we want to do is, calculate the functions f at x1, f at x2 and so on.

We will do this using our myFunInt. Let us open myFunInt. If we give our xVec as our x as the
vector, what we see is that this will result in f evaluated at all these points. This is because of this
function is already vectorized (2- x) is going to give us a vector which is of (n+ 1) size + log of x
log of x is also going to give us a vector of (n+ 1) size for (n+ 1) size vector was added to (n+ 1)
size vector will give us again (n+ 1) size vector.

So we do not need to make any changes myFunInt okay. So we can just say fVec, the vector of
function value is nothing but myFunInt (xVec). And that should be sufficient and let me delete
this one. So what let us do now is, just check and verify that this is indeed works. So let us run

146
this okay. And let us also myFunInt at 1, myFunInt calculated at 2, and myFunInt calculated at
1.5. So these are the3 points when we have 2 intervals. Let us check what fVec looks like.

Let us type fVec, the first guy is 1 which is here and the last guy is 0.6931 which is over here and
the middle guy is 0.9055. So this is indeed what we expect. So let us clear all clc. Now what we
have fVec is f1, f2, f3 and so on up to f (n+ 1). What we want to do now is, to calculate i1 i2 and
so on up to i (n+ 1).

The way we are going to do this is, we will do this in a for loop. for i = 1 to n. So these are the n
intervals okay. Iinterval = zeroes because there are n intervals zeroes of size of n, 1 okay. The
integral for the first interval. So Iinterval for the Iinterval. This is nothing but (f1+ f2) * h/ 2, for
the second interval is (f2+ f3) * h /2, third interval (f3+ f4) * h / 2.

So on ith interval if it is going to be (fi+ fi+ 1) / h / 2. So that is what we will put over here is, h/
2 sorry, multiplied by h/2, h/2 * (fVec i+ fVec i+ 1). End and I_trap, I_trap using the first method
is going to be just the sum of all these values .So I will just say sum Iinterval err1= abs (trueVal-
I_trap 1).

For h=num2str h. error= err1 sorry, num2str err1 okay. So that is what we have. Let us save this
and that is run. Hopefully it will run without giving us any errors okay. Let us go to MATLAB
command prompt and see what the results are. So the integral value is 0.8760 and for step size of
0.5, the error is 10 ^ - 2 okay.

Let us now change from n = 2 to n = 20. So by doing this the step size is going to decrease by 1
order of magnitude. So let us run and see what happens to the error. So when we will run this by
decreasing the step size by 1 order of magnitude, the error has decreased by 2 orders of
magnitude. The error has gone from 10 ^ - 2 to the 10 ^ - 4.

Let us decrease the step size by 1 more order of magnitude. Let us save this and run this. What
we will see is that the error will decrease further 2 orders of magnitude. So when we divided the
step size by 10, the error decreases by factor of 100. When we divided the step size further by a

147
factor of 10, the error further decreased by a factor of 100. If you recall, what this means, this
means nothing but the method is h to the power 2 accurate.

It is the second order accurate method from the point of few of global truncation error. (Video
Ends: 11:57) What I will do now is try to solve this problem using a second way and which is
going to be an easier way from writing a MATLAB program. What we are going to do is, we will
just add up i1, i2, i3 and so on up to i n. So you will have h/ 2 multiplied by f (a) + 2* f (a+ h) so
on and so forth.
(Refer Slide Time: 12:15)

So finally the result that we will get is, if we were to write f(a) as f1, this guy is f2 ,this guy is f3
up to f(n+ 1), the result that we are going to get is, i is h / 2 * f1+ 2 times summation of all the
middle guys + 1 times f (n+ 1). It is very easy to get this formula. You can very quickly try it out
and get this formula. So what will go, now go and do now in a MATLAB is.

(Video Starts: 12:50) To solve this using this direct formula from multiple intervals using the
direct formula okay. Using the direct formula I_trap2 is nothing but h/2 multiplied by f1 which is
nothing but fVec1, fVec1 + 2 * summation of all the middle guys+ 2* summation fVec2 to fVec
from 2 to n okay, + fVec n + 1 okay. That is going to be our I_trap2 using the direct method.

148
So now what this guy does, this guy will extract the subpart of the vector from the second to nth
value. And we just want to sum it up as we have written over here. And multiplied that with 2
and add f1 and fn+ 1. And that will give us the desired result will be multiple it with h /2. So
display I_trap1. I will also display I_trap2. err2 = abs (trueVal- I_trap2).

Yeah for h = num2str err2. Let us save this and let us run it for again for n = 200 okay. So as you
can see from both these methods, the result is the same as we can see displayed over here and the
errors that we can see between the trueVal and the numerical values are also the same okay.
(Video Ends: 15:32) So what we have done really is to calculate a function, sorry, calculate a
integral using multiple applications of the trapezoidal rule.

The first 1 was by using by calculating the integral for each of those intervals and summing that
up, the second and the more efficient way and the method that I will recommend finally is to
calculate the overall result. Pre calculate the overall result and then code that in MATLAB as
shown over here okay. So that is what we have primarily covered in today’s lecture and we have
used that for the example of f(x) = 2- (x+ lnx).
(Refer Slide Time: 16:21)

What we also did was, observe that for the trapezoidal rule. The global truncation error is h to the
power 2. So it is the second order of the accurate method. When we use multiple implementation
of the trapezoidal rule in contrast local truncation error was, h to the power 3 for trapezoidal rule.

149
(Refer Slide Time: 16:44)

What you can do as the practice problem is, you can solve the same example by writing a code
for multiple applications of the Simpson’s 1/3 rule. And you can then go ahead and verify the
global truncation error and see for yourself that reducing the step size by 1 order of magnitude
indeed reduces the errors by 3 orders sorry, by 4 orders of magnitude in the Simpson’s 1 third
okay.

So with that I come to the end of this lecture. In the next lecture we are going to cover
integration using 2 MATLAB functions trapez and quard. So we will cover them in the next
lecture. Thank you and see you in the next lecture goodbye.

150
Matlab programming for numerical computation
Dr.Niket Kaisare
Department of Chemical Engineering
Indian institute of technology, Madras

Module No. #03


Lecture No. #3.6
MATLAB Functions and Application

Hello and welcome to MATLAB programming for numerical computations. We are in


module 3. In the second part of this module we have been covering numerical integration.
This is the last lecture in this module. In this lecture, we will consider MATLAB
functions that can be used for numerical integration. Specifically, we will take 2 functions
trapz and quad.

We will also look at solving an application problem using numerical integration. In the
previous lecture, we have considered trapezoidal rule. We have considered both the single
application of trapezoidal rule as well as multiple applications of the trapezoidal rule. We
wrote our own code in order to compute the integral using trapezoidal.
(Refer Slide Time: 01:05)

Today we are going to use a MATLAB function called trapz. In order to compute that
integral using the trapezoidal rule okay. The usage of this MATLAB function is, integral i

151
is trapz x, fval where, x is the independent variable and f is f calculated at both various
values of x.

x in this case is going to be an n+1 dimensional vector and fval also is an n+1
dimensional vector such that the function values fval are specified are the values that are
specified to the corresponding x values. What that means is the first guy in fval is f(x1),
second guy is f calculated at x2, the third guy is f calculated as x3 so on and so forth.

So let us look at the example that we took in the previous lecture and solve this using the
MATLAB function trapz. (Video Starts: 01:58) So this was the script that we wrote in the
previous lecture. What we did was we specify the problem statement over here. Then we
used multiple applications of the trapezoidal rule and did that in 2 ways.

The first one was to calculate trapezoidal rule by summing from n intervals, the second
way was we drive a direct formula for using the trapezoidal rule and use this as shown
over here. The third way is to use the trapz function and I will display. I will show that
today using MATLAB and trapz function okay.

Let us do help trapz okay. And if you look at the help for trapz, your integral is trapz of y.
If you have your x as unit spacing, that means if x is 1, 2, 3, 4 and so on up to n+1. Then
we can use z = trapz y. However, we know what our values of x are. And they are not
given by unit spacing.

Therefore, we need to use the command z = trapz x, y where, x are the independent
variables and y are the corresponding values of the function. (Video Ends: 03:31) So this
is exactly the usage that I have shown over here. (Video Starts; 03:36) So what we can do
is, we can just write that I_trap3= trapz xVec, fVec using trapz function disp I_trap3, err3
it will also display error 3.

And let us calculate err3. err3 is abs (trueVal- I_trap3). Let us save this and let us run this
function. Let us run this script rather okay. We have run this and we can see using the

152
trapz function. We get the same integral value as we had gotten before and the error that
we get that is trueVal- that itrapzval is also the same as before.

So as you can see it is very easy to implement or apply this trapz function okay. Let us,
let us look at writing the same multistep integral from scratch, I will say edit
multiStepIntegral2 okay. Redo the previous problem. I am doing it again just for sake of
clarity, a was 1, b was 2, n was I believe 200.

Yeah n was 200 and our function was given by this okay. Our h was (b-a) divided by n,
our xVec was a in steps of h to b, our fVec was myFunInt of xVec okay. And i was equal
to trapz of xVec, fVec. We just save this and run this and we will see the results.

So i was 0.8863, abs (trueVal- i) you will see the error is the same okay. So what I have
done is, I have shown you how quick it is to use trapz function in order to calculate the
integral using the trapezoidal rule okay. Let us save this and let us close this function
okay. (Video Ends: 07:04).

Now we have finished using the trapz function that was the first Matlab function that I
wanted to cover in this lecture.
(Refer Slide Time: 07:11)

153
The second function that I want to cover is what is known as quad. In quad in the above
in the method quad what we actually do is, we pass on our function file itself to the
function to the MATLAB command quad. How we do that is, what is known as the
anonymous functions okay. At x means x is the variable and myFun is the file that
calculates the function f(x) okay. So the function myFun returns f(x) for any given value
of x.

This x is specified in the brackets inside this at sign and the myfunction is the name of the
file that we are going to use in order to pass on that function okay. This x is the values at
which we are going to calculate our integrals okay. Now using the function this is going
to be the common syntax of using any function that we are going to do you pass on as an
argument to a MATLAB command.

(Video Starts: 08: 25) Okay let us go to MATLAB and look at this now. I will do help
function handle okay. Function handle is obtained by giving at function name or at
arglist. That means list of arguments followed by that function name. What that means is
this is, let us say we want to pass on a function.

What function we wanted to pass over here. We wanted pass on the function myFunInt
which is a function of x the way we pass this on is at x myFunInt x okay. So this specifies
the, that x is the variable that is required in the function, this is the name of the file
containing the function and this is exactly the same as this particular guy.

If we have a function of 2 variables, say x and y the way we will write this is, myFunInt
x, y okay. That is how we are going to pass on the functions to MATLAB commands.
(Video Ends: 09:48).
(Refer Slide Time: 09:49)

154
That are you going to use these functions okay. The command that we are going to use in
order to calculate the integral is known quad. (Video Starts: 09:58) So let us check help
quad okay. quad will you give the integral of a function specified in a file fun from a to b.
We have our function f(x) that is specified in myFunInt.

So at x myFunInt x, that is how we are going to pass on the function handle to the
command okay. So our i is going to be quad function name, initial, the lower bound, the
upper bound. This is how we are going to run our quad command. Our lower bound was
1 and upper bound was 2 and we run this I, and we will get our integral.

If we recall from what we did just a couple of minutes back, the value of integral the true
value of integral was indeed 0.8863. This is the way to use quad. (Video Ends: 11:15)
What I am going to do next is, we will take up one more example and that is going to be a
physical example that we take.
(Refer Slide Time: 11:22)]

155
So what we are going to do is, find this integral from 0 to 0.5 of this particular function.
Where, the function is f/ k * 1, divided by (1- x) ^ 1. 25. Where f is 10, k is 5 and we
want to integrate from 0 to 0.5 okay.

(Vide Starts: 11:46) So let us go to MATLAB and see how we want to do that. We will
create a function edit myReactorFunction okay, function fval = myReactorFun (x) okay.
Capital f and small k are the values that we need to give F 10, k is 5 okay. And the
function is f/k fval= f/ k, the whole thing divided by (1- x) ^ 1.25. That is going to be our
function. Now how do we vectorizer this function?

So let us say we were expecting our x not to just the scalar x but to be vector x. So we
want to make this into more vect allow x to be vector as well. How we are going to make
this into a vectorizer function. We just make a dot slash over here because this guy is a
vector and I will also got dot carried over here okay. And I saved this as myReactorFun.

How are you going to call this particular function or pass on this function to MATLAB
commands using at x myReactorFun(x). This is going to be this entire, thing is going to
be the function handle. That we are going to use. So what we do is v=quad. This
particular guy which is basically at in bracket.

156
The variable x and followed by space and the file name with that same variable inside the
bracket. So that is what is going to be and the limits. If integral from 0 to 0.5, so you go
over here, so limits of integral are 0 to 0.5 okay. And if we press enter and this is going to
be volume of the reactor that is required to obtain conversion of 0.5. (Video Ends: 14:46)

So with that I come to the end of this lecture. What we did in this lecture basically
covered 2 different MATLAB commands in order to finding integral. The first 1 used the
values of function at corresponding values of x in order to give the integral using
trapezoidal rule that function was known as trapz. The second function that we used was
quad, we use the function quad in order to find the integral of any function.

In using the function quad, we passed on the function handle using this particular method
of passing the handle okay. So with that I come to the end of this lecture and indeed to the
end of this module. In module 3, we covered numerical differentiation. In first 3 lectures,
in that we covered first derivatives and higher derivatives f d, df / dx, d 2f / dx2 using
forward, central and backward difference formula.

We also saw that there is an optimal step size for which the error is minimum. We also
looked at partial differentiation how to do. In the second part of this module. We covered
numerical integration in lecture 3.4 be covered trapezoidal and Simpson’s rule. Single
application, we extended it to multiple application of trapezoidal rule.

In case we divide the overall rule domain into multiple intervals and applied trapezoidal
rule. And finally, in today’s lecture we covered 2 MATLAB functions called trapz and
quad in order to compute the integral. With that I come to the end of this lecture and the
end of this module. See you next week, thanks and bye.

157
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #04


Lecture No. #4.1
Linear Equations – Linear Algebra in MATLAB

Hello and welcome to MATLAB programming for numerical computations. We are in now in
week 4 of this course. In this week we are going to cover linear equation. So basically we are
going to cover linear algebra in the first lecture 4.1 followed by several methods to use linear
algebra to solve the problems of the nature ax equal to b to find the values of the vector x.
Before we do that let us recap some of the results from linear algebra
(Refer Slide Time: 00:41)

For this the related videos are in the computational techniques course module 3 lecture 1.The
link for which is given in front of you. So Let us review some of the results in linear algebra. So
what is a vector is nothing but an ordered set of scalars an example is shown over here. So this
vector has a dimension in this case the dimension of this vector is 2 as convenience what we are
going to do in this entire lecture series.

We are going to assume all the vectors to be column vectors which means that the vectors will
have n rows but a single column. As you can see over here we have 2 rows but a single column

158
now this column vector has a dimension which is nothing but the number of elements. If we want
to look at the geometric interpretation.

This could be nothing but a point in this space in a 2 dimensional space which is the distance 2
on the x axis and the distance 3 on the y axis or more appropriately distance 2 on axis on the first
axis and distance 3 on the second axis. Now this particular vector has a direction which is
basically given by the value 2, 3 as well as it has a norm.

Now the most common norm that we are used to so far is what is known as the 2 norm of this
vector which is nothing but the square of the distance of that particular point from origin. So
square root of 2 square + 3 squared is nothing but the 2 norm of this particular vector. Likewise
we have the 1 norm of vector and n norm of the vector or the infinitive norm of the vector
something we are not going to go into.

But essentially just the way we are associate a distance with a physical point in space a vector
can be associated with a norm which is nothing but an indication of the size or the distance of
that particular vector.
(Refer Slide Time: 02:52)

Okay, now in next case we have seen is what does it mean when we want to solve 2 equations
into unknown. So here I am showing 3 different cases of 2 equations and 2 unknowns. The

159
unknowns are x1 and x2 and the 2 equations can actually have a single solution can have 0
solution and can have infinite number of solution. So when do we get a single solution, 0
solutions and infinite number of solutions we are showing graphically on this slide okay.

So if you look at the first example, we have the equation x1+ 2 x2 = 1, x1- x2 = 4 these
represents the blue and the red line over here and these 2 lines intersect at a point (3-1). So (3-1)
is a unique solution okay. Next is if these 2 equations instead of having x- x1- x 2 as the second
equation, if we were to have 2x1+ 4x2 which is nothing but the double of the left hand side, as
the left hand side of the next equation which is 2x1+ 4x2 that is the left hand side of the next
equation.

And the right hand side if we take that value as 4, we can see that these now represents 2 parallel
lines. These 2 parallel lines are not going to intersect and we are going to have 0 solutions in this
case. And now in this case if the second equation was nothing but double of the first equation so
2x1+ 4 x2=2, which is nothing but double of this first equation.

We are going to have these 2 lines lying on top of each other and we are going to have infinite
number of solutions. So the question is when do we get single solution? When we do get infinite
number of solutions? And when we do get 0 solutions? Okay and answer to that is from finding
out the rank of the matrix.

So if we look at the rank of matrix a and if the rank of matrix in this particular case was equal to
2, we are going to have a unique solution if rank of a square matrix of an n by n dimensional
square matrix is equal to n, then we will have a unique solution for the problem ax=b okay.

But what if the rank is not equal to 2? That is the case that we see in the middle column as well
to the right column. In that case we need to look at the rank of the matrix a, b if rank of matrix a,
b is equal to the rank of a, then we are going to have infinite number of solutions but if rank of a,
b is not going to be equal to rank of a, then we are going to have 0 solutions okay.

160
(Video Starts: 05:45) Let us go on over to MATLAB and look at these 3 examples that we have
seen so let us look at the matrix a, matrix a is 1 2; 1 -1. b is the right hand side that is 1, 4 okay.
So these are the 2 values that we have, when we want to solve ax = b. We know that the solution
x is going to be inv, inverse of a*b okay. And x as you can see over here is 3, -1 something that
we had seen in our power point slide a minute earlier okay.

Now MATLAB has a more efficient way of solving these equations and that is using slash in this
case backslash. Because we are pre multiplying inverse of a with b, so we write this as a
backslash b and when we press enter we are going to get the solution 3, -1. A backslash b for
and a which has a rank = n is exactly same as this particular command okay.

Let us do help slash c and see what we get okay. If you recall, what we did in the first module we
had looked at element by element division and element by element operations and this is what
the later part of this help slash showed us. But what we are more interested in is to look at what
the left division and right division actually mean.

So a backslash b is left division which is same as inv a*b, if a is an n by n matrix with rank = m
okay. It is something different if the mat rank of a is not equal to n we will not go into that right
now. And then we have an equivalent called a right division right division is b multiplied by
inverse of a okay.

For the problem ax equal to b, we needed to have a left inverse. So x was inverse of a multiplied
by b not b multiplied by inverse of a, and therefore we had left inverse which is indicated by
backslash, a backslash small b over here not capital b and that is what the solution that we got
okay. So that was the first thing that we saw how to solve these equations.

Let us look at how to find out using MATLAB whether or not your equations are going to have a
unique solutions or infinite number of solutions or 0 solutions. And we can do that by using rank
a and rank of matrix a is equal to 2, so a we say is a full rank matrix. That means a, the rank of a
is equal to the number of rows or the number of columns of matrix a, okay.

161
We can also find the size of matrix a by saying m, n = size a. m is going to be number of rows, n
is going to be number of columns we have already seen that in module 1. (Video Ends: 09:25)
Let us now look at the second example and the second example, our b remain the same we
changed our a to 1 2 2 4. (Video Starts: 09:33) Let us make that change a equal to 1, 2; 2, 4. Let
me echo this by skipping the semicolon okay, so our a is 1 2 2 4. Let us get rank of a and rank of
a = 1 okay.

And therefore a will not have a unique solution sorry, ax= b we will not have a unique solution.
It can either have infinite number of solution or 0 solutions. And to do that we need to find the
rank of matrix a, b. And the rank of matrix a, b = 2 which means that the rank of a, b is not the
same as the rank of a. (Video Ends: 10:19)

So we are going to have 0 solutions that is this particular case. Now Let us change b to 1 2,
(Video Starts: 10:26) b = 1; 2 note that we have used that semicolon because we want the column
vector. So that our b. Rank of a is not going to be change because we have not changed rank
sorry we have not change the matrix a. Let us now look at rank of a. And rank of a, b in this case
equals rank of a in which case (Video Ends: 10:52)
(Refer Slide Time: 10:53)

162
We are going to have infinite number of solutions okay. Let us now look at the next concept in
linear algebra and that concept is what is known as condition number. In these 2 examples we
had equation x1+ 2x2 = 1 and x1+ 4x2 = 2. Instead of the second equation being in x1+ 4x2, if
we have to change this to 3.999 x2 okay. We are going to this particular equation okay and our
matrix a is going to be 1 2; 2 3.999

If you solve this equation we are going to get the solution as 3, -1. Now if we make a very small
change in our b that means instead of 1 and 2.001 if we change it to 1 and 2.0, we are going to
have a very large change in the solution. And the reason for this is what is known as condition
number or what is known as a poorly condition matrix. Condition number is nothing but the ratio
of Eigen values of the matrix.

So in this particular case a being n / n matrix has 2 Eigen values and the ratio of 2 Eigen values
in this case is approximately 25000 which is a fairly large number and therefore we see that the
results change quite significantly for a reasonably small change in b matrix okay. (Video Starts:
12:28) So Let us look at the condition number of a when a was defined as 1 2; 2 3.999 that was
our a matrix.

We will use the command cond in order to find the condition number of a and cond a is 2.5 was
approximately 2.5*10 ^ 4 this is approximately 25000. Now we can find Eigen values of a using
the command eig and eig will give you 2 Eigen values and we can also verify that the condition
number is the ratio of the 2 Eigen values okay. And the absolute value of that. (Video Ends:
13:30).

Yeah and this is exactly what our condition number is that we obtained using the command cond.
So the MATLAB command cond is finding condition number of any matrix. The MATLAB
command rank is to find out the rank of that particular matrix and MATLAB command eig is the
command for finding Eigen values of that matrix.

(Video Starts: 13:45) Now Let us go back and look at what else our Eigen values command can
do. So I will type help eig okay. And so we can see that Eigen values. So if we give a command

163
eig with just 1 output argument, we are going to get a column vector containing Eigen values.
However if we give 2 arguments v and d, v is going to be of a matrix containing the Eigen
vectors and d is going to be matrix whose diagonal elements are the Eigen values.

So Let us look at that in MATLAB v, d equal to eig okay. So as remember just a few minutes
back we had said that will define vectors as column vectors. So this is the Eigen vector 1 which
is -.89, 0.45 that is the first Eigen vector corresponding to this Eigen value. The second
eigenvector is this guy which corresponds to this Eigen value.

Okay Let us look at whether this satisfies the equation rather a multiplied by v = lambda
multiplied by v whether this is satisfied or not okay. Let us try to look at that okay. So Let us say
l was nothing but d1 okay. And our v was nothing but the first column. The first column basically
means all the rows and column number 1. This l is the first Eigen value, this v is the first Eigen
vector. Let us say a*v- lambda * v what we get is, we get the value as 10 to the power- 15.

Look, Let us look at the value of the a * v and a * v is of the order of 10 ^ - 3. So for all practical
purposes this particular guy a * lambda is up almost equal to lambda * v okay. Again keep in
mind what he had said earlier in the previous 2 modules is, computer is a finite precision
machine. (Video Ends: 16:17)

What that means is that computer has a least count. So when we are going to do problems using
floating point numbers using real numbers, we are not always going to get when we do (a-b). We
are not always going to get the value equal to 0 if a and b are equal. We are going to get some
times value of (a-b) as a very small number compared to value of both a and b.

(Video Starts: 16:44) And that is what essentially we are looking for is whether or not a multiple
by v is close enough to a lambda * v. So a multiple by v is this value and lambda * v is also the
same value. Let us look at the other example where, we take the lambda as this second Eigen
value.

164
So l is the d2, 2 and v is nothing but v all the rows and second columns okay. Let us look at a
multiplied by v, this is the value of a*v. And let us look at lambda multiplied by v and that is the
same value that we get as lambda * v okay. To recap what we did. We did use the command eig
to find Eigen values and Eigen vectors, when you run the command with 2 arguments. (Video
Ends: 17:37)

The first argument will give you a matrix containing the Eigen vectors. The first column of that
matrix is the first Eigen vector, the second column of that matrix a second Eigen vector and so
on. The second argument from eig is a matrix whose diagonal elements are the various Eigen
values.

The first a11 is the first Eigen value, a22 is the second Eigen value and so on to ann is the nth
Eigen value.
(Refer Slide Time: 18:18)

So that is the review of the linear algebra that is going to be useful for rest of this course and let
us now recap all the useful MATLAB functions that we have done so far. In the previous lectures
in module 1 primarily. We have looked at matrix exponent, matrix logarithm, matrix square root,
matrix power and today we also looked at matrix division, left division as well as right division.
So these were the commands that we have covered before in like in modules before.

165
Today we covered the command inv to find the inverse of a matrix, rank to find rank of matrix,
cond to find condition number of matrix. We will also in a minute use the command norm to find
norm of a vector or a matrix. We will just take ourselves to vectors for now but the norm exists
for a matrix as well okay.

We also used eig to find Eigen values and Eigen vectors, lu cholesky qr factorization, schur
decomposition these are something that a lot of engineers use quite a bit and I have put them in
this particular table just for the sake of completeness. Lud composition is something that we are
going to cover in the fourth lecture of this module okay.

However you do not have to worry about the remaining thing singular value decomposition or
schur decomposition or cholesky factorization and so on for the purpose of this course okay.
Okay so Let us finally, now let us go and find out the norm. (Video Starts: 19:49) So Let us say,
Let us look at our vector x. Now norm of this vector is nothing but 3 2 + 12 square root of that,
which square of n.

So norm of x is going to be square root of 10 which is nothing but 3.1623 okay. So norm is an
indicator of the size of any vector. (Video Ends: 20:16) So with that we come to end of lecture 1
of module 4. So what we have done in lecture 1 of this particular module is, to cover the basics
of linear algebra.

So primarily we have looked at trying to solve equations of the form ax = b, look at Eigen values
and Eigen vectors of a what does the condition number mean, what does rank means and how we
can use rank in order to find out whether we will get unique solutions, infinite number solutions
or 0 solutions to an equation of the form ax =b so that ends this lecture for recapping linear
algebra. And see you in the next lecture thank you.

166
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #04


Lecture No. #4.2
Linear Equations – Gauss Elimination
Hello and welcome to MATLAB programming for numerical computations. We are in week
number 4. In this week we are covering linear equations. In lecture 4.2, we are going to cover
one of the most popular method of solving linear equations which is known as Gauss
Elimination. In today’s lecture we are going to use what is known as the naïve Gauss Elimination
technique.
(Refer Slide Time 00:33)

Okay let us take an example. In order to show to how we are going to solve this problem this
example is from the computational techniques have video course module 3 part 2 the link for
which is given over here okay. So these are 3 equations and 3 unknowns. The 3 unknowns are
x1, x2 and x3 together they form a vector which is column vector containing 3 rows okay.

We will write this equation as Ax = b where, a matrix is going to be nothing but 1, 1, 1; 2, 1, 3; 3,


4, -2 and our b vector is going to be 4, 7, 9, in the previous lecture we have seen that we can use
MATLAB’s powerful linear algebra sweet in order to find the solution x. Either using inv(a)*b or

167
the backslash command, what we are going to do today, is to use the Gauss Elimination
technique in order to solve this linear equation okay.

(Video Starts: 01:36) And Let us say edit gaussElim and this is the one that we are going to
create in order to solve the problem using Gauss elimination. Let us go on and look at the
problem, our a is 1 1 1; 213. Let us write it down a as 1 1 1; 2 1 3 and 3 4 -2. Our b matrix is
nothing but Let us go back and see 4, 7, 9 b is 4; 7; 9 okay. Let us save this and run this we can
get a and b.

And our x is going to be nothing but a\b and that is our x which is 1 2 1. So that is the solution to
our problem using MATLAB’s backslash command. (Video Ends: 02:40). Okay what we want to
do is, to solve this problem using Gauss elimination. So the first step in Gauss Elimination is to
create augmented matrix.

(Video Starts: 02:48) And Let us go ahead and do that. Let us make comments first. Solve Ax =
b, naïve Gauss Elimination okay. And augmented matrix Ab = A, b and we will end this with a
semicolon. (Video Ends: 03:19) Okay Let us go back okay. Now at each step A( i,i) is the pivot
element. So first we will have A(1,1) as the pivot element then will have A(2,2) as the pivot
element and finally A(3,3) as the pivot element.

(Video Starts: 03:31) We do not have to do anything with the A(3,3) mainly because A(3,3) row
number 3 is the last row so. Let us actually do this A(1,1) as pivot element. Yes we will have a
set of computations with that. We will have set of computations with A(2,2) as pivot element and
with A(3,3) as pivot element. Let us say Gauss Elimination, notice that when I have used 2
percentage signs, this has formed a section in this particular code okay. And with this enter over
here okay. Now we are ready to do the next steps. (Video Ends: 04:38)

So what is the step with A(i,i) as the pivot element? So Let us say A (1, 1) as the pivot element
we want to create 0’s in the pivot column which means in column number 1. So column 1 over
here is basically 2, 1, 3. So what we are going to do is, we are going to subtract 2 times row 1
from row number 2.

168
(Refer Slide Time: 05:01)

So we need to use row operations. If row 2, the new row 2 is going to be row 2, the old row 2-
alpha times row 1 where, alpha is A(2,1) divided by A(1,1), A(2,1) which is 2 divided A(1,1)
which is 1. So that is going to give alpha as equal to 2 and row 2-2* row 1 is going to be our first
step.

(Video Starts: 05:28) So let us do that in MATLAB saying alpha = A (2,1) that is the second row
first column divided by A (1,1) okay. (Video Ends: 05:44). Let us go back and see that is exactly,
what we had αi,j is going to be equal to A (j,i) divided by A (i,i). So A (2,1) divided by A (1,1) and
A (3,1) divided by A (1,1) okay.

(Video Starts: 05:59) So that is going to be our alpha. Our row 2 = row 2- alpha times row 1
okay. This is not a correct way, so how do we get the row, how are we going to get the row? And
we are going to be get the row of the Ab matrix and the way we are going to do that, let us
actually copy paste this, and yeah so we have this Ab matrix.

So, how are you going to get row number 2? Row number 2 of Ab is nothing but row (2, :).
Entire row number 2 will be extracted by this command. This is what we get. So this is going to
be row 2. What is row 1? Row 1 is Ab (1, :) okay. So, what is alpha * row1? Alpha multiplied by

169
row 1 is going to be nothing but 2 * Ab (1, :) and when we do that that is exactly, what we get so
this is 2 * row 1.

What we want to do is row 2 -2 * row 1 and assigns it to row 2. So Ab (2, :) is going to be equal
to Ab (2, :) which means row 2 = row 2- α * row 1. And row 1 was Ab (1, :) write this is row 1
which is exactly I have written over here. Alpha value is 2, this is exactly what I have written
over here, row2 is Ab (2, :) which is exactly what I have written over here and that expression is
been assigned to Ab (2, : ).

So let me cut this, go back over here and paste this. So this is going to be nothing but row 2-
alpha time row 1 okay. That is cool with us. Next what we want to do is, we want to repeat that
with row 3. As we have seen over here. (Video Ends: 08:11)

We use the pivot element to create 0’s the entire pivot column, entire column 1 we are going to
use row 1 in order to create 0 over here as well as 0 over here okay. So we need now to repeat
that for row number 2. (Video Starts: 08:25) And the way we are going to do that is nothing but
alpha now is going to be a 3, 2 / a, sorry, 3, 1 / a 1, 1.

Note that only the row number is changing. Row number has changed from 2 to 3 and we
change from 2 to 3 over here and - α * row number, what is it row3= row3 - α * row 1. So that is
all the computation required with A (1, 1) as the pivot element and we have done over here.

So that is the first step. Now we need to repeat that for column number 2. (Video Ends: 09:09)
So what now after at the end of this? We are going to get is, we are going to get 1 1 1. This guy is
going to be 0, this guy is going to be modified number and so on and so forth. This is again
going to be 0 and this we are going to have some numbers.

(Video Starts: 09:25) Let us do this. Let us save this, just run it and see what value of Ab we are
getting and if we are getting any errors okay, cool. So we do not have any errors. Let us clear the
screen and type Ab okay. So what we have done at the end of the first step is with the A(1,1) as

170
the pivot element we have obtained 0 in that pivot column in column number 1, in row 2, 3 and
so on. We have obtained 0’s over here okay. So that is the first step.

Now the second step is going to be, to convert this as 0 using A (2, 2) as the pivot element, we
are going to use the row operations with A (2, 2) as the pivot element, And Let us go back to
MATLAB. Let us do that. In this case now alpha is going to be something divided by A (2, 2)
and what is that something going to be.

That is going to be row number 3 right. What we want to do over here is, we want to use row 2
in order to get this guy equal to 0. Alpha is going to be this guy divided by this guy. So this is A
(3, 2) divided by A (2,2). So this is a 3 sorry, 3, 2 okay. Again keeping up with the equations that
we had seen a minute before. That is the value of alpha over here and our value of Ab (3, :) is
going to be nothing but Ab (3, :) - α * Ab.

And this time it is (2, :). Why because our equation with A (2, 2) as the pivot element was r3-
alpha time r2 okay. So r3 is this guy equal to r3 - α * r2 and that is what we get over here. So Let
us save this and run this and see what we get with our matrix Ab okay. Oops! That is something
wrong and Let us go back and check what exactly did we do wrong okay.

Alpha is going to be equal to a, I am sorry, this is a problem I should be using Ab over here and
not a okay. So shift everything I need to change right. And alpha over here also has to be Ab
because we are working with the augmented matrix okay. Because this is in the lower triangular
form, we do not really need to do anything with the pivot element A (3,3).

So oops! Yeah let me move over here and delete this part with pivot element. And this is the end
of our Gauss Elimination. We have done with the Gauss Elimination over here. Why did we
think we just 2 steps because we had 3 /3 matrix okay. Next step is going to be back substitution
okay. (Video Ends: 12:44)

171
(Refer Slide Time: 12:44)

Now yes with respect to back substitution what we do is, we have our last equations as if, (Video
Starts: 12:53) we go back to MATLAB and check this, our last equation is - 4 x 3 = - 4. So x3 is
just going to be this guy divided by this guy okay. So our x3 is going to be equal to 1 okay. Then
we go on to the second last equation, second last equation is - 1x2+ 1 * x3 = -1.

We take this on to the right hand side and divide by A (2,2) and that is going to be our solution
for x2 and then we use x2 and x3 in order to find x1. That is the procedure we are going to use.
So Let us do that. So we are going to do x. So x, initialize our solution okay. So our x3 is going
to be nothing but Ab end, sorry, Ab (3, end) which is the last end basically means the last column
and third row so Ab (3, end) divided by Ab (3, 3).

That is the value of x3 right. Again what we will get with respect x3 is, Ab(3,3) is the
denominator and Ab (3,end) is the numerator Ab(3,end) is the numerator and Ab(3,3) is the
denominator that is our x3. Our x2 is going to be nothing but Ab (2, end) right. Ab row 2 and last
column so Ab (2, end) – Ab (2, 3) * x3 okay.

That is going to be the numerator, so we will put the numerator in the brackets okay, divided by
Ab (2, 2) that is going to be our x2 and our x1 is going to be like just copy this and we will have
to be change from 2 to 1 everywhere and everything else remains the same. So (1, end) - Ab (2,

172
3) * x3 will change to Ab (1, 3) * x3 and we will do this. We are going to add Ab (1, 2) * x2, (1,
2) * x2 okay.

So let us see what we have done. What we have done over here is the, first guy x1 is going to be
Ab (1, end) - Ab (1,3) multiplied by x3 - Ab (1,2) * x2 and that is really what we have done over
here in our MATLAB okay. And that will give us the solution. Once I changed the denominator
also okay. Keep in mind that I have an inner bracket over here and then an outer bracket over
here okay.

And save this. We run this and we check whether the solutions are met or not our value of x is 1,
2, 1 which were the same as a backslash. So this our Gauss Elimination is working to the way we
desired it to do okay. (Video Ends: 16:33)
(Refer Slide Time: 16:34)

Now let us spend some time in order to make this better. What we want to mean by this is that
MATLAB has a very powerful linear algebra set up tools and we do not really need to write this
in the way. We have written it right now. (Video Starts: 16:53) What we need to do is, we need
to put it in a loop okay. First of all let us look at this equation. Let we copy this numerator that
was in the bracket and paste it over here.

173
Let me just put some spaces so that it will become a little bit easy for us to check okay. So we
have Ab (1, end) - bunch of things okay. So Let us keep Ab (1, end) on a scroll. We do not have
to worry about that part. Let us look at the thing that was there in the brackets. That was Ab (1,3)
* x3+ Ab (1,2)*x2. So if i have to write Ab 1, 2 to 3.

What I am to get is, the 2 guys so Ab (1, 2) and Ab (1, 3) okay. If I write x 2 : 3 and I am going
to get now vector which is the column vector and if we multiplied these 2, what I am going to get
is, Ab (1, 2) * x2+ Ab (1, 3) * x3 which is exactly what we had over here okay. So let me just
multiply this guy with this guy okay.

And that is going to be Ab (1, 2) to 3 * x 2 to 3 and we want to check whether the result is same
as this guy that is the whether the result is equal to 3 or not. So let we press enter and we actually
find. Yes that is absolutely what we get. So, let me go over here and I am going to change the
thing in the bracket to what we had written in the command prompt.

The command prompt what we had written is, this copy it and paste it over here okay. And that is
going to be our value of x1 okay. Now if that is value of x1 this is, if you see 1+ 2 to 3 okay. So
we will just write it as sorry 1+1 okay, to n and will go up and we will write n = 3. We will have
3 equations. So, I will be write it over here will actually take this and put it right at the top okay.
So 1+1 to n and 1+ 1 to n okay divided by Ab (1, 1).

Likewise, what we are going to do is Ab (2, 2+ 1) to n * Ab (2+ 1 to n) okay. Why we are writing
in this way because now we have this x going from 3 to 3 and that is good enough for us okay.
Let us put this n loop for i = 2 in step of -1 to 1 end. End write end this is going to be i. I have
replacing 2 with i over here okay.

Everywhere where there is 2, I am replacing it with i okay. Same thing I am going to do over
here but everywhere there is 1, I am going to replace that i okay. Now we compare these 2. What
we will see is, that these 2 expressions are exactly the same that is how we write that I, the
results in end loop okay. As a matter of that if we were to do this for x3.

174
What we are going to get is Ab (3, end) - Ab (3, 3) + 1, 4 to 3 and 4 to 3 is null. So 4 to 3 is
going to be nothing so is going to be Ab-nothing divided by Ab (3, 3). So I will delete this and I
will change this to 3 and gaussElim okay. And we will see our x is exactly the same as we got
over here. (Video Ends: 21:29) With that we come to the end of lecture 4.2 and I will see you in
the next lecture thanks and bye. `

175
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #04


Lecture No. #4.3
Linea Equations - LU decomposition; Partial Pivoting

Hello and welcome to MATLAB programming for numerical computations. We are in module 4,
in this module, we are covering solving linear equations of the type ax = b where, a is an n /n
matrix, x and b are n dimensional column vectors. In the previous lecture, what we had covered
was a popular technique known as Gauss elimination followed by back substitution.

In this lecture, we will take the example, from the previous lecture, and in order to do two
different things, one is to look at new method called LU decomposition and the second one is to
look at partial pivoting in gauss elimination.

(Refer Slide Time: 00:52)

So, let us consider, the example from previous lecture, that was, a was 3 / 3 matrix as we all
know x is a 3-dimensional vector containing x1, x2 and x3 where as b is 4, 7 and 9.

176
We solve this problem using gauss elimination and back substitution, and what we are going to
do is, take the same code, from that we generated using MATLAB in the previous lecture, and
use it to do two things, LU decomposition and partial pivoting. (Video Starts: 01:26) So, let us go
to MATLAB, and edit gauss elm. So, this was the code that we wrote in our previous lecture. So,
we will kind of, just modify the code little bit.

So, what we have done is, with A (1, 1) as the pivot element, we just enumerated for row 2 and
as well as for row 3. So, let us just modify that and put in a, for loops for i=2 to 3, because we
did this for row 2 over here. And we did this for row 3 over here. So, when i=2 okay, everything
that we have 2, we can just replace it with i and see what we get. And let us see that we will
replace Ab (i). Ab (i) and this is Ab1.

So, we do not have to change that, and we will just have to delete this, type end, and that should
be good for us, and just indented as appropriate okay. And for this particular case, again what we
are going to do is, something like this, we will just say i=3 because for i=3, 2, 3 in this particular
case. And alpha is going to be Ab(i,2) just as above divided by Ab( 2,2) and Ab (i, :) = Ab ( i, :) α
* Ab( 2, : ). So, you have done that. And now Let us run this and check if there are any errors.

I will click on run and I will go to the MATLAB command prompt, and type our x and also type
Ab matrix, yes Ab matrix is indeed a lower triangular matrix and x, the solution is indeed what
we expect 1 2 1 okay. So we are happy with this change. (Video Ends: 03:28). Let us go back to
power point okay. So, what we did now is, we took the gauss elimination example from the
previous lecture. And modified it so that we use basically loops in order to do the gauss
elimination computation okay.

(Refer Slide Time: 03:47)

177
Let us go on to the next slide LU decomposition. So, what happens in an LU decomposition is as
follows, interested readers can go on and look at computational techniques course. It is the sister
course of this particular course, you can look at that course, module number 3, lecture 3 and
there I have explained what LU decomposition is.

And basically, the idea behind, LU decomposition is, that any matrix A can be written as a
product of the lower triangular matrix L and an upper triangular matrix U where, the U matrix is
nothing but, the matrix that we obtained after gauss elimination. (Video Starts: 04:29) What that
means is, our U matrix is nothing but, this guy 1, 1, 1; 0, -1, 1; 0, 0, -4. That is our U matrix.
(Video Ends: 04:41) What is our L matrix? Our L matrix is nothing but, 1, 1, 1 as the diagonal
elements and the alpha are those ratios of a A(2,1) / A(1,1) , A(3,1) / A(1,1) , A(3,2) / A(2,2) etc
those ratios which we have already pre computed in gauss elimination form the l matrix okay.

So what do we do? So, what do we need to do is, U matrix is already obtained a gauss
elimination. So, we do not really have to do anything about it. What we need to do is, to define
that L matrix first as an identity matrix and afterwards as we do computations of gauss
elimination, just populate the element alpha (2,1), then populate element alpha (3,1) and alpha
(3,2) and so on and so forth.

As we do the computations, (Video Starts: 05:37) let us go on to MATLAB and what we will do
is gauss elm. We will save as myLUcode okay. I will just change this, LU decomposition using

178
naive gauss elimination. Everything remains the same, what we need is, the L matrix and the U
matrix, we do not need back substitution. So, let us go, and delete our back substitution okay.
Now that, we have deleted our back substitution. Our U matrix is nothing but, Ab from 1 to n, 1
to n. We do not want the b part of it. So, that is the reason, why we are writing U in this
particular manner. Now the question is how to obtain L?

As I had said earlier, what we do with L is, initialize that, as an identity matrix eye n okay.
(Video Ends: 06:41) So, when we initialize, it as an identity matrix, we are going to get once as
the diagonal elements. The sub diagonal elements right now are all 0s and as we do the
computation, we are going to populate that particular matrix as well okay. (Video Starts: 06:58)
So, what we need to do, this alpha is, nothing but the first column row 2 and row 3.

So, L row i, that is row 2 and row 3 and the column number 1 is going to be nothing but the
alpha okay. And we write that, and that should be good enough for us, and the same thing we are
going to write for, our column number 2 as well. So we are going to write this and this is not
going to be column number 1 but this is going to be column number 2. So, row number 3,
column number 2, again is this alpha that was computed over here.

Keep in mind, that I am not writing this L before the alpha. I am writing this L after the alpha and
myLUcode and I will press enter. Hopefully we will not get an error. No, we did not, so we look
at our L matrix and we look at our U matrix. So, this is our L and U matrix. And let us, take the
product L * U and let us see what do we get L and when we say a L * U, we will get exact same
a matrix back. So, that is what LU decomposition is. (Video Ends: 08:11)

(Video Starts: 08:13) And we go back once again to the code, and see really what changes have
we really made. The only changes, that we have made is, this particular command L (i,1) has
been added in this overall computation and we initialized our L as identity matrix and finally we
compute our U in this particular. So, those are the only changes that we have to make you save
this and we quit. (Video Ends: 08:44)

(Refer Slide Time: 08:46)

179
So, the idea of partial pivoting is as follows, so what we are go doing in gauss elimination is, we
use row exchange and row operations. In order to do to get 0s, in the sub diagonal elements, in
the elements below the diagonal okay. In the naive gauss elimination, we just blindly use
whatever so, overall results that we have, we just keep blindly using that the matrix A. We
blindly use that, in case of gauss elimination with partial pivoting.

What we are going to do is, we are going to use row exchange, in order to exchange it, in such a
way. Exchange the rows in such a way, that the pivot element is the largest value, the absolute
value of pivot element is the largest value, in the pivot column. So in the first operation, when
A(1,1) is the pivot element at that point, we look at all the coefficients in the first column okay.
And the largest coefficient is going to be swapped with row 1.

In this case, the large coefficient happens to be 3. So, we will swap these 2 equations, and then
do the first step of gauss elimination okay. (Video Starts: 10:03) So, let us go back to MATLAB,
and open our gauss elimination code okay. And I will save this as gauss elimination with partial
pivoting, using gauss elimination, okay. So what I need to do is now A(1, 1) is the pivot element
change, to ensure A(1, 1) is the largest in column 1 okay.

So, what that means, is that, we want to look at the column 1. So, col 1=Ab, the entire row, all
the rows and in the first column. So, that is the column 1, we want to find which one is the

180
largest, and to do that, we use the command called max. How do we exchange row 1 and 3, and
we remember Ab (3, : ) is nothing but the third row right. So if we were to write Ab (3, : ) and
assign it to Ab (1, : ) okay.

That is going to actually, give us the third row and it will put the third row instead of the first row
okay. There is going to be a problem with this. I will show you, what that problem is by clicking
enter okay. Can you can see what the problem was, compared with our Ab matrix? Can you
actually, see what the problem was? Okay.

The problem is this, by doing this what is happening is, we have now lost our first row okay. We
now no longer know what our Ab (1, :) was. So, we need a way to store that Ab (1, :) and then
use this particular command. So let us just copy this and go onto our editor and do this again.
Before, we do that, we need the max also. And we want to store Ab (1, :) before, we change the
value of (1, : ). So let us just reuse that variable dummy because anyway that variable was a
dummy variable.

So dummy is nothing but Ab (1, : ). It is a temporary place holder for the first row. And Ab (3, : )
is nothing but dummy okay. So, let us save this, and what we can do is, this is okay. And I will
just right click and click on evaluate selection, and see what we get. If we type Ab, we have seen
that row number 3, has now been exchanged with what earlier was row number 1, so row
exchange has happened. So, this is how partial pivoting will work. So, this is partial pivoting.

What we have done over here is, absolutely what partial pivoting is. We did that with the first
row when A (1, 1) was the pivot element okay. Computation in the pivot column okay. And we
repeat this, row exchange to, okay. And again we need to look at only the sub diagonal elements,
so col2 is going to be=Ab ((2 to end), 2) okay.

And as before, just copy this and dummy, idx okay, is this, and dummy=Ab (2, :). Ab (2, :) =Ab
(idx) and Ab (idx) =dummy, and the same thing we can do over here as well. Ab (1, : ) is going
to be dummy, okay. Ab (1, :) is nothing but Ab (idx, :) and Ab (idx, :) is going to be nothing but
dummy okay. So we have this and we continue with our gauss elimination, so okay. So save this
and let us hope there are no errors.

181
We have run this and go back and check. Our Ab, is now it is a upper triangular matrix again
over here okay. But we have done this partial pivoting, which has resulted in exchange of row
number 1 and 2 in the first case okay. And let us see, what we get as the solution x, after back
substitution. And after back substitution, we notice that our x is exactly the same as before 1, 2,
1. So yes, it does work and we want to find out.

Let us say if the idx is over here has changed or not. So I can just type, I think there was a
mistake over here. I think this should be col2. I do not, I am not sure why, this actually run,
probably I did because, let us see how, what Ab is different this time okay. As you can see Ab is
different but x you are going to get as the correct solution 1, 2, 1, and you will see that in this
particular case. In this case of partial pivoting, in this particular case, a row exchange between 2
and 3 had happened in the second, for the second pivot element.

In this case, the particular row exchange had not happened. So, that is the only difference
between these 2 examples okay. And we do get our x solution as 1 2 1 using partial pivoting
okay. So, that pretty much brings me to the end of this particular lecture okay. (Video Ends:
17:41)

What we have covered in this lecture, two concepts, one is that of LU decomposition where the
matrix A can be written as a product of lower and an upper triangular matrix. The upper
triangular matrix was nothing but the solution obtained after the gauss elimination, and the lower
triangular matrix have been the matrix containing the alphas okay.

And the idea behind gauss elimination plus partial pivoting, was to use row exchanges. In order
to ensure that the A (i, i) is the largest element in that particular column, when we are comparing
with sub diagonal elements only okay. So, with that we come to end, of this particular lecture.
Thank you for listening and see you in the next lecture.

182
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #04


Lecture No. #4.4
Linea Equations - Gauss Siedel

Hello and welcome to MATLAB programming for numerical computations. We are in module
number 4 where we have been discussing solving linear equations of the type ax =b. This is
lecture 4.4 and today, we are going to change the gears a little bit, and talk about what are known
as iterative methods. The most popular iterative methods are Gauss Siedel method and the Jacobi
Iterations.

(Refer Slide Time: 00:36)

In Gauss Siedel and Jacobi iterations, what we do is, we write, rewrite each and every equation.
So that the first equation can be used to obtain the solution, to the first variable x, the second
equation is rewritten to obtain solution to second variable x(2), so on and so forth up to x(n).
Which means for example, if we were to take the first equation, we will write A1,1 x(1) = b1 – A1,2
x(2) – A1,3 x(3) and so on up to A1,n x(n) okay, then divide the whole thing by A1,1.

183
What that will end up giving is, the first equation will then we used to find the solution for x1.
Likewise we rewrite, the second equation, to take all the terms from the left-hand side except A 2,2
x(2). All other terms in the second equation, are taken to the right-hand side and we use the
second equation to get x(2), so on and so forth. That is the strategy that is used both in Jacobi
iterations as well as the Gauss Siedel iterations.

The difference between Jacobi iterations and gauss Siedel iterations, is that the, Jacobi iterations
uses the solution vector x that was obtained in the previous iteration. Whereas, Gauss Siedel
method uses, only the latest value of x.

(Refer Slide Time: 01:58)

Gauss Siedel method was discussed in, computational techniques course module 3, lecture 6
okay. And the link for that is given over here. The example that we considered, is x1 + 2 x2 =1,
x1 - x2 =4. The 2 ways to do this is, we call this as equation 1 and this as equation 2, to get these
expressions, or we call this equation as equation number 1 and we call this equation as equation
number 2 to get this expression.

What we do we mean is can rewrite this as x(1) =1 - 2x to as shown over here, and x(2) =4 - x(1)
the whole thing divided by – 1, which is - 4+x1 okay. So that is written over here, so that is the
first way of doing things. The second way of doing things is, we can write x (1) =4+x2 as the

184
first equation. And x(2) =1- x(1) the whole thing divided by 2. So, we are going to look at, Gauss
Siedel method using this as well as using this method.

(Video Starts: 03:08) So let us go to MATLAB okay. Now that we are at MATLAB, let us do the
Gauss Siedel iterations. So let us first, we need to initialize our solution. So our solution, we will
initialize as x = (0, 0) okay. And then the first iterations is going to be x (1) =1 – 2* x (2), 1 - 2 *
x (2) and the second is x (2) is going to be = - 4+ x(1), - 4+ x (1) okay and we will just call this
as iter1 okay.

Again, we are not writing the script. I am just showing you directly by typing it on the command
prompt. You can do this for relatively small problems into 3 or 4 variables in a few minutes. I am
going to show you how to write a function, in order to do this using the Gauss Siedel method
okay. Anyway, this is the result after the first iterations. We get our x value as [1, - 3] okay.

Let us repeat, this for iteration number 2. After iteration, after the second iteration x is [7, 3]. Let
us do this for iteration number 3. x becomes [- 5, - 9] iteration 4, iteration 5, iteration 6, iteration
7, iteration number 8, iteration number 9 and iteration number 10 okay. At the end of 10th
iteration the value of x becomes 1027 and 1023 as you can see the values are diverging. (Video
Ends: 04:53)

In this case, when we use the first equation, to get compute x (1) and second equation, to get
compute x (2) the value, the solution is diverging okay. (Video Starts: 05:08) Lets now see, what
happens, if this is, what we were going to use, and now in this case, we will use a loop, again x =
(0,0 ) okay. And let us use a loop for iter =1:10. So we will just repeat this for 10 iterations.

In this case, so x (1) is going to be equal to 4+x(2) and our x(2) is going to be equal to, let us see
x (2) is going to be equal to half multiplied by 1 – x (1). 0.5 * 1 - x(1) okay, and end okay. So let
us see what the solution for x is. Now x we have gotten as 2.998 and - 0.9990 okay. Let us go
and do one more iteration again. And we will see, what the value of x is. The value of x is 3.0010
and - 1.005.

If we were to do, another iteration okay, x is going to be 2.9995. Let us do one more iteration and
another one, okay just we will just stop, after one more, and this is the x solution, that we are
getting the solution is, slowly and surely converging to (3, - 1). (Video Ends: 06:45)

185
(Refer Slide Time: 01:58)

So, this is the just a very quick recap of, some of the theoretical underpinning, behind the gauss
siedel and Jacobi iteration. So, that you can get a feel for, the fact that gauss siedel or Jacobi
methods are not guaranteed to converge.

Okay, so now let us consider, the equations are the example from the previous lecture. We have
these 3 equations and 3 unknowns. What we are going to do, in order to make the system
diagonally dominant, is that the first equation, is going to be this guy, and the second equation is
going to be this guy, and the third equation is going to be this guy. Our guy 4 over here is the
largest value. So we want to put that, as this particular equation as equation number 2. Then this
equation ended up becoming equation number 1. And this ended up becoming equation number
3.

So, if this is the, first equation, that is 2, 1, 3 is the first equation. So let us go here. (Video Starts:
07:50) clc edit gauss siedel. What we want to do, rearrange to get diagonally dominant matrix
okay. So we want to rearrange as 2, 3, 1 right. So, As, we just called that as a new variable. New
matrix is going to be =Ab(2) that the second row, followed by Ab the third row, followed by Ab
the first row okay. So this is rearranged and we will delete this okay.

So now, we have set up the problem. Now what we want to do is, we want to solve this using
gauss Seidel method. We will define, our error and our solution over here. Initializing x = zeros

186
(3, 1). Actually what I will do is n =3. There are 3 equations and 3 unknown’s n =3 and x =zeros
(n, 1) and I will have error = zeroes (n, 1) also okay. Keep in mind, I am using the word err and,
not error because error is a keyword in MATLAB. So we want to avoid using known keywords
in MATLAB and gauss g s iterations okay.

So, for iter =1 to, let us say we want to do 25 iterations okay. And we are going to use equation
number 1 2 3 and so on up to n. So k = (1, n) and end the loop here as well okay. So, our x(k) is
going to be equal to numerator divided by denominator. The denominator it is easier to put. So,
the denominator is A(k, k). So, we will just put that first A(k, k) and let us see what the
numerator is going to be. The numerator is going be Bk. Now the Bk is as kth row and last
element right, again this has to be As (k, k) okay.

So Bs was nothing but sorry, the Bk is nothing but the last column in the As matrix, so the kth
element last column is going to be As kth row and the last column. So, that is why we have As
(k, -end) okay. (Video Ends: 11:04). What we have over here is, summation of A (k, j) * x (j)
okay. (Video Starts: 11:12) A (k,j) is going to be As (k, j) * x(j) okay. And we are going to sum it
over from j =(1: k – 1) right okay.

Now again, matrix multiplication actually works, in this particular case, because As(k, 1: k – 1)
is going to give us a row vector, and x(j) (1: k – 1) is going to give us a column vector, (k, k+1:
n) * x(k+1:n) okay. So, this is going to be the b part, this is going to be the first summation, and
this is going to be the second summation over here okay. And x(k) is nothing but numerator
divided by As( k, k)okay.

In order to calculate our error, what we want to do is, first store our x(k) in a dummy variable
called xold. So xold = x(k) and we can have the command over here. err(k) is nothing but
abs(xold) or rather abs(k) – xold okay. And we have done this, and now at end of every iterations
let say we want to print few things. So, we want to print, the iteration number, iter num 2, str iter
and error =num2str (max (err)) okay. And let us, save this, and run this and see what we are
going to get.

So, gauss Siedel, oops! So there is an error. Let us go over here. So, there is an error, in this
display, unbalanced parenthesis, which basically means that, I did not have, the brackets over
here, so that was the error. So, again in the case of debugging, what we realized was, there was

187
error, and the error said that, there were unbalanced parenthesis, with basically meant that we
have to check our brackets. So, let us just clear this screen and run the gauss Siedel again okay.

So, what happens is, an iteration number 1. The error is 3.5 and that error drops quite
significantly up to 7e -5 and we can say that, whenever the error has dropped below 10 to the
power of minus 4. We should, we will say, that we are happy with the convergence. So, we will
stop this at iteration number 23 itself okay. And if we type x, the solution that we are going to get
is 1.0001, 2.0000 and 1.0000. If you recall from the previous lecture, this was indeed the
solution. (Video Ends: 14:53)

So, this is what we have done. What we have done so far is, look at the Gauss Siedel method and
solved a 3-dimensional problem, using the Gauss Siedel method. We will have 2 assignments for
you. The first assignment is, you take this code for Gauss Siedel method, that will be posted
online, and you modify this code to run this using the Jacobi iterations. That is your first
assignment.

(Refer Slide Time: 15:23)

The second assignment problem is going to be, that you solve using the gauss Siedel method
instead of a 3/ 3, problem you solve 4 /4 problem and get the solution x. Keep in mind this is the
same problem that was given as home assignment, in the previous lecture okay.

188
So, what I suggest to you, if possible is after the end of this particular video, you go back and try
to resolve your Gauss Siedel method in MATLAB without looking at this video. And once you
are able to complete the solution, you can then go back look at this video and compare your
solution, with the solution that I have given.

In case you are stuck, then you can go back and replay this video step by step and get yourself
help to get unstuck from this, from this, from this problem okay. So, this is how I would suggest,
you use this video, in order to understand, the Gauss Siedel method better. With that I come to
the end of lecture 4.4. So thank you and see you in the next lecture.

189
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #04


Lecture No. # 4.4a
Linea Equations - Gauss Siedel – 2

Hello and welcome to MATLAB programming for numerical computations. So, what we have
done, in all these lectures so far is, solved various problems using a MATLAB scripts. However,
if you want to develop, a generic solution technique that you can then reuse for different
problems, it actually makes sense to have a MATLAB function, in order to do that.

An example that we covered in module 3, was the trapz function, to calculate integral using
trapezoidal rule. So, if we want to create, a function of the same sort, for solving linear
equations, that is something that I am going to talk about in today’s lecture. This is we can call
this as a lecture 4. 4a okay.

(Video Starts: 00:47) So let us go on to MATLAB and open our previous file, gauss siedel okay.
And now what we want to do is, rather than having this as a script file, we want to have this as a
function file, what it means by having this as a function file is our a and b matrices are going to
be supplied to us from an external calling entity. And we are going to solve this and return our
solution using this okay.

So we will say function x = gaussSiedel (A, b) okay. And we will have to delete these. I will just
cut these. I will go on over here, and I will just paste them over here. So, that we have a and b in
our workspace okay, so now we will put the comments after the function. So, solving Ax=b using
gauss siedel and that is very much, all that is needed, with respect to initializing, let us move this
n at right at the top. And n, is going to be nothing but, length (A) okay.

Usually, when you write functions of this sort, you also need to do certain error checking. For
example, we want to check that, A is an n/ n square matrix. We also would like to check whether
B is an n dimensional vector. I will skip these steps for now okay. And so, we will just go ahead
and do this.

190
So, let us, we will probably need to delete, the diagonally dominant elements. And we will just
call this as, As and that should be good enough. With respect to initialization, the x is indeed
initialized in this way, and max error is initialized in this way. We would also like to have, this 25
as another variable called max iteration equal to, let us say 100 okay.

So, 1: maxIterations and we will also need a tolerance tolX=1.0e-4 okay. So this is what we
need. And so, what we have done so far? What we have done so far is this, the things that we had
coded. For example we need to do this for 25 iterations. We have now kind of given, a variable
which is called max iterations. When we do, we tell our solution methodology to stop, rather than
you know, deciding that in an ad hoc way, we are trying to formulize that using the tolerance
which we called as tolX okay.

So, and we do not want our code to keep displaying these results. So instead, what we need to do
is, if (max (err)) < tolX, so what we mean by that is, the maximum of the error is less than tolX.
Then we want to break, end, and what break does is, it breaks from the for loop, which means
that it is going to come out of the iteration for loop. Before, we do that we would also want to
give, isSolution=true okay and we will say isSolution = false here.

Okay so that is a flag that basically tracks whether or not the solution is converged. So, if the
solution is converged, I am going to say that isSolution = true otherwise, it returns this solution
as false, and this is something that we want to return as well. So x, isSolution okay.

So this is what, we have so far and yeah so we will save this, and now what we need to do is,
basically, call this function using our usual function calls. So gaussSiedel (A,b) okay. And if we
just give this gaussSiedel (A, b), the first output argument will be returned as the A and s that is
the array, that is the vector that captures the solution okay.

And, if we use this gaussSiedel (A, b) in a naïve way, what we are going to get is, basically an
incorrect solution over here. And the reason, we do get that is because this overall matrix, was
not diagonally dominant okay. So let us consider, the more diagonally dominant case, where
basically, we have replaced the first and second rows that that should be sufficient for us, for our
purpose, so we can say a Anew (1,: ) = A( 2,: ); Anew( 2,: ) is going to be equal to A(3,: ) and
Anew (3, : ) is going to be equal to A(1,: ) okay.

191
And same thing, with bnew, also so bnew (1) is going to be equal to b(2), bnew(2) is going to
be=b (3) and bnew(3) is going to be = b(1) okay. Or actually I should say, b(1,1), b(2,1) and
bnew (3,1) okay. And we press enter, and we see bnew is this guy, and Anew is again rearranged
matrix okay. So, now we call gauss siedel, using Anew and bnew and we get the solution, using
the gauss siedel method, the solution that we stop i have end, the tolerance of 10 to the power-4
is met.

Okay so, if we want to look at our flag as well, we can write this as [x, iflag] =gaussSiedel (A, b)
and the flag should say that is not converged. So the flag is 0. Flag is 0 means that flag is false
that means is converged. So is a false state and if I repeat that with Anew, bnew okay, I am going
to get flag = true which means that the solution has converged, within the desired tolerance value
okay. And our solution, is captured in the vector x okay.

So, what we have done over here is, took the solution of the previous problem, that we did in
lecture 4.4 and converted it from MATLAB script into a MATLAB functions. A MATLAB
function, which we now hope to use later on for various other computations, if desired okay.

So, this is how you would build your functions, and your files in order to build a large set of
programs, as you become more and more experts in MATLAB. So with that, I come to the end of
lecture 4.4a and next lecture, we are going to talk about, an exciting new method called Thomas
algorithm or tridiagonal matrix algorithm thank you and see you in the next lecture. (Video Ends:
09:53)

192
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #04


Lecture No. #4.5
Linea Equations - Tri-Diagonal Matrix Algorithm

Hello and welcome to MATLAB programming for numerical computations. We are in module
number 4, and this is the last lecture of this module. In module 4 we have been covering linear
equations of the type A(x) =b where, A is an n/n square matrix and b is an n /1 column vector. In
this lecture, we are going to cover, a special type of algorithm known as the Thomas algorithm or
Tri-Diagonal matrix algorithm.

(Refer Slide Time: 00:43)

So, let us look at how a Tri-Diagonal structure looks like. The matrix A shown over here is, what
a Tri-Diagonal matrix will look like. So basically, there is diagonal elements d1, d2 up to the dn
okay, there is 1 sub diagonal row which is l2, l3 up to ln, and 1 super diagonal that is 1 above the
diagonal which is u1, u2 up to un-1 okay. This is known as a Tri-Diagonal structure.

Lot of engineering problems, end up reducing to a Tri-Diagonal matrix structure, or sometimes


what we have is, what is known as banded diagonal structure. Tri-Diagonal, is a specific example

193
of banded diagonal structure, with the band width=3. So, let us look at 1 particular example
where, we get a Tri-Diagonal structure. This example is from the computational techniques
course module 3 part 5 and the link for which is given over here okay.

(Refer Slide Time: 01:28)

If we have a rod, in through which, conduction is taking place, one end of the rod is held at 100
degree Celsius and the other end of the rod is at 25 degree Celsius, and this rod is losing heat to
the surroundings, then the overall model for the system, can be derived, to obtain something like
this okay.

(Refer Slide Time: 02:05)

194
Now if we want to solve this set of equations, we can use the numerical differentiation that we
covered in module 3 okay. And the central difference formula will be of this type. Now this is
substituted to the left-hand side and right hand side becomes, gamma multiplied by Ti-25. You
write this for multiple locations that means at the initial point T1, the next point T2, T3 and so on
up to Tn+1 okay. If you do that, you will end up reducing, this particular differential equation
into, n linear equations in n unknowns.

(Refer Slide Time: 02:37)

195
And these, linear equations for example, the example that we covered in computational
techniques lecture, consisted of discretizing in 10 intervals. If gamma=4, then the first equation
is going to be T1 =100, and for all the nodes within the rod, we get equations. If we do the
derivation, will get the equations of the type, T1 - (2+ alpha) T2+ T3=beta and so on and so forth
okay.

So, what you can see over here. The first equation, the diagonal element is 1 and the non-
diagonal elements are all 0. And for the vector b, the first element is 100. If you look at the next
equation, the diagonal element is (- 2 + alpha) okay. And the sub diagonal and the super diagonal
elements are both equal to 1. So, let us go on to MATLAB, and start trying to solve this
particular problem.

(Video Starts: 03:38) myTDMA, I have already created a skeleton of the structure. So, first step,
that we are going to do is, we are going to create the problem matrices okay. (Video Ends:
03:53). If we go and look at power point again, okay what we are going to have is, basically the
A matrix of the sort. We have the diagonal elements which we are going to store in vector d. The
super diagonal elements where we are going to store in vector u, and the sub diagonal elements
which we are going to store in vector l. That is what we are going to do okay.

(Video Starts: 04:18) So, our vector l, l(1, 1) is going to be equal to 0 okay. Let us look at this
again l(1, 1) = 0 because there is no element, in the first row for l. And in the last row, ln is also
equal to 0. So, we will do that as well okay. Ln, we should actually, let us just put this as ln+ 1, n
=10 because 10 intervals and therefore we have 11 nodes so this is also equal to 0. We need to
create n first. In this case, our n was equal to 10, our alpha was equal to 0.04 and our beta was
equal to -1. Let us just check those things, alpha=0 .04 beta=-1 and n=10 okay.

Likewise, for u also we are going to get, u (1, 1) =0. Remember, the first equation was just, T1 =
100. So, u (1, 1) was 0 and u (n+ 1, 1) was also equal to 0. Again, recall the last equation, was
also T11=0 sorry, T11=25 which meant that, the non-diagonal elements were all 0. And the entire
set of diagonal elements, that we have seen is the first guy, and the last guy, are both equal to 1
whereas, everything else is -2 + alpha okay.

So we will write d(1,1)=1 and d(n+1,1) =1 again and likewise b(1,1) was equal to 1 and sorry,
was equal to100 and b( n+1,1) was equal to 25. So, that is the structure that we have created. The

196
first and last row are taken care of. Now we want to take care of all the middle rows, so l(2:n,1)
equal to, we will decide this, u(2:n,1) again, we need to decide this. I will just put question marks
here, again this is an error but we will change that immediately. (2: n, 1)=?? And b (2: n, 1)
again=?? So, let us see what we need to put in our vector l okay. (Video Ends: 07:12)

So, our vector l, as we had said was, 1 * T1 that gets l. And 1 * T3, that is in u. So, u l3 is going
to be sorry, l2 is going to be=1, l3 is going to be also equal to 1. u2=1, u3=1 and so on. (Video
Starts: 07:38) So, all the middle elements, of both l and u matrices are all equal to 1. So, we will
put this, the diagonal elements. If we check the diagonal elements are (-2+ alpha), so we will put
that (-2+ alpha).

And our b elements are equal to beta. So, we will put that beta. And with this, we have done with
creating the problem matrices. We have saved this. (Video Starts: 08:11) Let us go to MATLAB
and run myTDMA. To ensure that, we do not have errors at this stage okay. We do it right. So, let
us say, were okay.

So here, if you look at this particular code, what I have done is, I have made a typo, instead of
putting a comma over here, I have put a dot, which is type of typo. So, let me go and change that,
and save, and let me run it, and see whether we get errors. So, does not look like. So, clc, what I
had also done was create a small function called show matrix.

So if I call the showMatrix and give (l, d, u) okay. I will get our A matrix over here okay. This is
just so for our ease of discussion, and I will just print, first 5 elements of this okay. So, these are
the 5 elements, and this looks exactly as we wanted, our first guy is going to be 1, our second
guy is (1- 2+alpha) and 1, the third guy is 0 1 (-2+ alpha 1) so on and so forth okay. And if we
were to print out the entire A matrix. We will get the desired result as well okay. And let us also
print out the b vector. And so, this is the b vector, and this is the A matrix okay.

Now at this stage, what we can also do, solve it using the methods that we have seen in lecture
4.1 which means our x. We will call this, as x1=A \ b and this is the solution okay. So, this is the
overall temperatures, in the rod going from one end to the other, from the 100-degree Celsius end
to the 25 degrees’ Celsius end. This is the result. What we want to do is, reproduce this result not
using the slash but using the TDMA algorithm. (Video Ends: 10:18)

197
So, how does the TDMA algorithm work? You can go again to this link that is shown over here
and you can see this video to try to understand how the TDMA or the Thomas algorithm works?
We are going to just use that, it is kind of similar to the gauss elimination, except we account for
the fact that, all the other elements except d, l and u are 0. So, we do not need to do all the
computation, that gauss elimination does, that is the one.

The second difference is that, at in each step, we are going to divide by the pivot element, d1 A
(1, 1), A1, A (2,2) so on and so forth, before doing a typical gauss elimination step. So, let us go
and start working that out. (Video Starts: 11:07) In the Thomas algorithm the first step, is going
to be, normalized by dividing okay. Let us put this in a loop, for i= (1 : n+ 1) the loop should not
be (n+ 1) we will come to that later, but for now, I have, let us just try to do it for the entire loop
okay.

So, the first row is going to be divided by, di, or any row is going to be divided by d. So at
whenever, you know, A (i, i) is the pivot element, everything to the left of the pivot element is
already 0. So, we do not have to worry about dividing anything in the l vector. We only have to
look at the d vector, the u vector, and the b vector. But keep in mind that because we are dividing
by d itself, we do not have to do anything with respect to d vector either.

So, what we are going to do is, u(i) is going to be nothing but u(i) divided by d(i), and same thing
with the b also b( i) = b( i) / d(i) and d(i) is going to be equal to 1. Why because d (i) is going to
be old d(i) divided by itself so therefore it is equal to 1 and that is the only thing that we need to
do. Let us also put end over here okay. So, we ended up normalizing it by dividing with the d (i).
So, that is the step that we are going to take. So, that is the first step with respect to normalizing.

(Refer Slide Time: 12:25)

198
So, what we did now was, with A (1, 1) as the pivot element, row 1 was divided by that pivot
element. So, that in the first row. Now we have 1, u divided by d 0, 0, 0 and so on. And the last
guy is b divided d okay. Now we are going to use that pivot element, to make zeroes in the first
column.

Now where do we make to need to make 0? We only need to make 0 in the row number 2, or row
number (i+ 1). Why because, that is the only non-0 element. So, l (i+ 1) is the only non-0
element in that pivot column, using pivot element for elimination okay. So, our alpha is going to
be nothing but l(i+ 1). Why, because our d (i) that means, A( i, i) is already equal to 1, because of
this step okay.

So if we want to make 0, in the i+ 1th row, ith column, then we need to just multiply row i with l
(i+ 1) and subtract it from row (i+ 1) and that is essentially what we are going to do. Your alpha
is just going to be l (i+ 1) / d(i) but d(i) is already 1. So, you do not need to make that division
okay. So, alpha=l (i+ 1), l (i+ 1) is going to be equal to l (i+ 1) - alpha okay which is basically
going to be equal to 0. So, why we should do this extra calculation? We do not need to do this
extra calculation. We can directly put that equal to 0 and that is essentially what we have done
okay.

Now d (i+ 1) is also going to change. And d (i+ 1) is going to change, because d (i+ 1) is going to
be d (i+ 1)-alpha * u(i)-alpha * u(i) okay. And the same thing with b also b (i+ 1) is going to be

199
equal to b (i+ 1) okay. What is above b (i+ 1)? Above b (i+ 1) is exactly d (i). So, it is going to be
-alpha * b (i) okay. So, that is the thing.

So, what about the u (i+ 1)? The element above u (i+ 1) is 0. The element above this guy is 0. So,
if we do any kind of a computation, it is still going to result in u (i+ 1). So, we do not need to
change u (i+ 1). So, we are done over here. So, that is all we need to do with respect to gauss
elimination okay.

This gauss elimination is applied to a special structure, the banded structure, of the Tri-Diagonal
matrix. So, let us now run this okay. I expect an error, and I will come to that, when we actually
get an error. I expect an error, and I will explain that in a minute. Let me just run it, okay and let
us see what error we get okay. Error is, the attempt to access l (12) index out of bounds. So, what
is happening over here is, this is when i= (i: n+ 1) with which means when i=11 okay we have
done this normalization, that is fine, however this cannot continue. Why, because there is nothing
called, i l (12) or d (12) or b (12), our matrix ends, at row number 11, column number 11 okay.

So, what we needed to do really was, we need to do only 10 row operations and not 11th row
operation because r (11) is not going to be used to change anything below because r (11) is the
last row. So we need to use this, to go from i= (1: n) only okay. So, let us do that. Let us go to
MATLAB, you clear, clear all, clc and run myTDMA okay. And we have our results over here
okay.

Now, what we want to do is, we want to check our A matrix. So A is showMatrix, and then we
want to display A (1: 5) and see how they look like okay. So, this is how our A matrix is going to
look. So, A matrix has now, an upper triangular structure and a special kind of upper triangular
structure. Only the u elements in the upper triangular structure are non-zero, everything else is
zero

So, we have your A (i, i) as non-zero. A (i, i+ 1) is non zero and A (i, i+ 1) is nothing but equal to
u(i) okay. So, when we do back substitution okay. A (i, i) is already equal to 1. When we take this
guy, to the right hand side, we are going to get b(i) - u( i) * x( i+ 1) okay. And that is our x (i).
We do not need to divide by A (1, 1), because A (1, 1) is already equal to 1. So, back substitution
is also significantly simplified okay.

200
So, our first guy is, our first step is going to be, we are going to say x=zeroes (n+ 1, 1) and x (n+
1, 1) and x is going to be equal to b (n+1) / d (n+1) okay. Why do we need to do divide d (n+ 1),
is because all these things, we have done it, only until row number 10, row number 11? We have
not yet divided by the pivot element. So, we need to do that, in the back-substitution step okay.
And then we have the for loop for back substitution for i=n in steps of (-1: 1) okay.

And our x (i) we said was going to be equal to b (i) – u (i) multiplied by x (i+1). Keep in mind, u
(i) is nothing but A (i, i+ 1). A (i, i+ 1) is multiplying with x (i+ 1). We take that to the right-hand
side, subtract it from b (i) and divide by 1. We do not need do that division because, dividing by
1 is going to give us the same result and end okay. And that is the back substitution that we
needed. So, let us see whether we are going to get any errors or this is going to run like a chance.

So, let me click the run, and let us go back to our command prompt, and see what solution x we
are getting okay. So, this is what we are getting as x. Let us check back, with x1 and compare the
2 results okay. So, as we can see, the results using the gauss elimination that MATLAB used
when it did backslash. And the results using our TDMA algorithm, they are matching exactly
okay.

So TDMA algorithm, is nothing different, it does nothing different from gauss elimination with
back substitution, except for the fact that exploits the structure of the Tri-Diagonal matrix. That
means, there are only 3 elements in any row which are non-zero and therefore, we need to take
significantly lesser number of computations, compared to a standard gauss elimination algorithm
okay. (Video Ends: 20:48)

(Refer Slide Time: 20:50)

201
So, that is what I wanted to cover with respect to Thomas algorithm. What I would put as an
exercise for you guys is, to do the same problem but with a larger number of examples. And that
is going to be first problem and the second problem, what it is going to be is, to solve the exact
same problem that we have done in this lecture. To solve this exact same problem but using the
gauss elimination method.

So here you will need to construct your A matrix and your b vector and compute this using gauss
elimination. So, what you will need to do is basically you create this A matrix and the b matrix. I
have not shown you my code, which is the show matrix code, which is, which I used in order to
calculate a matrix from l, d and u that is the main crux of your second problem of this assignment

So, with that I come to the end of lecture 4. 5, and indeed to the end of module 4. In module 4,
we have covered solving linear equations of the type Ax=b, in lecture 4.2 we covered naïve gauss
elimination, lecture 4.3 we change the gauss elimination to gauss elimination with partial
pivoting, as well as we saw very quickly how the LU-decomposition works.

In lecture 4.4, we moved on to what is what are known as iterative methods and covered gauss
Seidel method, and in today’s lecture that is the final lecture we covered a special method known
as Thomas algorithm for solving Tri-Diagonal system of equations. Thank you and I will see you
next week where we are going to start working on solving nonlinear equations. Thank you and
see you in the next week.

202
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #05


Lecture No. #5.1
Non-Linear Algebraic Equations - Nonlinear equation in single variable

Hello and welcome to MATLAB programming for numerical computations. We are now, in week
number 5. We are about the halfway mark, in this online course. In this week, we are going to
cover nonlinear algebraic equations. In the first couple of lectures, we are going to cover
nonlinear algebraic equations in a single variable, and finally we will cover nonlinear algebraic
equations in multiple variables.

The first lecture, lecture 5.1, is going to be about, introduction to ourselves for nonlinear
equations. And we will use, one simple method known as the Bisection method, in order to solve
one, an example of nonlinear equation in a single variable.

(Refer Slide Time: 00:55)

So, the simple example that we are going to talk about today, is to solve the equation 2-x+ln(x)
=0. Now if I am going to plot this function on the positive x axis, this is what the function is
going to look like, the blue color line represents 2-x+ln(x). Graphically, the solution to this

204
particular curve, is going to be nothing but, all the points that the curve intersects the x axis.
Keep in mind that, while the solution that is going to be the solutions where the curve intersects
the x axis; it is also possible to have multiple solutions as seen over here.

Unlike, the linear equations case, in linear equations, if the matrix A had is full rank matrix, then
we can have only unique solution. However, in nonlinear equations case, we can have finite
number of multiple solutions. If you recall from linear equations, whenever we had multiple
solutions, we had infinite number of multiple solutions okay. So, that is going to be one
difference between the linear algebra linear equations case and the nonlinear equations case.

The related set of lectures, in the computational techniques course is in module number 4 of
computational techniques, the link for the first lecture is shown over here. So, you can go to that
link, in order to find out some of the more theoretical stuff, related to solving nonlinear
equations. What we are going to primarily do in this lecture series is, really take examples of
solving nonlinear equations and solve them using MATLAB.

So, we are not going to derive any theory, but our primary focus is going to be on how to use
MATLAB, in order to solve this nonlinear equation.

(Refer Slide Time: 02:45)

205
So, the general setup, is going to be, let x be the variable of interest. And the objective is for to
find the value of x, that satisfies the nonlinear equation f(x) =0. An example for this is a model of
a reactor and the model of the reactor follows an equation that is given below. In this example
that x gets replaced by Ca and we are going to solve to obtain the concentration Ca, at the outlet
of this reactor okay.

There are several other problems of interest, from various engineering discipline, as well as from
various sciences. In this particular, set of lecture series, we are interested in solving, any generic
equation of the sort f(x) = 0. When we talk about, a variable in a single variable, or a function in
a single variable, we basically mean that x is a scalar valued variable, and f is a single scalar
valued function.

In case of multivariable case, x is going to be an n dimensional vector, and our function f is


actually not going to be a single function, but a vector of n functions, f1, f2 and so on up to fn
okay. So, let us now concentrate in the next couple of lectures, on solving the equations in single
variable.

(Refer Slide Time: 04:11)

206
And the methodology that we are going to adopt, the general strategy that we are going to adopt
is as follows okay. So when we have this particular curve, now keep in mind that, this curve how
the curve looks like and so on, is typically not known to us a prior. This is a reasonably simple
example, that we can actually plot the curve, and we can know how the curve looks like, but lot
of times we may not be able to know how the curve looks like.

In that case, we are going to start with certain initial guess which is shown by this orange color
dot over here okay. The choice of an initial guess is actually very important in nonlinear
equations, something that has been covered in the computational techniques lecture series, which
I mentioned a couple of slides earlier. You can refer to any standard textbook, in mathematics or
in numerical methods. And you will be able, to know, what, how the initial guess has a profound
impact on, how the solution evolves.

So we start with an initial guess, and this is going to be an iterative solution technique, just like
the Gauss Siedel or the Jacobi iterations where, in module 3, excuse me, in module 4 okay. So
we start with certain initial guess and we use a chosen method, chosen strategy, to move in the
direction of the solution. So that direction of the solution, is shown by the brown arrow and the
orange dot moves from its initial position to a new position okay.

Now, we actually check whether some kind of stopping criteria is satisfied or not. What start
stopping criteria could be, is whether or not that change in x, is sufficiently large and that is the
stopping criteria based on x, or whether f (x) is sufficiently close to 0. So the stopping criteria, is
either going to be from a graphical view point, whether this arrow, this brown color arrow is
short enough, so that we can say, that we have reached close enough the solution or if this
vertical distance is, short enough, so that we can say that, we are reasonably accurate, when it
comes to solving the equation f(x) = 0.

Clearly in this particular geometry case we have not reached our desired solution. And, therefore
we need to repeatedly solve these 2 steps over and over again. We use the same method to
choose, to move further in the direction of the solution. Again, verify whether the stopping
criteria is met. If the stopping criteria is met, we have our solution, if not we keep repeating these
2 steps over and over again okay. So, this is the methodology that we are going to implement in
an example that I am going to cover next.

207
(Refer Slide Time: 07:01)

And that example, is going to be bisection method, in a single variable okay. So, this bisection
method, is what is known as a, bracketing method, that means we do not start with a single
solution. Unlike this figure, we are not going to start with a single solution, but we are going to
start with 2 solutions. One of them is going to be, on one side of our actual solution, and the
other guess is going to be on the other side of the actual solution.

Which means that, our f(guess 1) and f(guess 2) are going to have opposite signs okay. So, start
with 2 initial guesses xl and xu, x lower bound and x upper bound. Verify that signs of xl and xu
are different. So, in this case, let us say our fl was going to be equal to 1, and our xl was going to
be equal to 1 and xu was going to be equal to 4 okay. Then, first we verify, whether f (1) is of a
different sign than f (4). If it is of a different sign, then those are permissible initial guesses okay.

And then, we choose the next guess, as nothing but the midpoint of the previous two guesses. So
our xnew is going to be nothing but x (l) +x (u) / 2 okay. Then we calculate our f (xnew), and
depending on where the sign i is, we are going to either retain xl or xu. So, for example, our
guesses are over here and over here are the initial guesses. The next is going to be, (4+1) / 2
which is 2.5. So, let us say that 2.5 is over here which is on the same side as xl. So we drop xl
and xnew becomes our new xl okay.

208
We continue that, and let us say our x, xnew again comes on the other side of this dot, which
means on the same side of x(u). In that case, we will drop xu instead of xl. And we keep doing
that over and over again, in an iterative fashion, and that becomes our bisection method okay.
The link, to the computational techniques module4, part2, is given over here. You can go to that
link, to get and to understand, some of the theoretical aspects behind the bisection method okay.

So, let us head on over to MATLAB and try to solve the equation 2-x+ln(x) = 0, using bisection
rule. Our initial guesses are going to be, x (l) = 1 and x (u) = 4. So, let us go on to MATLAB to
solve this okay. (Video Starts: 09:51) edit bisecRule okay. Yes, open a new function, script file to
use bisection method to solve 2-x+log (x) = 0 okay.

Initial guesses, xl =1, xu=4, fl is going to be equal to 2-xl+log(xl) and fu is going to be equal to
2-xu+log(xu). Check if signs, of fl and fu are different okay. So how do we check, whether the
signs are different or not, we will just multiply fl and fu, if the signs are same the product fl
multiplied by fu is going to be positive? So, if both fl and fu are positive, the sign is, of the
product is going to be positive.

If both fl and fu are negative, again the sign is going to be positive. So therefore, we are going to
multiply fl and fu, and check whether it is less than 0. If it is less than 0, we are good. If it is
greater than 0, that means fl multiplied by fu is positive. Then we are not going to be, we know
that fl multiplied by fu is positive, that means fl and fu have the same sign, so okay.

So, if fl * fu greater than 0, then error, initial guesses should have different signs. And fl
multiplied by fu is actually going to be less than 0. Then we are fine. So we do not need an else
condition over here. Iterative solution using bisection okay. So, let us look at, so let us say error,
is going to be equal to nothing but abs (xl-xu). That is going to be our error, and let us say, we
are now going to have our next solution. xnew is going to be (xl + xu) / 2 right. So, (xl + xu) / 2.
So that is going to be our xnew okay.

What we want to check is, whether f (xnew) that is fnew whether that has the same sign or not.
So fnew, is going to be 2-xnew+log (xnew) okay. So, let us check, the sign of fnew multiplied by
fl. If fl multiplied by fnew is going to be greater than 0, which means fl and fnew have the same
sign, then xl = xnew and fl = fnew, else xu is going to be equal to xnew and fu = fnew and so this
is one single iteration.

209
We need to iterate over this, but let us go on and run one single iteration and see whether that
works or not okay. So let us go to MATLAB and say bisecRule. Let us hope, that we do not get
an error. Let us press enter okay. So, it works for now okay. So now our xu is, so let us check
what our xl is. xl is 2.5, xu is 4.0, our fl now is 0.4163 and our fu remains the same as negative
0.6137 okay. So, what has happened in this particular case is, we have run one single iteration
okay of our bisection rule and in that single iteration of the bisection rule. (Video Ends: 15:02)

Now let us go on to power and see what has actually happened. So, we have moved from, this
particular x=1 to a mid which is located somewhere over here okay. (Video Starts: 15:15) So
now if you check the value of, f (l) or f (u), they are still sufficiently far away from 0. Or if you
check the distance between xu and xl, we still have a very large distance between xu and xl. So
therefore we conclude that our iteration has not converged.

So, let us go on and run this for one more iteration. So, I will just go on into this section, right
click and I will say, evaluate current section. We can do this sectioning, because we have used a
double percentage sign over here okay. If we do not use, that sectioning, it is not possible we can
then instead, highlight this entire passage. Right click on this, and say evaluate current, sorry
evaluate selection. So, the selected part, will be evaluated again, or we can just go over here and
say evaluate this section.

So, evaluate current section, which means only these commands are going to be reevaluated, and
let us see what our xl and xu are. Now xl remains 2.5, our xu has changed from 4 to 3.25, now
3.25 is the midpoint between 4 and 2.5 okay. And our fl, if we check, will remains the same as
before 0.4163 and if we check fu, now fu would have changed and if fu has become -0.0713.

So, let us go to MATLAB and do this in a loop. So, for now, I will just do this in a for loop okay.
So, for i=1: maxIter, where maxIter basically means max iteration. So, let us say maxIter equal to
say 50. Let us run this for 50 iterations okay. Let us select this, right click and say smart indent.
Smart indent means. The entire set of code will get beautified and we will go at the end and press
end okay. So, we now have created this in a loop and in a for loop and we want to check the
errors okay. So save this and error, keep in mind is nothing but abs value of xl-xu okay. And let
us run this. Let us see if we get an errors okay.

210
There are no errors okay. And what do we have over here is, err, which is the difference between
the xl and xu is 10 to the power of -15. So that is pretty small number. So that means, we should
be extremely satisfied with our solution, and if we check our fnew, our fnew is also 1*10 to the
power-15. So, that means we are now very close, to the solution fx = 0 as well okay. So we
actually did not need 50 iterations. So let us go back and reduce the number of iterations to 25
and see how this works. So save this and run it.

Let us go over here, and check our error. err is nothing but difference between xl and xu which
will give us, a tolerance in our solution, that we want in the solution x that we want and that error
is 10 to the power-7 and fnew which is trying to see how close our solution is to 0, is 10 to the
power -8. So, I think with this also it is a sufficient amount of convergence. (Video Ends: 19:07)
So let us go back to our PowerPoint and see what we have done. So we have solved this using
bisection method right. Now we have kind of solved it in a naïve way, not in a very polished
way. We will change this in the next lecture okay.

However, what we have done is, we demonstrate the core principle of any non-linear equation
solving, that we start with an initial guess or a set of initial guesses. And we use a chosen
strategy, to the move in the direction of the solution. In this case, that the case of bisection
method, the movement in the direction of the solution, was taking the average between the 2
guesses xl and xu. And checking whether the stopping criteria is satisfied. The last part, we have
not yet done, that is something that we would do in the next lecture okay.

(Refer Slide Time: 19:59)

211
Methods that to solve nonlinear equations, that were covered in computational techniques
module 4. Where, Bisection and Regula Falsi methods were bracketing methods, Secant, Fixed
Iteration and Newton Raphson were the open methods. We are not going to cover, all the
methods. We do not intend to be exhaustive in covering all the methods. But we want to cover
the most important of the methods. The most important bracketing methods in my opinion is the
bisection method that is also the method that is used partly that is used in the MATLAB function
fzero, and then we have open methods which were fixed iteration and Newton Raphson method.

Fixed iteration, is kind of similar to the Gauss Seidel method that we saw in the previous
module. Newton Raphson method is one of the most popular methods to solve nonlinear
equations, and a related MATLAB method is known as fsolve, which uses a very different sort of
algorithm, in order to solve the equations. We will not go into the algorithm for fsolve, but we
are going to primarily use MATLAB functions fzero and fsolve as well. So that is going to be our
strategy in the next 5 or 6 lectures of this module.

We are going to talk about Fixed Point Iteration and Newton Raphson method in single variable.
We are going to talk about how to use fzero method in order to get solution. We are then going to
talk about multivariate Newton Raphson method and multivariate Fixed Iteration method and
finally end with using fsolve. In order to solve a sequence of nonlinear equations, n nonlinear
equations and n unknowns and that is going to be the strategy for module 5. So with that I come
to the end of the lecture 5.1. I will see you in the next lecture.

212
MATLAB Programming for Numerical Computation
Dr.Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #05


Lecture No. #5.2
Non Linear Algebraic Equations – Using MATLAB function fzero

Hello and welcome to MATLAB programming for numerical computations. We are in week
number 5 lecture 5.2. In this week we are covering solving nonlinear algebraic equations and in
today’s lecture we are going to focus on using MATLAB function fzero. The MATLAB function
fzero is a method to solve a linear, a single nonlinear equation in a single unknown.

In the previous lecture we have seen the bisection method which was a method to compute the
solution to f(x) = 0, given 2 initial guesses. The 2 initial guesses lie on either side of the solution.
fzero is a MATLAB program that uses a techniques. Similar to bisection rule it uses bisection
and a couple of other techniques depending on the problem statement.

So we are going to do 2 things in today’s lecture, first see how to use the function fzero, second
we are going to modify the bisection rule that we made the program in the previous lecture in
order to do and understand bisection rule and understand numerical equation solving in a better
way okay.
(Refer Slide Time: 01:22)

213
So let us look at the MATLAB function fzero. We are going to use fzero in order to solve the
same equation. That we did in the previous lecture that equation was 2-x+ln(x) okay. Now if we
are going to do help fzero, we will find out how to use fzero. Some of the most important things I
have listed over here.

The usage of fzero is going to be as follows. The solution xSol that is the output from fzero =
fzero followed by the name of the function in the x variable followed by the initial guesses xSol
is the resulting solution, funName is the name of the function that returns the value of f(x) given
the variable x okay.

There are different ways in MATLAB in order to pass on the name of the function to a calling
function. We are going to use basically this @ func @ representation using what is known as
anonymized functions representation okay.

So this is the method that we are going to use. as seen over here the method is as shown is @
followed by in brackets the name of the variables that you need to solve for, followed by a space,
followed by the name of the function in which you will use to get the value f(x) = 0 that you
want to solve so funName is going to return f(x) given the function given the values of variable
x.

214
Now funName itself can have other parameter as well in which case we will have the other
parameter listed over here. This x and this x should be the same okay. That is the variable that
fzero is going to solve for. When we use this MATLAB function fzero okay. There are various, as
I said there are other ways of using functions as well and the most common way is to use @
funName.

We are not going to use that particular method for because that is somewhat of an older method
and this method of using functions. Although a little less simpler in the beginning is actually a
very powerful method okay. So let us go on in MATLAB and use function fzero in order to solve
f(x) equal to 0.
(Video Starts: 03:53) So edit, let us say fun4Bisec okay. So we are going to use the function and
the name of the function I am just going to call it fun4Bisec okay. So function fval =
fun4Bisec(x). That is the name of our function okay. Keep in mind the name of the function that
we have right over here should be the same this name has to be the same as the name of the file.

The file name is what the, what MATLAB recognizes the function as, so as far as we are
concerned the name of the function that we write over here and the name of the file always needs
to be the same okay. And the function that we have our f(x) is nothing but fval = 2-x+log (x) and
end. End is optional so as a good programming practice we are going to include the end also over
here okay.

So now let us say what the fun4Bisec is going to be for x=1, so f(x) = 1, so we will say x = 1 and
f is just 2-x+log (x) okay. And for x = 1, 2-x+log (x) that value is also equal to 1 let us call that
function fun4Bisec using x and that should return the value of 1 okay. Let us say we were to
recall it using 2. That should return f (2). If we call it using 3, it will return f(3). If we call it
using 4, it will return f of 4 okay.

If you recall from the previous lecture, we had given our initial guesses our xl we had given as =
1. Our xu we had given equal to 4. The reason why we had done that was xl for @ xl f(1) was
equal to 1 @ xu f(4) was equal to -0.6137 which are of opposite signs if you recall in bisection

215
method, we needed to give xl and xu of opposite signs and thats what is we need to do in fzero as
well.

So now that we have created that function okay. We are going to say xSol = fzero okay. @x okay.
x is the variable that we want to solve for, so @ x spacebar the name of the function. The name
of the function is fun4Bisecx okay. So, f(x) is the name of that is the function. So, f(x) is exactly
what we give over here okay.

This is nothing but our f(x). Just before f(x) we need to give with an inside the bracket, the name
of the variables that we are going to solve for. And our initial guesses are 1 and 4. We will
separate them with either a comma or a semicolon okay. And we press enter and we will get
xSol.

If you recall from the lecture 5.1, 3.4162 indeed was the solution that we got in the previous
lecture okay. Let us go over again okay in the syntax of fzero. fzero is going to be xSol, the
solution is going to be fzero in brackets, the name of the function, name of function of variable x,
the initial guesses. Our initial guesses were 1 and 4 okay.

Now before the name of the function on x what we had was, the variable that we need to solve
for @ x. So @ in bracket the variable we need to solve for followed by name of the function file.
The function file was fun4Bisec. I will later type fun4 and click and press tab over here and
MATLAB will auto fill this for me based on the function names that match with fun4 okay.

So this is really this is really the main part of fzero. The main part of fzero is how to pass on that
function. And what are or how to give the initial guesses okay. We press semicolon at the end of
it and we will get the solution without a going on the screen okay and we get xSol okay. Let us
say we were to give this instead of 1 and 4, let us say we were to give this as 2 and 4 again. 2 and
4 bracket.

The solution and to bracket the same solution and if we were to press enter, we are going to get
the same solution again. If you see on to your left hand side, our value of xSol has not really

216
changed. Our xSol is the same as 3.1462 okay. Now instead if we were to give our initial guesses
as 0 and 1 okay, now this is a problem. The problem is because log (0) cannot be obtained. So let
us change this from 0.

Let us change to a very small number say 1e-5 and let us see what we get okay. And we give this
and now we are getting the other solution. The other solution was 0.1586 and the first solution
was 3.1462. Recall what we did in the previous lecture. We had shown that the curve intersects
the x axis at 2 different points. We have now found out how to get both the solutions using fzero
okay. (Video Ends: 10:34)
So let us go back to power point okay.
(Refer Slide Time: 10:37)

What the problem that we just solved was, to use fzero to solve the nonlinear equation 2-x+ln(x),
we have created a function called fun4Bisec and use that function and passed it on to fzero. Now
what we are going to do is the bisection method from the previous lecture. We are going to
modify it okay. Instead of hard coding our f(x) inside our bisection method, we want to use the
function that we just created for fzero and we are going to use that function in our bisection
method.

(Video Starts: 11:10) So let us go back to MATLAB and do that okay. So again just to recap what
our aim is, let me bring up our let us clear and clc clear all. Let us edit our bisection rule that we

217
created in the previous lecture and we are going to modify that okay. In what we had done was
this f(x), we had directly hard coded it in the overall running program.

Use a new method in which we are going to use this function fun4Bisec that we recently created
in order to solve using the bisection rule okay. And this change is going to be fairly simple and
everywhere. Where we are going to calculate function? You will, the function we are going to
replace it with the function call itself. So fun4Bisec (xl) okay.

That is all. Remember what we did a few minutes earlier right. We said fun4Bisec okay. And
fun4Bisec (4) gave nothing but the value of the function, fun4Bisec (1) gave nothing but the
value of the function. If we were to say x= 4 and we were to call fun4Bisec with x, is going to
give the value the function f(x). So all we going to do is, change this to fun4Bisec.

So let us just change this to fun4Bisec and fNew is also going to be fun4Bisec fNew sorry, fNew
is going to be fun4Bisec, xNew, fun4Bisec. And that is all the changes that we need to do is
nothing else that is changed. Let us save this and solve it in MATLAB okay. And the way we
going to do is bisecRule, enter. And let us see our xNew and xNew is 3.1462. Now let us change
our initial guesses instead of 1 and 4, let us change them to 0 or rather 1e-5 and 1.

Let us run this fun4Bisec sorry, let us run this bisecRule and see what the solution is going to be.
xNew is going to be 0.1586 as we had obtained using our fzero method as well okay. And finally
what we are going to do is, we are going to make one more change in our MATLAB code and we
are going to have a stopping criteria based on an error criteria okay. So error tolerance are, let us
call this as tolX okay.

Error in x is 1e-6. So if the error falls below or if x new-xl falls below 1e-6, we are going to stop
this solution technique and that is what we are going to do over here. If abs of err is going to be
less than tolX okay, then break. This is something that we have seen earlier also end .So we are
going to break end less than 25 iterations.

218
So let us actually change the maxIter to 100 okay. So that we give enough number of iterations
for the bisection rule in order for it converges. Let us give our initial guesses as before are 1 e-5
and 1. Let us save this and run bisecRule. Let us clear the screen clc and bisecRule okay. And our
xNew in this case is 0.1586 okay.

And our and if we check our error, our error is 9*10 ^ -7 which is less than 10 ^ -6 okay. So now
let us do one final thing. Change the initial guesses back to 1 and 4 and see how this runs okay.
And run this bisecRule and press enter and our xNew is going to be the solution 3.1462.
That is the other solution over here. Our error is going to be 7*10 ^ -7. So our solution 3.1462
has converged to the error tolerance that we needed. And if we type i, we will know that we have
converged in 23 iterations okay. (Video Ends: 16:33)

So what we have done. To recap what have done in this lecture, we have done 2 things in the
lecture. The first thing that we did was to use fzero in order to solve the nonlinear equations 2-
x+ln(x), the second thing that we did was 2 make our bisection rule better with 2 different things.
One is to make it use the function fun4Bisec instead of hard coding it inside the script file itself

And the second change with it was that we stopped when as particular stopping criteria was met
when the tolerance value was less than the desired accuracy. At that time we stopped the
execution of the loop rather than letting the loop continue for a large number of iterations okay.
So with that I come to the end of this lecture.

So thank you for listening to this lecture and in the next lecture we are going to cover the next set
of numerical techniques for nonlinear algebraic equations. Specifically, we are going to start with
fixed point iterations which is an open method and not a bracketing method. Thank you and see
you in the next lecture.

219
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #05


Lecture No. #5.3
Non Linear Algebraic Equations – Fixed point Iteration (Single Variable)

Hello and welcome to MATLAB programming for numerical computations. We are in week
number 5 in this week. We are covering methods for solving nonlinear algebraic equations of the
form f(x) = 0. In lecture 5.1 we covered introduction to nonlinear algebraic equation solving
techniques as well as covered 1 technique known as bisection method.

In lecture 5.2 we covered MATLAB function called fzero that can be used to solve the equation
in single variable. In today’s lecture we are only going to cover single variable fixed point
iteration method.
(Refer Slide Time: 00:49)

So the fixed point iteration is also known as method of successive substitution the reason for this
will be clear in a couple of minutes from now. Related module from computational techniques
course is module number 4, lecture 2 the link for which is given below. That is the 10th lecture in
the computational techniques course.

220
The fixed point iteration instead of solving the equation of the form f(x) = 0. It instead solves an
equation of the form x = g(x). Now question is how g (x) and f(x) are related okay. So if we were
rewrite this equation as x - g(x) = 0 or g(x) - x = 0. We would have recast this equation as the
equation f(x) = 0.

We should note that g(x) is not a unique representation of the overall function okay. So there are
multiple ways in which we can convert f(x) into g(x) okay.
(Refer Slide Time: 01:59)

So looking back at the equation that we want to solve. The equation that we want to solve is x i+1
= g(xi). So what it means is that we start with initial guess x0 okay. And in this equation and in
this original equation we just substitute the value of x0. The result that we get we assigned that as
x1 okay. Next again x1, we substitute in our function g(x) the result that we get, we assign as x2
so on so forth.

What we are actually doing is, we are successively substituting x(i) into the equation and getting
x(i+ 1) and therefore it is also known as method of successive substitution. Now this method is
not guaranteed to converge under all conditions and when it converges and when it does not
converge has been also studied quite extensively.
(Refer Slide Time: 03:00)

221
What we will do in today’s lecture is this, we will take up the example that we have been doing
for the last couple of times 2-x+ ln(x) convert it into the form x = g(x) and see what solutions we
are going to get when we solve this in MATLAB. The 2 ways I have shown over here are you
just take this x onto the right hand side and you will get x = 2+ ln(x).

Other option is take these guys to the right hand side and get an exponent that will result in ln
(x)= (x-2) or x = e ^ x-2. So in case 1, we have g(x) = 2+ ln (x), in case 2, we have g(x) = e ^ x-2.
What we will do is, try to find out which equation for g(x) leads to which solution.

We have seen previously that there are 2 solutions to this problem. One solution is 0.1586 and
the other solution is 3.1462 okay. Depending on whether we start to the left of the solution,
whether we start between these 2 solutions or whether we start to the right of the solution. We are
going to get different results using the fixed point Iteration and that is something that we are
going to study in today‘s lecture okay.

So when it comes to the fixed point iteration, what we are going to do next is, to look at solving
x = g(x) for this formulation as well as for this formulation for different initial conditions okay.
Let us head over to MATLAB and write our codes to do the same. (Video Starts: 04:47) Edit
fixPtIter. So that is a new file that I will create to solve nonlinear equations using fixed point
iteration.

222
Case 1: g(x) is 2+ ln(x), Case 2 g(x) is e ^ x-2 okay. So Let us say initial conditions and initial
condition, let us say will start between the 2 solutions. Initial condition is x not equal to 1. We
are going to do for several iterations. So Let us say maximum number of iterations let’s just put it
as 50 okay. And Let us now start of i = 1 to maxIter okay.

So okay, what our fixed point iteration was is, just going to be x = g(x), g(x) was 2+ log(x) okay.
And that should be fine. Let us go on to MATLAB and just do help log to just ensure that log is
indeed natural logarithm and not log to the base of 10 okay. Yes that indeed what it is so log is
for natural logarithm, if you want logarithm to the base 10 which is not what we want in this
problem but in another problem.

Let us say if we wanted logarithm to the base 10. For that the MATLAB command is log10. And
log is the MATLAB command for natural logarithm. Let us clean this screen and let us go on
back to our code okay. So we also may want to compute our error. So I will just comment this for
now. Let us say err = abs(x-xold). And xold, we will comment this also, xold = x.

So whenever we run this stimulation once we have computed the error, then the current value of
x we have just now we just code in xold. And then we go back to the iterations okay. And Let us
say computation using fixed point iteration okay. Now what I expect? I expect there to be an
error. Let us see whether you can look at the code and figure out what that error is going to be.

Anyway I will held on to MATLAB and try to solve this problem. And we will see that we are
going to get an error over here. And I will go through, why we got that error and how to fix it. So
fixPtIter, enter yeah. So we are getting an error which is undefined function or variable x error in
line number 11.

So let us click on this line number 11. What does the error say? The error says that when
MATLAB reaches line number 11, it does not understand the variable x. What does that mean is
that the variable x is not in its workspace. So let us go to line number 11 and see what is
happening okay. So that does not seem to be any problem with 11.

223
So let us see what the problem is, we have defined x0 we have defined maxIter but at this point
we have not defined our variable x that appears on the right hand side over here okay. So what do
we mean by an initial guess? Initial guess basically means that before the first iteration starts,
our variable x should get the value of x0. So actually I should be putting it at the computation
part x = x0.

So x in bracket 0 is x0, x1 is g (x0) which is what happens when i = 1, x2 is g (x1) which is what
happen when i = 2 so on and so forth. So this is the change that we need to make. Save this and
run it and we should be able to get the solution. So fixed point iteration when we click that, we
get the solution x this 3.1462.

Let us now calculate what the errors and old values are. So let us uncomment this okay. Again
the same error we are likely to get over here. If we do not change something xold at this point is
not going to be defined. So what I will do over here is, I will also define xold as x0 okay. And
save this, run this and when it is run, the error has actually gone to 0.

We may not want to keep solving using fixed point iteration till error goes below the machine
precision. We need to decide a priori what our error tolerances are. So the solution is 3.1462. We
want the solution only up to let us say the fourth decimal place. So therefore we can say that the
error tolerance should be 1*10 ^ -4. And that should be sufficient for us.

So what we will do over here is, say tolX tolerance in x = 1e-4 and we compare whether the error
is less than or greater than the tolerance. So if err < tolX then come out of the loop using the
command break, end okay. This is exactly what we have done in our earlier lecture as well. And
now let us save this. Let us add a command that will clear all the variable.

Clear all and then let us run this and see what happens okay. And we have run this with an initial
condition of x0=1. Our solution is 3.1462, our tolerance was 1e-4 and the error at which we
stopped was 3.6e-5. The 11 iterations in order to reach the error below our error threshold. So

224
that is all we have at this point of time. So that is a fair amount of information that we are
actually able to generate through this 1 code okay.

So now let us change from x0 = 1 to x0 = 0.1 okay. And when we change to x0 = 0.1, what is
going to happen is log ( 0.1), will result in x as a negative number and we will eventually get our
x value as an imaginary or rather as a complex number. So let us run it and see what we actually
get okay. So as we had said that we get our x value as a complex number.

Let us type out x. x is a complex number and the reason why that happens is when we start of
with x0, so let us say we were to just copy this and paste it in the MATLAB screen and replace it
with x0. Our x value is -0.3026. Now if we were to take log(x) that is going to result in an
imaginary or rather in a complex number. And therefore at this stage we should not be continuing
this overall method because we are not guaranteed to reach the solution that we desire.

If we were to desire if we were desiring a real number solution okay. So again in this case it is
we are we were lucky that we were able to reach the solution 3.1462 and our imaginary part of
that complex number was 0. That may not always happen. So if you were expecting a real valued
solution and you get a complex number that is also a sign that you should break the loop that is
solving it.

However we are not going to change this code for now. Let us now try for a different initial
condition. Let us take an initial condition of x0= 4 and run this simulation. When we run this
with x0= 4 within 9 iterations we have again converge to 3.1462. Let us take another x0 like say
x0= 40 and see what happens. If we have x0= 40, this takes 11 iterations in order to reach again
3.1462. (Video Ends: 14:22)

So what we see over here is that if we were to use this particular g(x), we are going to reach no
matter where we start on this line. We are going to reach the second solution if we start of too
close to 0, we will actually get a complex valued result because we end up with logarithm of a
negative number during our computation okay.

225
So that is the first part of our solution technique. The second part is if we were to solve this for
x=e ^ x-2, so let us go back (Video Starts: 15:00) to MATLAB and change this to e ^ x-2 okay.
And for us to be able to do that the only place this remains same our initial conditions maxIter
remains same, tolX remains same, this part also remain same.

The only thing that changes is this particular line and this will change to exp x-2, e ^ x-2 okay.
And rest everything is also going to remain the same. Let us start with low enough initial
condition. Let us start with an initial condition of 0.1, which is to the left of the first solution. Let
us save this and run this code. When we run this code, the code runs fixed point iteration has
converged within 5 iterations okay.

And the solution is x = 0.1586. Let us now start between the first and the second solution. Let us
say x0=1. Let us solve this run and it is converge in 7 iterations. And we have reached a converts
converge solution of 0.1586 again okay. Let us say we were to start at 3. 1 which is very close to
the second solution. Let us run this and see what happens.

We have taken 11 iterations, starting with an initial condition of 3 .1. Again we have reached the
solution x = 0.1586. (Video Ends: 16:42) So as long as we are starting to the left of the solution
we are converging to the first solution. Let us see what happens if we start to the right of the
solution. (Video Starts: 16:52) If the initial guess was to the right of 0.315, let us say what we
get.

So let us start with an initial solution of 4, save and run this okay. And when we run this, our x is
going to be infinity okay. Even in 50 iterations our fixPtIter has not converged it has indeed
diverged so okay. So Let us again look at what this is x = e ^ x-2. So Let us put it over here and if
x was e ^ x0 - 2. Let us call this as x1 okay. x1 was 7.39.

x2 we will now calculates x2 = g (x1). So we have put that press enter and from 7.34 we have
gone to 219 okay. Let us say x3 was x2 e ^ x2-2 and we have got 1 .74 *10 ^ 94. So as you can
see the solution has very quickly diverged. (Video Ends: 18:09) So what we have observed over

226
here is, if you start with g, if you take g(x) = 2+ ln(x), no matter where on this line you start you
reach solution number 2 that is 3. 1462.

If you take g(x) = e ^ x-2. If you start between 0 and the second solution you will converge to the
first solution. If you start beyond the second solution our fixPtIter scheme is diverging okay. So
that is all I wanted to cover in this lecture today okay. With that we come to the end of lecture 5.
3.

In lecture 5.4, we going to cover a new method known as Newton Raphson method which is
probably the one of the most popular methods for solving nonlinear algebraic equations. So
thank you for listening to lecture 5.3 and I will see you in the next lecture bye.

227
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #05


Lecture No. #5.4
Non Linear Algebraic Equations – Newton Raphson (Single Variable)

Hello and welcome to MATLAB programming for numerical computations. In module 5 we


have been covering methods to solve non linear algebraic equations of the form f (x) =0. In
lecture 5.1 we covered a numerical technique known as bisection rule. Bisection rule we start
with 2 initial guesses and use the bisection rule in order to obtain the desire solution. In lecture
5.3 we covered different numerical technique known as fix point iteration.

Fix point iteration uses only a single initial guess then we recursively solved equation in order to
get the desired solution. In this lecture we are going to cover a third method known as Newton
Raphson method which is a very popular method. Today we are going to cover a single variable
version of Newton Raphson method. In a subsequent lecture we are going to extend the Newton
Raphson method to multiple variable cases. However today what we are going to do is 2 things.

First we will see how we can use Newton Raphson in MATLAB in order to solve a problem f (x)
= 0. And later on we will see how quickly Newton Raphson method converges to a desired
solutions okay. So talk about Newton Raphsons.
(Refer Slide Time: 01:33)

228
It is a popular method which was covered again in module 4 part 2 of the computational
techniques course the link for which given over here. Each new guess for Newton Raphson
method we can start with an initial guess x0 and use this equation in order to obtain x1 and then
we use x1 in order to obtain x2 so on and so forth.

We are interested in solving the problem f (x) = 0 and given the function f(x). We also need to
calculate f`x that is the first derivative of the function okay. So if we have the value of f(x) and
f`x at the current guess and then this is the equation that will be used in order to compute the new
guess using the Newton Raphson techniques.

A Newton Raphsons technique is a popular technique because it has one of the fastest rates of
convergence among the various methods use for solving non linear algebraic equations okay.
(Refer Slide Time: 02:35)

229
Let us look at now how we will use Newton Raphson in order to solve example that we have
been covering so far in this module. The example is f (x) is given by 2-x + ln(x) and we need to
find the solution to the particular non linear equations. As I said in the previous slide in order to
calculate this solution using Newton Raphson method we also need f `(x).

And f`(x) for this problem is nothing but -1 + 1 / x okay. So once we are equipped with f (x) and
f`(x), we can go over to MATLAB and use Newton Raphson to calculate the solution here using
an iterative method okay. (Video Starts: 03:23) Let us open the file from the previous time
which was fixed point iteration. And let us copy and save this as different file.

Let us call this as newtRaph, newtRaph okay. To solve non-linear equations using Newton
Raphson there is no case1 and case2. Our f(x) is 2-x+ln(x) okay. So what I am doing over here in
today’s lecture is rather than starting from a blank slate, I will take the solution that we obtained
in the previous lecture. And I will edit the solution in order to save the problem using Newton
Raphson.

So this is one more way to doing this. We will able to write a Newton Raphson code little bit
more quickly than if we were starting from a blank slate okay. We will again keep this same
initial guess x0 =1. Let us keep maxIter as 50; tolX is just keep it as the power of -4 okay. This is
where we need to change okay.

230
So for Newton Raphson method, first we need to calculate f (x) and f is nothing but 2-x+log (x)
and f ‘, we will represent as df. df is -1+1/x right. So -1 comes from here and 1/x comes from
here okay. So x i+1 is nothing but x-f / df. And that is our solution using Newton Raphson and
then we continuing calculating the error we store x in xold.

And if error is less than the tolerance value, then we stop okay. So that is our Newton Raphson
technique. Let us run it and let us look at the solution okay. So for an initial guess of 1 we
actually do not get a solution and this is a demonstration of one of the problems with Newton
Raphson. If we do not start with decent initial guess for Newton Raphson just like fixed point
iteration.

We may not get a Newton Raphson to converge. Let say instead of 1 we were to start with
different initial guess of 2. Let save this and run okay. If we were to start with an initial guess of
2, Newton Raphsons method converges in 4 iterations to the 3.1462. So let us head over back to
power point and see what we have done so far. (Video Ends: 06:31) We took up the same code
had used last time for fix point iteration and modified it using the Newton Raphson formula.

And this was the Newton Raphson formula. The new value of x is nothing but old value –f.
Function f(x) / f’(x). So now that we have solve the problem using Newton Raphson. In
MATLAB what we are going to do in the next few minutes is, to do some analysis of Newton
Raphson technique. In fact we do analysis of both Newton Raphson and fix point iteration we
have covered in lecture 5.3.

And we will compare how the 2 methods behave visually each other. We have already seen that
fix point iteration method. Take something 10 iterations in order to converge. Whereas, Newton
Raphson converges in something like 4 iterations.
(Refer Slide Time: 07:29)

231
Why that is because Newton Raphson has what is known as the quadratic rate of convergence
which basically means that the error i+1 iteration is approximately equal to error in ith iteration
to the power 2, on the other hand fix point iteration has linear rate of convergence. For example
what that means is, if let us say the error ith iteration for 0.1, then i+1 iteration will be the 10 ^
-2.

Then it will go 10 ^ -4, 10 ^ -8 so on and so forth. So it has usually a much faster rate of
convergence compare to fixed point iteration. Derivation and analysis of this was covered in
module part 4 of the computational techniques the link for which is given over here okay. So let
us look at what at mean, when we talk about quadratic rate of convergence, so what we want to
do is, want to look at how error in i+1th step compare with the previous step.

(Video Starts: 08:38) So let us go back to MATLAB and let us look at the Newton Raphson okay.
So this was the overall Newton Raphson solution. So if we have defined our error in this
particular manner, what I will do is, let us say I will call this error in ith step and so error i is this.
So let me save this and let us run this and that we have 3 different error values okay.

So let us type this err vector okay. So this is error1, error2, error3, error4 okay. So prevErr is,
prevErr is going to be error (1:3) and currErr = err (2:4) okay. So let me just do prevErr, to show

232
what I am actually going okay. So prevErr was, so this is ei. This e1 and this is e2. This e2, this
is e3, this is e4. If I put currErr as this y axis and prevErr as the x axis.

What we are plotting is e i+1 against e i in the x axis. So let us do that plot x axis is prevErr,
prevErr y axis is currErr okay. Let us plot this okay. So this is how this currErr and the prevErr
are going to change okay. So let us, what we also want to do is not have it in this way, we want
have the logarithm of that.

So that we have some meaningful conclusion. So log (prevErr) and log (currErr) okay. So we
have a log log plot and the slope of this lot is going to be approximately = 2. So we have this as x
is 0.3 and y axis is-1. When x becomes -5. 6, y becomes -13. So -13, -1 is going to be a -12 and
-5, -0.3, -5.6, -0.3 going to be approximately 6. So 12 / 6. So approximately speaking, slope of
that line is a 3.

So we have this particular line as slope of 2 with a slope of 2 okay. So let us type hold on and
that plot will be held let us go on to the other method, clear all, clc and that is fix point iteration.
So added fix point iteration and let us do the same thing err (i). And we want to start with an
initial guess of 2. So that is err (i) and we want to store the errors. Let run this, we are now store
the errors okay.

What I need to do is this yeah, because I did not have err (i) < tol (x). What this was doing was, it
if any err value is greater than tol (x) is not going to stop. So I made this particular change. Again
clear all and let us run this okay. So in tenth iteration it has converged and what we will do is
prevErr. PrevErr is 1: 9, currErr is 2 to 10 and what we want to do is plot loglog prevErr against
currErr.

And let plot this as dash red line, show graph okay. So this is how the error changes when this is
fixed point iteration and this is how the error changes when it is Newton Raphson. So in case of
a fixed point iteration what we get is, a linear line x has fallen or log x has fallen by about 9, y
has fallen so by approximately 9 from -1 to -10, y has gone. And from approximately 0 to -9, x
has gone. So x verses y has a linear slope whereas, the slope is approximately the slope of 2.

233
So that it was mean the linear verses quadratic rate of convergence. (Video Ends: 14:23). When it
is a quadratic rate of convergence error in the 1th step is square of the error in ith step and when
it is linear rate of convergence ith step error is approximately the same order of magnitude as i-
1th step error okay. So that is why partly Newton Raphson method popular because it has a
quadratic rate of convergence.

What we have also seen in today’s lecture, when we gave a very small initial condition or
Newton Raphson, when we started Newton Raphson with an initial condition of 1. Newton
Raphson did not converge but it indeed diverges. So the divergence problem that we have
actually seen for fixed point iteration problem is also there for Newton Raphson method. So one
needs to be very careful before applying method such as Newton Raphson or fixed point
iteration. So with that I come to the end of this lecture, lecture 5.4.

In this lecture and the previous lecture we have covered 2 techniques. The fixed point iteration in
lecture 5.3 and in today’s lecture Newton Raphson method. We ended today’s lecture by doing a
comparison between the convergence rate of Newton Raphson and fix point iteration. The aim of
that was really to understand what it means by a quadratic rate of convergence and why the
quadratic rate of convergence result in a popularity of Newton Raphson method.

As for as this particular lecture series goes, we are primarily interested in solving the problem
using MATLAB and the error analysis for the Newton Raphson method is something that we just
added on in order to give us flavor for how this techniques work. With that I come to the end of
this lecture and I will see you in the next lecture. Thanks, and bye.

234
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #05


Lecture No. #5.5
Non-Linear Algebraic Equations - Using MATLAB function fsolve

Hello and welcome to this course on MATLAB programming for numerical computations. We
are in module 5, covering methods to solve nonlinear algebraic equations. In this lecture, that is
lecture 5.5. I am primarily going to cover, how we can use a MATLAB function fsolve.
Previously, we have used another MATLAB function fzero. fzero was able to solve an equation,
in a single variable. fsolve, on the other hand, is capable of solving, multiple nonlinear algebraic
equations simultaneously.

(Refer Slide Time: 00:49)

Before we move on to the contents of today’s lecture, let us just recap what we have done so far
in module 5. What we have done is, if we have taken an example of f(x) = 2-x+ln (x) = 0 and
solved this single variable equation, to obtain the value of x, that gives the solution. In the first
lecture, we covered a method known as bisection method, we wrote our own code in order to
solve this equation and find a solution.

235
In the second lecture, we showed how we can use the MATLAB function fzero. In the next 2
lectures, we covered Fixed Point Iteration and Newton Raphson method. Both of these, can be
extended to multiple variables. However, in lecture 3 and lecture 4, we covered Fixed Point
iteration and Newton Raphson method for the same example 2-x+ln(x). We used this example to
also demonstrate, some of the properties of the numerical methods that we have covered in the 4
lectures.

What comes next, is the following; in this lecture, we are going to look at, how we can use
MATLAB function fsolve. First, we are going to use fsolve, to solve the same equation 2-x+ln(x)
= 0. In the next lecture, we are going to look at multivariate Newton Raphson method to solve
an, n equations and n unknowns.

So, let us gets started with a content of today’s lecture where, we are going to use the MATLAB
function fsolve to solve the single equation, in single variable 2-x+ln(x) and then we are going to
extend it, to multiple variable example okay.

(Refer Slide Time: 02:36)

So how to use the function fsolve? The syntax of function fsolve is very similar to the function
fzero. So, in this case, our variable x is going to be a vector okay. As we have discussed earlier x
is going to be a column vector. So it has n rows and a single column. So, it is an n/1 vector. The
solution is going to be, xSol okay. And that is also going to be n/1 vector and the function

236
funName is the function file that will calculate a vector valued function f as the function of the
vector x.

Again, because we are interested in solving n equations in n unknowns, f is going to return or the
function fun Name is going to return n values and it will be returned as a column vector okay.

And we need to provide initial guess, and the initial guess has to be of the same dimension as
number of variables. Unlike fzero, we do not have to provide initial guesses that bracket the
solution. We only have to provide, the initial guess, that we feel is the best guess, for the solution
that we are expecting okay.

(Refer Slide Time: 03:59)

So, let us look at an example, we will use, fsolve in order to solve the nonlinear equation 2-
x+ln(x) = 0. (Video Starts: 04:11) Let me go to MATLAB and so let us look at the function that
we have already created. We had already created the function called fun4bisec okay. Where, the
function value, it calculated the function value, 2-x+ln(x) okay. And that is what we want to
solve so. (Video Ends: 04:39)

As I had said here, our funName is a function that calculates f(x) okay. fsolve is the MATLAB
function that we will solve f(x) = 0. It needs, somehow a function that calculates for it the value
f(x) okay. (Video Starts: 05:02) And that is what we are going to provide over here. And we are

237
not going to modify that function at all okay. And we are just going to use the syntax, as shown
over here.

f xSol=or the solution or the = fsolve okay @ x that is the variable, that we want to solve for,
name of function 4bisec (x) fsolve @ x space fun4bisec in brackets x. That is how we call this
particular function and pass it onto fsolve and we need an initial guess, so let us say the initial
guess in this case, let us take the initial guess as 1 okay. And let us just press enter.

When we give an initial guess as x= 1 okay, we land into a problem. (Video Ends: 06:00). The
reason why, we get this problem is, let us look at this particular function. Now this function has a
maxima at x = 1. So, for example, if you differentiate this, as we had seen in the earlier lecture,
we get this as -1+1 by x okay. - 1+1 by x at x = 1 is going to be equal to 0.

Which means that particular guy is either going to be a maxima or a minima okay. So, if now, if
we take, d square we are going to get-1 by x square which is a negative value with at x=1 which
means that it is a maxima. So, this function, as we had seen earlier, goes through maxima at
value of 1 okay. (Video Starts: 06:55) So, unlike fzero, where we needed to give solutions, that
give an initial guess, that brackets the solution here, we need to provide a reasonable value of the
initial guess.

The problem with providing the value of initial guess is equal to 1 is that the method such as
Newton Raphson or fsolve, cannot proceed because the slow calculated at x=1 is equal to 0 okay.
So that is one of the problems, that both fsolve as well as Newton Raphson method faces. You
cannot solve, with start, with an initial guess that is a local minima or a local maxima. What we
are going to see next is if we start with an initial guess which is to the left of that maximum we
are going to reach the first solution, if we start with an initial guess to the right of the maxima,
we are going to reach the second solution okay.

So let us take an initial guess to the left and that initial guess for example is 0.5 okay. And let us
solve this. When we solve this, we get the result of MATLAB tells, that the equation is solved
and the solution xSol = 0.1586. Recall, that that was the solution, the left solution that we
obtained in the previous lecture okay. Now let us take an initial guess of x=2 and let us see what
happens. When we take an initial guess of x=2, the xSol ends up becoming 3.1462. So we get
now to the second solution.

238
Finally, a third set of initial guesses, are going to be to the right of the second solution. So, let us
take that value as 5 and see whether the fsol converges or not. When we take the method 5, again
sorry, we take the initial guess as 5 again, we get the result that the equation is solved and the
solution reached is the second solution 3 .1462 okay. So, with that we come to the end of the first
example, which was a single variable example okay. (Video Ends: 09:07)

The single variable example, with that we used in fsolve. We saw that, we were able to use the
exact same subroutine or sorry, the exact same function, as we use in f0 in order to return f(x).
Once, that particular function was passed onto fsolve, fsolve was able to solve the nonlinear
algebraic equation, for most of the initial guesses, but it was not always able to solve that
equation okay. One of the cases, when it is unable to solve this equation is, if we start at or if we
start close to a local minima or a local maxima as we saw, when we started with x0 = 1 okay.

(Refer Slide Time: 09:55)

Next, we are going to cover multivariable examples okay. And the example that I am going to
cover is a 3-dimensional system. So, we have our solution variable x in the f(x). Our x is going to
be a 3-dimensional variable and f is going to be a 3-dimensional vector valued function. For this,
we are going, to take an example of, what is known as the Lorenz system.

The Lorenz system, was a first example that demonstrated chaos and it is an extremely
interesting example, for people to learn and if you want to look at or try to get some idea about

239
what Lorenz equation is, you can go to this Wikipedia article over here. And it has a pretty nice
description of Lorenz equation is? You can go to this Wikipedia article over here, we are going to
get these equations, x – y = 0; 2x – xz – y = 0; xy – 3z = 0. So, let’s go and try to solve this using
MATLAB.

(Refer Slide Time: 10:46)

LorenzSystem okay. I will create a new file function fval= lorenzSystem X okay.

So, we have used, capital X as the inlet input variable over here. Let us define our, define three
variables x=x1, y=x2, z=x3, define f(x) okay. So fval (1, 1) equal to, fval (2, 1) equal to, fval (3 ,
1) equal to again. We had said multiple times earlier, that we are interested in ensuring that, fval
is going to be a column vector. That means n number of rows and a single column okay. So we
have x-y as the first equation. 2x-xz-y, 2 * x - x * z, -y that is the second f and third is xy-3z is at
x * y, -3 * z, okay. Let us save this okay. And let us go to our command prompt okay.

So, now we need an initial guess. So, let us take the initial guess as, 1, 1, 1 okay. And we will use
xsol = fsolve @ let us say x lorenzSystem x, x not that is the initial guess, and that should give us
the solution in xsol. I will press enter, equation is solved okay. And the solution is 1.7321, 1.7321
and 1. If you take these equations and try to solve these, you will find that Lorenz equation or

240
Lorenz system has 3 solutions. 1 solution is root 3, root 3 1, another solution is-root 3-root 31
and the third solution is 000 or the origin okay.

So, let us try, to get the other solution also. x0 instead of saying is 111. Let us say x0 was -1, -1, 1
and let us now try to find xsol. And when we do that, we get the other solution -root 3, -root 3,
1.0 okay. So let us try another initial guess. And in this case, let us say, the initial guess was say
-1-1 0. Let us try with that okay. And xsol. When we try okay, we get the solution, which is
approximately equal to 0.

Keep in mind, that we are not getting the solution exactly 0. Why because, we have a stopping
criteria, and the stopping criteria in fsol is, that the tolerance value for x is 10 ^ -6 okay. And that
you can, let us say help fsolve okay.

There is a command called optimoptions. And optimoptions is the command that you can use, in
order to change the optimization properties, in which case you will need to give the command for
solving fsolve as shown over here. Where option comes from the vector, options come from, this
optimoptions okay.

So, let us look at optimoptions. Let us say fsolve okay. When we type this okay, we get, what are
the default options that fsolve uses. The default option is, that the function tolerance. That we
specify in order to say f(x) = 0 is 10 ^ -6. The tolerance on x that is our solution variable is also
10 ^ -6.

So as long as our solution, defers from the true solution, by less than 10 ^ -6, our method is going
to converge, going to be set to have converge. In this case, the true solution was 0 okay. And
when the solution, was give comes out to be 10 ^ -7 multiplied by .3, .3, .5. That means, we are
closer to 0, than the variable tolerance tolx value okay. (Video Ends: 17:45)

So, with that we come to the end of this lecture. What we covered in this lecture were 2 things,
single variable fsolve and a multivariable fsolve. We took the example of Lorenz equations, for
the multivariable fsolve okay. With that I come to the end of this lecture, and I will see you in the
next lecture. Thank you.

241
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #05


Lecture No. #5.6
Non-Linear Algebraic Equations - Multivariable Newton Raphson

Hello and welcome to MATLAB programming for numerical computations. We are in module 5
where, we are covering methods to solve nonlinear algebraic equations. This is the last lecture of
this module lecture 5.6. In this lecture, we are going to consider multivariable Newton Raphson
method. In previous lecture 5.5, we have covered fsolve, which is a MATLAB function in order
to solve, multiple nonlinear equations in multiple unknowns.

So, when we have a system of n nonlinear algebraic equations in, n unknowns, we can use fsolve
in order to solve that. In lecture 5.4, we had covered a single variable Newton Raphson method.
In today’s lecture, we are going to extend the single variable Newton Raphson method covered in
lecture 5.4 to the multivariable case and we are going to solve the Lorenz equation problem that
we saw in the previous lecture, lecture 5.5 okay.

(Refer Slide Time: 01:18)

242
So, let us look at the steady state Lorenz equation. The steady state Lorenz equation, is of the
form x-y = 0, 2x-xz-y = 0 and xy-3z = 0 okay. Now in computation, of Newton Raphson’s
method, for a multivariable case, we need to compute a Jacobian. The Jacobian as you might be
aware is computed in the following form okay.

The 1, 1 element of the Jacobian is going to be partial f1 by partial x, df1 / dx. So df1 is f1 is this
guy okay, so when we take a partial derivate, with respect to x, we get that value equal to 1 okay.
The next guy, is going to be partial of f1 with respect to y. So, that is going to be -1 and third is
going to be, df1 / dx. Now let us look at the second row.

The second row is going to be, the first element of the second row is going to be, df2 by dx in
that is going to be 2-z. The second guy, is going to be df2 / dy which is -1, the third guy is going
to be, df2 / dz, which is going to be -x.

So, d / dz, of this term and this term is going to disappear, because there is no z dependence over
there. And only this term is going to remain and because we are taking partial derivatives with
respect to z, x is held constant and we get -x * 1, which is -x as the third guy. Likewise, if we
look at the last equation df3 / dx is going to be y, df3 / dy is going to be x and df3 by dz is going
to be -3 okay.

Recall that Newton Raphson’s method in a single variable. Let us insert equation over here,
Newton Raphson method in single variable, was written as xi+ 1 okay. So, the Newton Raphson
method in single variable was xi+ 1 = xi - fi / ji okay. Where, j in that case was nothing but f
dash. In case of multivariable case, we are going to just convert all of these into vectors okay.
And because we cannot divide by j, what we need to do instead is take an inverse okay.

So, this is going to be, ji inverse of that multiplied by fi. And this is our multivariable, Newton
Raphson’s okay. So this is for multivariable Newton Raphson. We want to solve where, fxyz is
nothing but this guy and Jacobi j is nothing but this guy okay. So let us head on over to
MATLAB and try to solve this problem, using multivariable Newton Raphson okay.

(Video Starts: 05:59) So, let us look at, what we had written earlier, a function called
lorenzSystem which was used with fsolve .Let us save that as a different file name. Let us call
that lorenzsysnr. nr standing for Newton Raphson okay. And we need to use the same function

243
name, as the file name. So, we will just replace the, function name with the file name. This
particular file needs to calculate both fvalue as well as the Jacobian. So, let us call the Jacobian,
as jac and let us define Jacobian okay.

And let us look at what we had over here. 1, -1 and 0. So j is going to be 1,-1, 0, the second row
is 2-z, -1, -x, and third guy is y, x, -3 okay. And this is all that we need at this stage okay. So keep
in mind that, we were able to write it in exactly same fashion, as we had returned it over here,
mainly because we were we had written the variables x, y and z = x1, x2 and x3 right in the
beginning.

Sometimes, we will not be able to do that, in which case we will have to write this as 2-x3-1.
And then this is going to be -x1. Likewise, y is nothing but x2 and we can write this as x2 and
this is going to be x1 okay.

Either ways is going to be exactly fine, they are both exactly the same thing okay. You need to be
careful, about that the variables that you have used. For simplicity, I will just undo my edits that I
have done a few minutes back okay. And we should be good to go okay. One thing you will
notice, I will just actually try to do this right. Now if I call this, and are using our variable some x
okay.

So, let us call this, Lorenz. Let us call f, j = lorenzSysnr say 1, 1, 1. We are going to get some
problem, and the problem, is the output arguments jac is not assigned. Now let us see, why we
get that particular error. We get this error because, instead of calling the Jacobian jac, I have
called it capital j over here. So I just need to change this and there will not be an error okay.

Other thing to keep in mind is that MATLAB. I will just undo this and show you now.
MATLAB, is fairly helpful when it comes to pointing out some errors or warnings. So if you see
over here, what was the green button has changed now to orange button over here. And it says
that, now if you click over here, it will go to the location where it is giving you a warning. If you
take your mouse and hover at that point, you will be able to see the warning.

The warning says the function return value jac might be unset. What that means, is that
MATLAB is trying to tell you that, in this function lorenzSysnr. I am expecting jac as my output
variable. However, in the entire function, I have not specified what that jac is. So I should go

244
here and change my j to jac. When I do that you notice that the orange bullet has turned into
green which says that no warnings are found okay. So, let us save this okay.

Now let us say, we want to go to Newton Raphson’s and we want to do a multivariable Newton
Raphson. So let us go over here and save as multivariablenr for Newton Raphson okay. So let us
say, solve not yes, solve a sequence of nonlinear equations using nr, Newton Raphson okay.

Now, our initial conditions were capital X = 1, 1, 1 okay. Keep in mind, what I am doing over
here is, I have taken a previous solve problem and I am editing that, rather than writing this
entire multivariable. Newton Raphson from scratch, maximum iteration was 50 and tolx to keep
the same as, what we saw in fsolve in the previous lecture. We will take the tolx as 1e-6 okay.

Computation using, not fixed point iteration, Newton Raphson okay. Our initial X, we should
call this X0, our initial X is X0, and our Xold is also X0, so okay. Now over here f, j is =
lorenzSysnr (x) okay. So previously what we had was, we calculated f using 2- x+ log(x) instead
of that instead of our fx equal being equal 2-x+ log(x). Our f(x) is x-y and so on and so forth.
Our f(x) was x-y, 2x-xz-y and xy-3z. So, we replaced this particular guy with this lorenzSysnr
and lorenzSysnr in addition to giving f also gives Jacobian okay.

Our new value of X is going to be, our old value of X-jacobian inverse. So inv j multiplied by f
okay. Error is going to be X-Xold okay and error, we want all the rules ith column should be this
okay. And our capital xold = x and if error is less than, if all the errors, are less than tolerance x,
then end this computation okay. So, that is what we have, let us clear all and okay. Let us solve
this multivariable nr okay.

Solve multivariable nr and we get the solution x as 1 .7321, 1. 7321 and 1.0 and we have got this
x in only 5 iterations okay. Now let us change, that initial guess to -1, -1 and 1 and let see what
happens when we run this. So, when we run this, we again got the solution and solution again we
reached in 5 iterations okay. And the solution is -root 3, -root 3 and 1.0.

Let us change this to -1 -1 and 0. And let us see with that what we get our solution and we run
this okay. We get our x as 10 ^ -21. So that is really close to 0. This is the overall code that we
have used in order to solve the system of 3 nonlinear equations and 3 unknowns using
multivariable Newton Raphson’s.

245
We started off with the single variable, with Newton Raphson code and the changes that we
made were primarily at 2 locations. The first change, that we made were, instead of calculating
x= x sorry, 2-x+ ln(x) we used our function lorenzSysnr in order to calculate our f and our
Jacobian. And instead of using x = x - f / df instead of that, inverted Jacobian and pre- multiplied
our function f, with that Jacobian okay. That was the other change that we made.

The third change that we made was that error in any ith iteration is no longer a single value, but
it is a column vector okay. So, we found that column vector, as the difference between x and xold
and checked whether all the 3 elements of that column vector, whether or not they are less than
that x tolerance. And if they were, then we stopped the iteration, if not we kept continuing the
iteration, till the stopping criteria was met okay. (Video Ends: 16:34)

So, with that we basically come to the end of, not only this lecture but to end of module number
5 okay. And thank you, for listening. In the next module, number 6, we are going to cover
regression, interpolation and curve fitting. Thank you and see you next week.

246
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #06


Lecture No. 6.1
Regression and Interpolation – Introduction

Hello and welcome to MATLAB programming for numerical computations. We are in week
number 6, and this week we are going to cover, regression and interpolation. Regression is
something that is familiar to you, probably as curve fitting. So, for example, fitting, straight line
to several data points, interpolation was probably familiar to you as, simply joining the dots. So,
we are going to cover regression and interpolation in module 6 okay.

(Refer Slide Time: 00:47)

Let us first take an example. Let us say we have been given the following data. The x is given
over here and y is given alongside with x. And this, when we plot in x versus y curve, we are
going to get this as these data points on the 2-dimensional space okay.

What regression, tells us is, how do we obtain a straight line. For example, that best fits the data.
So, this is, the straight line of the form a0 + a1x = y. And we want to find the values of a0 and a1.

247
So, that this particular line is, the best fit line for all these data points. So, what we are going to
do in regression is, to minimize the error between the data points on this line and the
corresponding data point that is shown over here.

(Refer Slide Time: 01:45)

Now what is interpolation? Interpolation, is kind of joining the dots which basically mean that
we want to find a curve that passes through all the data points. The reason why we would use
interpolation is for example fill in the missing data. So, let us say this is the particular set of data,
and we want something at 2.0. See there is no data available at x = 2.0. So, in that particular case,
we are going to use interpolation.

As again interpolation, when we are using to use regression is, when we need some kind of a
functional form as y = f (x) and we want to find that particular functional form or specifically we
want to find the parameters, that best satisfy that functional form that is when we use regression
okay.

(Refer Slide Time: 02:35)

248
So, in regression, we are interested in fitting a chosen function to the data. The function for
example, in this case, might end of being y = 0.45+ 0.47 x. In case of interpolation, given a finite
amount of data, we are interested in obtaining new data points, within this range, that is new data
points within the range of 0.8 to 4.9 okay. So, that is the intend of interpolation and regression.

So, for example if you want to query at x=2.0, what is the value of y? What is the probable value
of y? If this is the interpolant that we are going to use, than the value of y, is going to be 1.87
okay. So, what are we going to do in this module?

(Refer Slide Time: 03:21)

249
In module 6, the first lecture, that is this lecture, is primarily a demonstration lecture. I am going
to, cover linear regression and Newton’s interpolation, to just give ourselves some idea of how
the regression is going to work okay. We are not going to use, this particular examples to see how
interpolation and regression works.

We are not going to use these examples in order to code something in MATLAB. I will quickly
demonstrate a MATLAB code for you to show you, what regression means, what interpolation
means and the difference between the 2 and primarily for the sake of completeness okay.

In the next lecture, we are going to cover, curve fitting with multiple parameters and we will take
up one MATLAB function called lsq curve fit in order to solve this problem as well. So we will
take a problem, and we will solve it in 2 ways. One is using a method that we would learn in the
next lecture, as well as using a MATLAB based algorithm.

In lecture 6.3, we are going to use these concepts for parameter estimation. We will take the
example of a reaction rate, something that you would have done in your chemistry course in
eleventh and twelfth grade. We are going to take that forward in lecture 6.3 using MATLAB. So
6.2 and 6.3 will cover examples in linear regression.

In lecture 6.4, we are going to cover interpolation. In case of interpolation, we are not going to
build our own codes to do interpolation. Instead we will use two powerful functions in MATLAB
spline and pchip in order to do interpolation. We will cover what spline and pchip does in lecture
6.4 as well as methods to use them okay.

(Refer Slide Time: 05:43)

250
So, let us go over to the demo part, of for linear regression as well as Newton’s interpolation and
we will take this particular set of data, in order to demonstrate regression and interpolation. So in
case, of linear least squares regression, the question that we want to ask is, how to fit a straight
line y = a0+ a1x to the given data. In the computational techniques lectures, in module 5, lecture
number 1 and lecture number 2, we have covered methods, to do linear regression and the link
for which is given over here.

So, if you want to understand the derivation, for how to get this particular equation. You can go
over to this link and you can look at the derivation for getting this equation. We are going to get
2 equations and 2 unknowns. This is the first equation, this is the second equation. We put these
equations together. We are going to get this in the form a phi = b. So, our phi vector, which is a0
and a1, the two parameters is a0 and a1 is just going to be a inverse multiple by b okay.

(Refer Slide Time: 07:01)

251
In case of, Newton’s divided difference formula which was covered in module 5, lecture 4. In
that case, we built, what is known as divided difference tables. So, divided difference table, in
the first divided difference, is based on difference between y2-y1 / x2-x1, y3-y2 / x3-x2, y4-y3 /
x4-x3 so on and so forth. The next set of divided difference is based on the d’s. So, that means d
square is nothing but d2-d1 / x3-x1, d3-d2 / x4-x2 so on and so forth and so on we go until d ^ n.
This is how we built the overall divided difference table.

And the top row of the divided difference table forms the divided difference coefficients. Again,
and this is for demonstration purposes only, to show the things that were covered in the
computational techniques course, we are kind of go showing parallels using MATLAB. And that
is the intent of this demonstration lecture. So let us go to MATLAB okay.

(Video Starts: 08:20) And let us look at the example. Okay, unlike the previous lectures, today
what I am going to do is, I will just show you, the final code and I will just go over some of the
interesting aspects of the code, I am not going to discuss the code in detail, since this is a demo
lecture. So, let us look at look at the various lines of code.

This is the x and y data points n is nothing but the length of the vector x. This is the part, where
we compute linear regression and this is where we compute interpolation, using divided
difference. Our matrix a was formed as shown over here. n we have, n summation xi, summation

252
xi. Summation xi squared as the 4 guys. n sum(x) sum (x) and sum(x ).* x which is summation
of xi squared.

Likewise, we form the b matrix that is shown over here okay. And then phi, is going to be
nothing but inverse of a * b. We are plotting the original data points and we are plotting the value
a0 + a1x. So a0, that is like phi 1+ phi 2 * x for this value of x which will be a red straight line
okay.

So, that is what we will do in a linear regression part. And the divided difference part, we have
created another function called ndd, to calculate the interpolant using Newton’s divided
difference. So, we will calculate the interpolated values, in the entire range 0. 8: 4 .9 in steps of 0
.01 so, that we can see the entire curve how the interpolated curve looks like. So let me now run
the code okay.

So, the blue dots that you see over here, were the original data points, the red line is the
interpolated line, that we have plotted. So this is the interpolated line, that we have plotted in red
and sorry, that is the regression line. The best curve fit line that we have plotted in red. And in
black, we are getting the interpolant okay. And the interpolant that we obtained is using
Newton’s divided difference formula.

So this is the straight line, these are the data points and this is the interpolated curve okay. The
thing that you see over here, because these 3 points are located fairly close to each other in the y
direction. That is why we see a very funny way, in which this interpolated curve looks like,
instead of the interpolated curve going smoothly in this way okay.

That is one of the things that happens in interpolation, we have taken an extreme example over
here, you will not get this type of behavior for most of the interpolation cases okay. Anyway,
now to finish up this lecture, let us look at the Newton’s divided difference formula okay.

Newton’s divided difference, we create a divided difference table, first by initializing it as a 0, a


vector of n by n array of zeroes okay. And at each step, we are going to calculate the d column.
And the d column, is going to be nothing but, the difference of the previous column, divided by
xi-x1, xi + 1-x2 so on and so forth okay.

253
If we go over here and we look at the divided difference table each of these guy is computed
from the previous column of the divided difference table. And that is what the MATLAB code
does. And finally, the coefficient is going to be, nothing but the first row of the divided difference
table and we will use those coefficients, to do the interpolation okay. So, this is the, part where
we are doing regression, this is the part, where we are using Newton’s divided difference formula
in order to do interpolation okay. (Video Ends: 12:47)

To recap, what we talked about in this lecture was, was this. We talked about linear regression
which is to fit a straight line or a curve to the given set of data, the example that we took was of
fitting a straight line. But regression, does not necessarily mean, only to fit a straight line. That is
something that we are going to see in the next lecture. Newton’s interpolation, we use in order to
find the value of y, at intermediate points of x okay.

So, these are the things that we demonstrated in today‘s lecture. And in the next 3 lectures we are
going to spend 2 lectures covering regression and parameter estimation. And the last lecture
covering interpolation okay. So, with that I come to the end of this lecture and I will see you in
the next lecture. Thank you and bye.

254
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #06


Lecture No. #6.2
Regression and Interpolation – Linear least squares regression
Hello and welcome to MATLAB programming for numerical computations. We are in module 6.
In this module we are covering regression and interpolation. In the first lecture of this module we
introduced ourselves to regression and interpolation. In this lecture we are going to start with
regression. Specifically, we are going to cover linear regression or linear least squares regression.
(Refer Slide Time: 00:40)

In the previous lecture we tried to fit data y equal to some function of the independent variable x.
In that case the function that we were looking for was y = a0+a1x. Where, we were given data of
x1, y1, x2, y2 and so on up to xn, yn. And the parameters that we wanted to fit were is a0 and a1.
In that case we got our a0 and a1 as 2 unknowns which were expressed in terms of 2 equations.

2 equations in 2 unknowns so then solved in order to obtain a0 and a1. Now the matrix method is
an even more powerful method of doing linear regression. And in matrix method we are not just
restricted to 2 variables. We can extend it to multiple variables. In this case for example we have
4 parameters a0, a1, a2 and a3 okay. So when we have data in terms of x1, u1, and w1 which are
the independent variables and y1 which is the dependent variable so this is the first data point.

255
Second data point is x2, u2, w2 and y2 and so on up to the nth data point xn, un and wn and yn.
In this case we want to have a model fit of the form y=a0+a1x+a2u+a3w. In that case we form
the matrix which is shown over here okay. The model that we see is 1 * a0 + x1 * a1+ u1 * a2 +
w1 * a3.

So the first row multiplied by our phi vector is nothing but the right hand side of the equation
evaluated at the first data point. Now let us look at the second row. Second row is nothing but 1 *
a0 + x2 * a1+ u2 * a2 + w2 * a3 okay. So the second row is nothing but the a0+a1x+a2u+a3w
evaluated at the second data point and so on and so forth okay.

This we will have as n equations in 4 unknowns. For example we can have 15 data points to
which we want to fit the 4 parameters. So in this case we might have n let us say if n was 15, we
will have 15 rows and 4 columns only okay. So this does not have a unique solution okay. In that
case we are interested in finding least square solution.

We are not going to cover of course the way of deriving the least square solution that was
covered in computational techniques course module 5 part 2, the link for which is given over
here. We had also shown in this computational techniques course that this least square solution
for 2 variables x and y gives the exact same solution as the 1 we saw in lecture 6.1 the previous
lecture okay.

So in this case what we are going to do is if we have just 2 variables, so that means y as a
function of x if I want to say y=a0+a1x, the first column is going to be 1, 1, 1, 1, 1 and so on, the
second column is going to be the data x1 x2 up to xn that will form our matrix x. Our matrix y is
going to be nothing but y1, y2 up to yn and phi is going to be (x` * x)-1 * x’*y.

Let us say we wanted to fit a model of the form y=a1x+a2u+a3w, we did not have the a0 okay.
Then this particular column is going to go out and this particular guy is also going to go out, so
without this column and without a0 we will have x1 a1 u1 a2 w1 a3 = y1.So if we did not have

256
a0 we would not have this column if we did not have u and w we would not have these 2
columns.

If we had a0+a1x+a2 u we did not have w we would not be having this column we would not be
having a3 either okay. So this is very easy to think of how to form this matrix x, how to form this
vector y okay. And that makes it a very powerful tool in order to do linear least squares.
(Refer Slide Time: 05:48)

What we are going to do now is to take the same example that we did in lecture 6.1 and solve it
using the matrix method of multiple of linear least squares in multiple parameter. We will do it
for 2 parameters for now and we will see that this method is easily extendable to multiple
parameters. Let us go to MATLAB and try to do that okay.

(Video Starts: 06:10) This was the code that we used in the previous lecture. Let us just save it,
multiRegression, we will just call that okay. And we will retain the guys in the top, we will delete
this and we will delete the interpolation part okay. Now let us calculate the linear regression and
plot it. Our matrix x is going to be nothing but 1, 1, 1, 1 is going to be the first column.

We are going to have n elements in the first column. So we need to get this by using the
command ones N number of rows and single column that is going to be first column. Our second
column is nothing but our data x. So we will just put this as x over here and that is our capital,

257
our capital y is nothing but was small y okay. This is was what our capital y is y1, y2 up to yn
which is nothing but the same as the small y.

Our phi was nothing but inverse of let us go back and look at the equation x ’ * x, and this is very
important and it is not x, x transposed but it is x’ * x, (x` * x)-1 * x’ sorry, multiplied by x` * y. We
have cleared everything and now let us run this multiRegression. Okay and we see the exact
same results as we had seen in lecture 6.1. We see those 6 data points and this is the best fit line.
(Video Ends: 08:16)

Okay let us go back to power point okay. Okay, so in this lecture what we have done is we took
the same example as lecture 6.1 but used a more powerful matrix based technique for doing
linear regression. In the next lecture we are going to use this method and extend it to a practical
problem for finding out parameters for a real case study. That case study will be taken from our
11th grade, example of finding the rate of reaction for a given reaction something that we did in
chemistry in our eleventh and twelfth grade okay.
(Refer Slide Time: 08:53)

(Refer Slide Time: 08:56)

258
So this was the example that we used using our linear least squares. The other way of doing this
is using MATLAB command lsqcurvefit. lsqcurvefit requires the optimization tool box okay. So
let us go MATLAB and do this okay. (Video Starts: 09:13) So function let us say fval=
myLinExample phi, xData, yData okay. What is my lsqcurvefit supposed to or the function
supposed to return function is going to return just ymodel=f (x, phi).

It is not going to return the yData and it is not going to return the errors or things like that it is
just going to return the function. So I do not even need yData over here. It is just xData. That it is
going to return. So what this is going to return is the function form f (x) for all the xData. And
what was our f(x)? Our f(x) was nothing but a0+a1x. So that is what this guy is going to return.

So fval is going to be phi (1) +phi (2) * xData. Now phi (2) is a scalar, phi 1 is a scalar. So our
scalar phi (2) is going to multiply all of the data. And our phi (1) is also going to add to each and
every element of the phi (2) * xData. So this is indeed going to be a0+a1*x. So let us save this as
myLinExample. Let us go to multilinear regression okay.

And let us say phi = myLinExample okay. Oops! Sorry, not myLinExample, it should be
lsqcurvefit at x, xData, myLinExample(x, xData), p0, xData and yData. So let us say the initial
values of parameters were say 1 and 1, xData, yData okay .That is all I need and let us say using
lsqcurvefit. Let us call this as phi (1) instead of phi.

259
And let me say hold on over here okay. And plot, what I want to plot is basically just this guy
and I will plot this as. Let us say dotted lines of plot 0.5. 0.5 phi (1) + phi (2) multiplied by the
same guy and dotted. Let us make this as a green color dotted line okay. Let us save this and its
clear all, close all and let us run multi linear regression okay.

We have a problem over here okay. Our xData was not xData was just x. So let me change that
yData was just y. So let me just change that also okay. See what had happened okay. I had just
finally gone to our MATLAB where, I had given the syntax for the overall. You know this
solution that we wanted to obtain and I had simply copied and pasted over that.

Now xData and yData is something that is not defined in this code. At all the xData is covered in
the vector x, the yData is covered in the vector y. So it has to be the vec, the vectors that were
previously defined we do not have to blindly give the same vector name okay. That is what it is
actually complaining. It is complaining that this xData is not defined. So now let us run multi
linear regression and hopefully this should work okay.

So it says the local minimum found and let us look at the result okay. So this is the red line that
was there as before and on top of that red line there is that green line. I do not know, if you can
see let me line with, let me just increases to 3.0. So, that you can see this. Yeah so now you can
see the green line which is on green dotted line which is on top of that red dotted line may be I
will just make this line a little bit thinner. Yeah so you can see the red and the green dotted okay.

So as you can see this that the 2 lines are exactly on top of each other that is the red line was,
nothing but our matrix based method to find the linear regression. And the green line was our
MATLAB lsqcurvefit based method to find the linear regression okay. I am sorry, I actually that
is not what we did over here okay.

Let me edit this and now let me run okay. Yeah so that is again what we see over here. Let us
convert this into instead of a red color, let us change it to a black color line or rather instead of

260
green color will convert it to a black color line and mark 2 okay. So now we can see. So that is
the black dotted line exactly falling on top of the red solid line.

The red solid line was for the straight line that was computed using our matrix method. And this
one was for the straight line that was computed using the MATLAB based lsqcurvefit. Let us see
the values of phi. phi is this let us see the values of phi1 which is also exactly the same. (Video
Ends: 16:37) So what we have done now in this lecture is, to use MATLAB method and
lsqcurvefit as well as write our own multi linear regression code using the matrix method in
order to find the value of phi which best fits the model of the form y = a0+a1x and so on.

With that I come to the end of this lecture. In the next lecture we will take up an example of the
rate of reaction. And we will show how to use, how to convert that rate of reaction problem into
a linear regression problem and next we will use the matrix method in order to solve that
problem okay. So I will see you in lecture 6.3 thank you bye.

261
Matlab Programming for Numerical Computation
Dr. Niket Kaisare
Department Of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #06


Lecture No. #6.3
Regression and Interpolation – Functional and nonlinear regression

Hello and welcome to MATLAB programming for numerical computations. We are in module 6.
In this module we have been covering regression and interpolation. In the first lecture of this
module we covered introduction to regression and interpolation. In the previous lecture we
covered linear least squares regression and took up an example and solved it using a matrix
method for linear least square.

In this lecture we are going to take a physically relevant example and show how we can do linear
and nonlinear regression. We will take example of rate of reaction, the Arrhenius form that we
have studied in our eleventh and twelfth grades. We will take an example, for the Arrhenius form
and show we can use linear or nonlinear regression in order to obtain the parameters for the rate
of reaction.
(Refer Slide Time: 01:07)

So the Arrhenius model for reaction rate is given by rate r = k0 * e ^ -e /rt. Where, e is the
activation energy and r is the gas constant multiplied by concentration c to the power n. So k0 * e

262
^ -e / rt c ^ n. Now we will solve this problem in 2 different ways. First is using linear least
squares regression. Now this is clearly not a linear form of equation. You see, you have your k0,
you have your the other parameters is e / r and the third parameter is n.

They are not related to each other in a linear fashion. However if we take a logarithm of this, we
will get logarithm of r = logarithm of k0 + negative e/r * 1 / T + n * ln(c). So if we were to write
1 / T = x, we were to write ln(c) = u. We will get this in the form y=a 0+a1 * x+a2 * u. where,
our a0 is nothing but log of k0, our a1 is nothing but-e / r and our a2 is nothing but our n okay.

So this has a form which is linear in the parameters that we are interested in finding. Note that
when we talked about linear regression it or nonlinear regression, we mean that it is linear in the
parameters or nonlinear in the parameters respectively. It does not matter whether it is linear or
nonlinear in the measured quantities. That is in this case temperature; concentration and rate of
reactions are measured.

It does not matter whether it is linear or nonlinear in these quantities. What is important that is
that, it should be linear in a0, a1, a2 and so on. It is indeed linear in a0, a1, a2. So first we will
solve this particular problem using linear least squares and then we will solve it using nonlinear
least squares using the MATLAB function lsqnonlin. lsqnonlin is a function that is available in
MATLAB’s optimization toolbox.
(Refer Slide Time: 03:52)

263
So you will have an access to that function only if you have access to the optimization toolbox.
So let us go ahead and solve this problem in 2 ways for the following data. So the data is
obtained at 5 different temperatures 400, 450, 500,550 and 600 Kelvin’s and 4 different
concentration 1 mole per liter, 2 moles per liter, 3 moles per liter and 4 moles per liter and these
is the rate of reaction given in moles per liter per second.

If we take logarithm of the rate, we are going to get the expression of the form y=a 0+a1 x+a2 u
okay. So y is log (r), x is 1 / t and u is log (c) and that is what we will use in order to do the linear
regression. Let us go to MATLAB and do that okay.

(Video Starts: 04:37) So what I have done already is, written down a snippet of code for
obtaining the data in concentration and temperature. So let us see what data that we have. So let
us, when we run this, I have given a command to display xData and yData and let us go ahead
and do that okay. So this is basically our independent variables concentration and independent
variable temperature and our dependent variable rate of reaction.

Let us look at the table that we have had so far. 1 moles per liter and 400 kelvins it was 1. That is
48 for 2 moles per liter, 400 Kelvin’s. It was 2.35 for 1 and 400, 1.48, 2 and 2 and 400, 2.35 and
3 and 400, 3.28 for 3 and 400 it is 3.28 so on and so forth. So if so the temperatures are given as

264
xData2 and concentrations are given as xData1, the first column is concentrations, the second
column is temperatures and yData is nothing but our rate of reactions okay.

So what we want to do is, get our x is nothing but 1/ T. Our u is nothing but log (c). So let us go
ahead and do that in MATLAB first okay. So let me delete, sorry let me delete this line, as the
instructions. Delete this line before having the code. The reason why I had this line over here is,
to display to you what the data looks like okay.

So our concentration is going to be, yeah our concentration is xData column 1, our temperature is
xData column 2. What we first need to do, we want to call our x as 1 / T. So x=1./ T and t is
xData. All the row’s first, sorry second column okay. That is our x. Our small u is small, u is the
first column of xData. Logarithm of that u is logarithm(C).

C is the first column of xData. So u is going to be logarithm of our concentrations. Concentration


is nothing but xData all the row’s first column okay. So that is our x and u. Our y is nothing but
logarithm of rate. Rate is n yData and our y is nothing but logarithm of rate. So we will put that
in log (yData) okay.

So let us run up to the stage and see whether we have xu and y correctly or if there is some error,
MATLAB will throw us an error okay. So there is no error, so, xu and y has probably been
obtained correctly. Let us look at our x, x is 1/temperature. So we should be getting 1/400, 1/450
and so on. So let us type x and see what the values are. So 1/400 is 0.0025, 1/500 is 0.0020. So
this looks like its correct okay.

So we have our x, we have our u, we have our y. So let us construct our big matrices. Our x was
nothing but ones and that is size of n rows and single column. Then we had our x followed by
our u okay. This should give an error. I will come to that in a minute but let us say why nothing
but our y okay was. So let us run this and see whether we get an error or not. Yes we get an error
and the reason why we got an error was because our variable n was not defined okay.

265
So again, I am doing this in a step by step manner. So, that this becomes fairly easy for us to
debug. So every time I write a few lines, I will go back and check. You do not need to do that,
you can write the entire code and then check it. The reason why I am doing this is basically on a
video like this, it becomes easier for me to go and show where the errors can possibly be rather
than typing out the entire code okay.

So it is up to you how you want to do this. Whether you want to do this in a single shot writing
the entire code and then debugging it or writing few lines of code running it and then check
whether the results are okay or not and debug whenever you get an error. So anyway the error
was that n is not defined. So let us define our variable n over here.

And n=length(y). So n is the number of rows in our vector y okay. So this is our x, this is our y
and our phi is going to be nothing but (x` * x)-1 * x’*y okay. (Video Ends: 10: 53)

That was how the way we do linear least squares x transpose x inverse x transpose y. So inv of x`
multiplied x. Our k0 is exp of phi (0), sorry phi (1), a1, a0 is phi (1) right okay. So this is our phi
(1) is going to be nothing but logarithm (k0). So k0 is e ^ e1. e/r is nothing but negative a1. So
e/r is –phi (2) and oops! Let us get to power point and our n is nothing but phi (3) okay.
So that is our linear least squares. Let us save and let us run this hopefully. It will run without an
error okay. We have run this and our e/r is 480.1, k0 is 4.8 and our end is 0.75 approximately.
(Refer Slide Time: 21:29)

266
So that is our rate of reaction using linear least square regression. Next what we are going to do
is, we are going to use nonlinear regression of this particular function directly using lsqnonlin.
So let us look at the syntax for lsqnonlin.
(Refer Slide Time: 12:30)

The syntax for the lsqnonlin is phi=lsqnonlin @ p space function name p and the initial guess. So
let us start with that okay. (Video Starts: 12:39) Let us say our initial guess for phi (0), phi (0) or
phi_guess, let us say was 1 was our k0. Let say our e/r was 100 and our n was also 1. So that was
our phi_guess okay. Our phi known phiLSQ is going to be lsqnonlin at phi. Let us say
rxnFunction phi okay.

267
So this is the standard syntax. We need to define our function. Function called rxnFunction and
our phi_guess. And I think that should be, yeah that should be all that is required now fName.
The function f sorry, the function f provides errors in the form of e=y- the model for values for
various values of phi. So we basically need to get that in our function. So let us go here, edit,
rxnFunction. Yes okay.

So function f error=rxnFunction and our phi. So k0 is phi (1). Our e is phi (2) and our n is phi
(3). Rate of reactions is going to be nothing but k0 * exp-e/rt. So –e. So you are here. We are
using this e/r so, e/r. /t okay. Multiplied by concentration c ^ n okay. That, the final bracket was
not required is just for our sake for human readable sake. That is rate of reaction and our f error
is nothing but yData - r, okay.

So to get parameters calculate rate okay. So before that we need to get what c is, c is nothing but
our xData all the row’s first column, t is xData all the row’s second column okay. So again when
I run this, I am going to get an error. So let us see what the error is going to be okay. Let us run
this and we get an error okay. The error is undefined function or variable xData in rxnFunction.
The reason why we get that get this error is xData and yData are defined in our workspace but
this is a function. They are not defined in our function. We need to somehow define pass on this
information in the function. So let us put this as 2 parameters, 2 additional parameters of the
function okay.
So xData and yData need to be passed on to the function by the reaxparam script okay. And the
way we do that if we know how to do this, is we have at phi. So that is the variable we need to
solve for and an rxnFunction in addition to phi. We pass on all the other parameters that are
required xData, yData okay.

Keep in mind xData and yData over here, stand for this variable name and this variable name the
xData and yData over here stand for basically the variable name that we are going to use over
here. And here they happen to be the same variable names, because it is the exact same data but
it need not be that I can actually replace this with just let us say xx and yy and that will also work
okay.

268
xData, yData will also work and xx and yy will also work. So let me just save this and run this
and see that it works hopefully. It should without an error, oops! There is an error okay. The error
is with the multiplication in line 11 and we wanted an element by element multiplication. So we
should have put a dot star there okay. And star is fine because k0 is a scalar. So let us, let’s clear
all and run this okay.

Okay, so it’s run and let us see what phiLSQ gives us. So with phiLSQ we got our k0 as 4.84.
Our e/r is 486 and our n is 0.75 okay. So we are getting very close numbers, using our lsqnonlin
as well as using our linear regression okay. Let us go over here and see what we have done again
okay. lsqnonlin at phi rxnFunction phi, xData, yData.

The name xData over here has to be the same as this name over here. yData has to be the same as
this name over here. phi has to be the same as this name over here okay. So let us change this
data to xData and yData. So everywhere we have changed this to xData, everywhere we have
change this to yData and let us run this okay. And it runs with our phiLSQ as the same values as
before.

So as you can see the variable names are dummy as long as this name is exactly the same as this
name. And this name is exactly the same as this name. There is actually not going to be any error
that you will see okay. (Video Ends: 20:01) So with that we have completed our aims of this
particular lecture. Our first aim was to convert this into a linear regression form and use linear
least squares and our second aim was to use nonlinear least squares using MATLAB function
okay.

Its linear least squares we use matrix method and created that matrix. The y matrix was log(r),
the x matrix the first column was ones. The second column was 1/t, the third column was l and c
and we use that in order to get our log (k0), our a/r and n. Then we used MATLAB function
lsqnonlin, the standard syntax for which was phiLSQ nonlin at p fName p, p not were the initial
guesses okay.

269
And if there were parameters, then we change this to at p fName p, param1, param2 and so on
and so forth. fName returns the vector of errors which is y-y model by which y is nothing but r-
the right hand side. So this is the error as a vector that fName needs to return okay. So with that
we come to the end of lecture 6.3 and I will see you in lecture 6.4. Thank you and goodbye.

270
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #06


Lecture No. #6.4
Regression and Interpolation – Interpolation options in MATLAB

We are in module 6 where we are covering regression and interpolation. In lecture 6.1 we gave
an introduction to regression and interpolation techniques. In lecture 6.2 and 6.3 we have
covered linear and nonlinear regression techniques in MATLAB. In this lecture we are going to
cover techniques for doing interpolation. Specifically we are going to talk of 2 interpolation
techniques in MATLAB called spline and pchip interpolation okay.
(Refer Slide Time: 00:43)

So what are these 2 interpolation techniques? Spline is known as a cubic spline interpolation. So
that’s the command that will use to obtain cubic spline interpolation. That’s nothing but fitting
the data with a cubic polynomial which satisfies certain properties so that we get an overall
smooth curve that fits all the data that we have. The other option in MATLAB is known as
Piecewise Cubic Hermite polynomial.

We will take 2 examples to show spline and pchip interpolation and we will use this example to
demonstrate when or when is it advisable to use pchip interpolation. And in all other conditions

271
we are going to use spline interpolation okay. The syntax for both spline and pchip are very
similar to each other. The syntax for spline I am giving over here. All you need to do to run pchip
is just to replace spline with pchip and you will be able to use pchip instead of spline.

The syntax is simple yInterpolated that means the output of the function spline is the interpolated
dependant data that is given by spline (xData, yData). xData is that independent data, yData is
the dependent data okay. And so this is the data that we have in with us. Now we want to
interpolate between some x values between these original data points okay. And that is given by
xVal. We will come to that example in in a little bit okay. So spline and pchip are 2 different
interpolation techniques that we are going to cover in today’s lecture.
(Refer Slide Time: 02:37)

Let us consider the example of temperature variations in a day; this data was taken for
temperature in Chennai on 30th of October. This starts with midnight of 29th of October and ends
at midnight of 30th of October and we have temperature data in degree Celsius at various
different times within the day on 30th of October.

So we have temperature data at 1 AM, 2 AM, 3 AM and so on. Let us say we wanted the data at
2:30 AM or 6:30 AM or let us say 10:15 AM and so on okay. In that case we do not have this
data directly available from the website. So what we are going to do is, we are going to use an
interpolation function in order to calculate the data at those intermediate points.

272
(Video Starts: 03:35) Let us go to MATLAB and see how that works okay. So I have already
created a file called interDiurnalT to have to have the data for us ready okay. So let us go and
check what the data looks like. So at time 0, the temperatures 25.6, at time 1 its 25.4 okay and at
time 23 is 26.8 and time 24 is 26.1. So, 26.8 and 26.1 okay. Interpolating temperature values.

So let us say we want the hI, let us say that contains the hours. Excuse me; the hours, at which
we want the values to be interpolated. So 2:30 AM is going to be 2.5, 6:30 AM is going to be 6.5,
10.15 AM is going to be 10.25 let us say we also wanted the data at 5 pm okay. This is just to
check how our interpolation works at 5 pm we expect our data to be exactly the same as what we
have over here.

So the last value should be equal to 31.8. So we will check whether that’s what we get okay. And
interpolated values TI are going to be nothing but spline let us go to the syntax spline x data
yData our xData that is the independent data is in the vector h, the dependant data is in vector t.
Keep in mind that the size of vector h and t are both 25. So they are row vectors of length 25
okay.

That is important, it is important that h and t are of the same size and hI are the interpolated at rr,
the hour values at which we want our interpolation to be done and TI is going to be the
temperature values at those points so let us run this okay. And let us go and check what the
values of hI and TI are okay.

So at 2.5 hours the temperature is approximately 25 degree Celsius, at 2.5 temperatures is


approximately 25, at 6:30 hours temperature is 26.13, at this point the temperature is 26, 13 at
10, 10 hours 15 minutes the temperature is 30.9 and at 17 hours that is at 5 PM temperature is
exactly the same as data which is 31.8 okay.

The temperature at this point is exactly the same as what the data shows over here okay. So these
are the temperatures calculated at the intermediate data points using the spline interpolation okay.
Let us go on to another example and this example is of a car moving on a busy Chennai street
okay. (Video Ends: 07:02)

273
(Refer Slide Time: 07:03)

This is the car that is moving in front of the government hospital in Chennai. And there is a
signal right in front of the government hospital. So the car takes one and half minutes to go from
this end of the road to the other end of the road. The road that we are currently monitoring it is.
When it enters this particular road the speed of the car is 45 kilometers per hour. It comes to a
halt at the signal under speed becomes zero.

It is stays as 0 for something between 10 to 20 seconds okay. After which the car starts moving
again and the car accelerates to go up to 55 kilometers per hour okay. So this is the data. (Video
Starts: 07:52) Let us go to MATLAB and let us bring up the file which I have already created
with this data. So this is the time and this is the speed okay. Interpolation using spline okay. ,TI,
let us say our let us say we want our speeds at 5 seconds, 15 seconds, 25 seconds, 35 seconds so
on and so forth.

So let us say 5:10:90. I could have also made at 85. That is not a problem, so the time values are
going to be 5, 10, 15, 20 sorry, the time values are going to be 5, 15, 25, 35 up to 85 okay. The
speed interpolated using spline is the command is same as before spline independent data,
dependent data and the vector at which we want the interpolated values okay. So let us run this
okay.

274
So, when we run, we have we now have the interpolated data in vectors tI and sI. So tI, sI, let us
type this out okay. So at time 5 the speed is 45.37. So the speed has kind of slightly increase
from 45 to 45.37. In this interpolated data at 15, the speed has dropped to 13.9, at 15 seconds of
speed has dropped to 13.9. Now there is something interesting that happens at 25. The
interpolated value of speed shows -3. 2 okay.

What that means is that between these 2 points although we know for a fact that the car is
stopped at that signal. So between these 2 points the speed has to be 0. The interpolated value
shows that the speed is nonzero. The reason for this is because splines tries to fit a smooth curve
to all these data points. It tries to fit a smooth curve and when it tries to do that, it gets the value
between 20 and 30 to be negative okay.

Let us go and change this code a little bit. So between 5 and 90, we want the interpolated values
at all data points okay. And we also want to plot them. So let us say we will plot our original data
t, s. We will plot them as blue circles. Let us say hold on okay and we will plot. Let us move this
up here and we will plot our interpolated values tI, sI as black line okay and let us see what is
spline does.

So in fact tI 5 to 90 let us go right from 0 to 90 in steps of 1. So the interpolated value SI is going


to be speeds at 0 seconds, 1 second, 2 seconds, and 3 seconds and so on up to 90 seconds okay.
So let us run this and see what we get okay. So this is the blue lines are the original data and
black lines are the interpolated data.

As you can see that the spline interpolates is the data in form of a smooth curve and because this
particular speed has dropped rapidly to 0 and stayed at 0 for a while. When we try to fit a smooth
cubic spline to this particular data, we get negative values between 20 to 30 seconds okay. Now
we know for a fact that the speed will now is not going to be negative because the car is not
expected to go in the reverse direction.

So that kind of constraint is actually there in that data. So let us say now we want that particular
physical situation to be handled more gracefully under such situations. We will use the other

275
interpolation formula which is the pchip interpolation formula okay. (Video Ends: 12:34) So let
us see how we will use pchip okay. What we said was the syntax is exactly the same we just
replace spline with pchip okay. And that is exactly what we are going to do in MATLAB.

(Video Starts: 12:45) So this is what we had written earlier. So let us just copy paste this
interpolation using pchip and sI_pc equal to instead of spline, pchip okay. And let us just do this
for tI from 5 to 90 in steps of 10 okay. For now we do not need plot. So let us comment this out
okay. And let us clear all, close all and clear the screen oops! Yeah, let us run this okay. Yeah we
did not need the figure plotting okay.

So let us see what tI sI where, this is what tI sI where and let us type tI and sI_pc and see what
they were okay. So let us focus on the point at which we got negative values of the speed okay.
And that was 25. At time 25 the value of speed you are using the spline interpolation was -3.23.
Whereas, using pchip is zero.

So pchip maintains this kind of monotonicity in the data. What that means, we will come to that
in a minute but this particular expectation that the speed is not going to go below 0 when the
vehicle is at halt is maintained by the pchip interpolation. Whereas that is not maintained by
spline interpolation. At other data points, we have spline and pchip behaving in more or less
similar manner.

There is some difference between the interpolated values but that difference is not very
significant. The most important difference comes in at this particular data point where we want
pchip or we want an interpolation to match the requirement that when the vehicle comes to a halt
its speed is at 0 okay.

So let us plot this. Let us change this back using control z to get the data, to get the interpolated
data at all points between 0 and 90. And let us plot this also and plot sI_pc. And let us plotted
using red- line okay. So this is the new command that I have okay. I am plotting tI versus sI_pc
as red- line and that I will use to compare with this spline okay. Okay let us clear everything
okay. So this is what we have and let us run this okay.

276
So the blue circles are the original data points, the black line that you see over here is the spline
interpolation that we have done earlier. And a red dash line is the pchip interpolation, the
piecewise cubic hermite interpolation. Now you see the difference between spline and pchip
okay. There is no difference for example or there is very little difference between these 2 points
or between all of these points but there is significant difference over here and over here okay.

And the difference arises between pchip and spline because of the following reason. What pchip
does is it maintains monotonicity. What that means is between these 2 data points when we try to
interpolate the y value, is always going to lie between these two y values okay. As you can see
spline the value at this intermediate point is greater than the value at this point. Pchip does not
allow that to happen all the values in the intermediate point strictly lie are below this value and
above this value okay.

That is the other reason why when this p is 0 and 0 over here. We get this flat line using the pchip
interpolation because pchip ensures that the monotonicity is maintained. So let us go to power
point and finish, what, what we started off with. (Video Ends: 17:38) So what we did in todays
lecture is we considered 2 popular interpolation techniques spline interpolation and pchip
interpolation and we demonstrated that using 2 examples.

The first one was temperature variation near during a day. In this particular example we were
happy with what the spline interpolation gave us. In this particular example we had a constraint
that the speed is not going to fall below 0 and pchip was therefore found to be more suitable
because pchip maintains the monotonicity in the data. So that is primarily what we have covered
in this particular lecture. This is the last lecture of module 6. Just to recap very briefly, what we
did in module 6?

In lecture 6.1, we went to introduction of regression and interpolation, in lecture 6.2 and 6.3 we
covered several examples of regression techniques, linear as well as nonlinear regression
techniques using MATLAB. In today’s lecture we have covered 2 specific interpolation
techniques those were a spline interpolation and pchip interpolation. So with that we come to the

277
end of module 6. I will see you next week in the next week in module 7. We are going to cover
ordinary differential equations. So see you in the next week. Thank you.

278
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #07


Lecture No. #7.1
Ordinary differential equations – Initial value problems – Introduction and Euler’s
Methods
Hello and welcome to MATLAB programming for numerical computations. We are in week
number 7 in module 7 we are going to cover ordinary differential equations initial value
problem .In this module we are going to tackle methods for solving ordinary differential
equations. Lecture 7.1 we are going to go over introducing ourselves to ODE and take up 1
example and solve it using Euler’s method. Euler’s method is one of the simplest methods for
solving ODES.
(Refer Slide Time: 00:48)

So let us let us look into the ODE problem. So introduction, what is an ODE. ODE is when we
want to solve an equation of the form dy/dt =f (t, y) subject to initial conditions, y at some time
t=0 is given us y0. We start with that initial condition and march forward in time at each time
finding the value of y at that corresponding time instant. In this way we will get the overall curve
y as a function of t which i have plotted over here.

279
Now the function f (t, y) is nothing but the slope of this y versus t curve at any point in the
domain okay. So if we are going to start with a y0 t0 we will then march on and find y1 t1 y2 t2
y3 t3 so on and so forth is what we are going to actually find. Eventually we will develop the
entire curve y as a function of t. And that is the actual solution that we are intending to find using
a numerical scheme.

The discussion and theory for numerical methods for solving ODE-IVP initial value problems is
discussed in computational techniques course module 7 the link for which is given over here. In
this lecture or for that matter in this course we are not going to cover any theory or discussions
with regarding ODE-IVP. Rather we will use MATLAB in order to solve the initial value
problems. Let us get started with 1 of the simplest methods which is known as the Euler’s
method. Before we go on to Euler’s methods ah method let us look at how we would want to
solve this ODE.
(Refer Slide Time: 02:44)

We can write dy/dt in a finite difference form (y i+1 - y i) / h where h is known as the step size
okay. If we remove the limit h tends to 0 and take it as a very small value, we can rewrite this
equation in the form y i+1 = y i + h * F (t, y). Now if the function f is calculated at yi and ti we
get what is known as Euler’s forward difference or sorry. Then we get what is known as Euler’s
explicit method.

280
In general numerical methods for ODE-IVP we try to find certain slope Si which is the best
estimate that can be used in order to march forward along this curve. So, Si is going to be
dependent on y, y’s and t’s and we are going to find that particular Si such that we get y i+1 as
close as possible to the true solution.
(Refer Slide Time: 03:59)

In the simplest case us we can consider Si as nothing but f calculated at T(i),yi and that is what
Euler’s explicit method is. y i+1 = y i + h* f (ti, yi) is the formula that we will use for Euler’s
explicit method. At the initial time we have y0 t0 we can use this in order to compute now y0 is
known, t0 is known and y0 is known therefore f at t0 y0 is known. We can use this to compute
y1. Once y1 is known and t1 is known we can use them to compute y2.

That we can use to compute y3 so on and so forth okay. So this becomes very simple straight
forward method in order to solve ODE initial value problems. The problems with Euler’s explicit
method are 2.The first problem is that the method is not very accurate to tackle this problem. We
are going to talk about Runge- Kutta method in lecture 7.2 and thereafter, the second problem is
regarding the step size that we need to choose in order to get the system to be to be stable.

Stability of numerical methods something that I will introduce in this lecture but that this beyond
the scope of this course. I will introduce the concepts of stability and so on, mainly for the sake
of completeness although it is not strictly a part of this MATLAB course okay.

281
(Refer Slide Time: 05:33)

So the example that we will solve is dy/dt = -2ty with the initial condition y0 = 1.If we solve this
analytically the solution we are going to get y at anytime t is going to be equal to e exponential
of -t square. What we want to do is use Euler’s explicit method and compare the solution using
Euler’s explicit method to the analytical solution that we have written down over here. Let us go
on to MATLAB to solve this problem using Euler’s explicit method okay.

(Video Starts: 06:09) Let us create a file called Euler’s explicit, solve ODE-IVP. So we have put
down some parameters t0, y0 was what was given to us let us say that we want to solve it until
time tEnd =5. And let us say the step size that we are going to be use is 0.1. So put this down ah
all of this over here, initializing, okay. I have put a transpose over here because we want our t
and y to be column vectors.

y is the solution vector and that we will initialize as zeros of size (N+1). And y1 which is the first
value is equal to y0. Keep in mind you would have realized by now that MATLAB does not have
array location 0. The array start with index 1 and therefore we will use the index 1 for time T = 0
okay. In addition to this we also need to specify the value of n. So n is going to be nothing but
tEnd- t0, t0 / h okay.

282
Let us save and run this to see that we do not get any errors okay. So we are not getting any
errors over here let us now continue solving using Euler’s explicit method for okay. fi is going to
be nothing but -2*T(i)*y(i) . That is our fi and Y (i+ 1) is y (i) + h*fi. So that is our Euler’s
explicit method. So let us go and look at us Y (i+1) is y (i) + h*f (ti, yi) okay. Our f was- 2ty. So f
at time i, is going to be nothing but -2 multiplied by the vector t, ith location multiplied by vector
y, the ith location okay.

That is going to be our fi. Y (i+ 1) is nothing but y (i) + h* fi okay and end, this should do. So
that completes our Euler’s explicit method. Let us plot this, plot (T, Y). So let us hope that this
runs without an error run. And this is the numerical solution using Euler’s explicit method. Let us
also compute the errors. So plot results and obtain errors yTrue = exp (- T square) correct yeah.
exp of(-T .^ 2). We need dot because this element by element squaring and err = abs, absolute
value (yTrue- y) okay.

So that is what we have, let us clear our screen and that is called Euler’s explicit here, solves this
and let us look at error err let us call max, max value of err and the max (ERR) is 0.035. If h was
reduced to 0.01 let us run this okay. And see max (ERR) okay and the maximum error went from
0.03 to 0.003. So by reducing h by 1 order of magnitude the maximum error also reduced by 1
out of magnitude.

Let us save this by reducing h further and we find max (ERR) and the max (ERR) fell by another
order of magnitude. We now instead of 3*10 ^ -3, we now have 3*10 ^ -4. So from here you
would have probably guessed start the order of accuracy for the global truncation error rather for
Euler’s explicit method is the order of h ^ 1. This is something that will cover in a little bit more
details in 1 of the later lectures in this module okay. (Video Ends: 12:42)

Okay so let us go back, change our h to our old value and let us go back to our problem okay. So
this was the example that we solved using Euler’s explicit method. Now in Euler’s explicit
method our f we calculated at (ti, yi). We could have very well calculated f at T (i+1), Y (i+1)
instead of (ti, yi). If we were to do that we will get what is known as Euler’s implicit method.

283
(Refer Slide Time: 13:03)

In the explicit method ti and Y (i) were already known and you could directly calculate this and
assign it to Y (i + 1). However that is not possible in Euler’s implicit method because our y(i)
itself depends on y sorry, our Y (i+1) depends on itself through this function f. So this is a
nonlinear equation which we need to solve. We can do it by using a nonlinear solver such as
fsolve. fsolve is something that we covered in module 5 of this course.

So if we were to rewrite this we will rewrite this as Y (i + 1) - h * F at (T (i+ 1), Y (i+ 1)) – y (i)
= 0. And this we can assign it to fsolve and we can solve okay. Now Euler’s implicit method is
not a part of this particular course. (Video Starts: 14:05) However for the sake of completeness I
have already created Euler’s implicit code and I will display that clear all, close all, clc edit
Euler’s implicit okay.

This is the code that I have created earlier for same values of y0, t0, t and h everything else is
remaining same, now our f is calculated at T (i+ 1), Y (i + 1). So first I will calculate T (i+ 1)
okay. Y (i + 1) is yet unknown and therefore we need to use fsolve in order to solve this okay. So
Y (i + 1) is the unknown quantity so this is unknown this over here is unknown. So we have used
what is known as anonymous functions in MATLAB in order to give this particular f of x. (Video
Ends: 14:58)

284
So that f is nothing but y - h * (- 2 * t * y) - Y (i). So this is what was written over here okay.
(Video Starts: 15:14) And the initial guess that we are going to give is capital Y (i) okay an
anonymous function is another thing that we have not covered in this in this course. We are not
going to cover anonymous function in MATLAB in this particular course so these are 2 concepts
which are actually beyond the syllabus if you will okay.

I have just shown it for the sake of completeness, so that you can understand how we can do this.
Let us also plot and obtain errors as before. Plot (T, Y), ERR = abs (yTrue- y). We also need
yTrue. yTrue is exp (-T .^)and there is a cap dot cap sorry, instead of cap okay. This is basically
what we have done for Euler’s implicit. Let us run this and hope that we will not get any error
okay.
One thing you will notice is that Euler’s implicit method takes a little bit longer than Euler’s
explicit method in order to run that is because we need to solve fsolve at each line okay. And the
figure looks approximately similar what we had in Euler’s explicit method. Let us go and find
out the max (ERR). Max (ERR) is again of the order of 0.01. If you recall in Euler’s explicit
method also with h=0.1. The error we got was 0.03 okay.

So the order of accuracy of Euler’s implicit method is similar to that of Euler’s explicit. (Video
Ends: 17:06) So now the question is, if the order of accuracy is similar between Euler’s implicit
and explicit and implicit method is much more difficult to solve than Euler’s explicit method,
why are we interested in implicit methods in the first case. The reason why implicit methods are
very useful is because implicit methods are globally stable okay.

This was covered in the computational techniques course module 7.5 link for which is given over
here. Basically what that means is, if we start increasing the step size, we will need to sometimes
increase step size in order to speed up the overall computation. If we start increasing the step size
there will come threshold value of h beyond which Euler’s method explicit method will become
unstable. This means that you will not get a stable solution.

Euler’s implicit method on the other hand is globally stable which means you can choose any
value of h of your liking. And Euler’s implicit method will remain stable. The stability is the

285
reason why implicit methods are popular especially for solving tough ODE problems okay.
(Video Starts: 18:17) Let us kind of let us now show what loss of stability means.

Okay, we will just clear this and we will go back to Euler’s explicit method. So now if h instead
of 0.1 let us say if we were to take 0.25. And we run this okay. We get the solution. Now the
solution qualitatively looks similar to that we saw earlier. Only thing is it looks more bumpy and
that is because the errors are higher with step size of 0.25. While the errors are higher the
solution is still stable.

Now let us increase this to 0.5. Let us run Euler’s explicit. And we see that is something funny
that is happening. There is nothing that happened in the first 2 steps rather. In the third step the
solution directly went from 1.0 to 0.0 okay. And that is the reason why that happens is h=0.5 is at
a border of the stability. If we increase our h beyond 0.5, we will make the overall solution
unstable.

So let us go and increase this to h=1 and we will see what instability. Let us run this okay as you
can see the value of T(i) sorry, value of Y (i) at t=5 is greater than 100. And this particular series
solution is diverging okay. The reason is because Euler’s explicit method in this particular
example has a limit of stability at h=0.5. Any value of h > 0.5 will result in unstable solution.

Let us actually go and change h to 2.5 and see what happens okay. And our solution has gone to-
12 and if again same thing if we were to change 5 we will see a similar behavior. Let us changes
to 10 see what the behavior is okay. And you see we have gone that the solution has reached
something like -1 * 10 ^ 4. So we have very clearly gone this taken the solution to an unstable
range. Let us compare that with Euler’s implicit.

We will use h=2.5 and tEnd = 10. Keep things the same as before tEnd=10, h=2.5. Let us save
this okay. And let us run this and see okay. Although this solution is very approximate, we do not
have a very good approximation using ODE using the ODE solver Euler’s method. We still have
the solution to be stable. The reason is that Euler’s implicit method is globally stable. (Video
Ends: 21:36)

286
So we can choose the size, the step size h as large as we want. And the solution will still remain
stay okay. So with that I will come to the end of lecture 7.1. What we covered in lecture 7.1 is
introduce ourselves to ODE initial value problems, took up 1 example and solve it using Euler’s
method. Euler’s method was the simplest method that we could choose.

And we compare Euler’s implicit and Euler’s explicit. In the rest of this module we are going to
work with more accurate explicit methods known as the Runge- Kutta family of this in the next
lecture. I am going to introduce the second order Runge-Kutta method. So that is our plan for
remainder of this module. See you in the next lecture. Thank you.

287
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #07


Lecture No. #7.2
Ordinary differential equations – Initial value problems Runge Kutta (RK-2) Methods

Hello and welcome to MATLAB programming for numerical computations. We are in module 7.
In this module, we are covering ordinary differential equations, initial value problems. In lecture
7.1, we introduced ourselves to ODE initial value problems and we used Euler’s explicit and
Euler’s implicit.

We said in the previous lecture, at the end of the previous lecture, is that in the rest of this
module, we are going to cover, explicit methods of the type known as Range-Kutta family of
methods.

In order to introduce ourselves to RK methods, we will consider the second order Range-Kutta
method. Before going to the, second order Range-Kutta method, I will give you a general idea of
what Range-Kutta method looks like.

(Refer Slide Time: 01:06)

288
Euler’s explicit method, we had Y (i+ 1) = Y (i) + h * f (ti, yi) okay, which we have written as fi.
This is how we used the Euler’s explicit. We also said in the previous lecture is that for getting
higher accuracy, we can replace our fi, by some other way of calculating the slope Si.

And, we can use that slope, in the overall approximation Y (i+ 1) = Y (i) + h (Si). Now in nth
order Range-Kutta method, gives the slope, as a weighted sum of, n different functions okay.
Now in this case the nth, functions are going to be k1, k2 up to kn. k1 is going to be nothing but f
(ti, yi). k2 will depend on (ti, yi) and k1. k3 will depend on (ti, yi) k1 and k2 and so on. kn will
depend on (ti, yi) k1, k2 and so on up to kn-1.

Once we update the k1 to kn, we calculate the weighted sum, w1 k1+ w2 k2 and so on up to wn
kn, substitute this over here. And we will get the next time solution using the Range-Kutta
method okay. So, that is how the Range-Kutta method works. Range-Kutta method is a generic
method for any nth, any number n. Most popular are, fourth order Range-Kutta methods. Range-
Kutta methods the theory and discussions behind it, was discovered in module 7 part 2 of
computational techniques course, the link for which is given over here okay.

(Refer Slide Time: 03:05)

289
Now let us consider, a couple of second order Range-Kutta methods okay. In Heun’s method Y
(i+1) is Y (i) + h /2 *(k1+ k2) which basically means our w1 and w2 are both 0.5 okay. k1 is
nothing but, f computed at (ti, yi ). And k2 is f (ti+ h, yi+ h) multiplied by k1 okay. So that is the
formula for Heun’s method. It is a second order Range-Kutta method because we have w1k1+
w2k2 only okay. That is why, it is a second order Range-Kutta method okay.

What we can do now, is we will go to MATLAB, we will start with the example that we took in
the previous lecture and we will modify it to solve that using RK2 method. (Video Starts: 04:05)
Okay this was, Euler’s explicit method, let us save that as rk2Heun. Solve ODE-IVP using
Heun’s method, okay. This initial part remains the same, let us take tEnd as 5 and h as 0.1.
Initialize the solution, in exactly the same way as before, solving using Heun’s method okay.

So the first step, in Heun’s method is going to be computation of k1, which is f (ti, yi) okay. So,
our k1 = f (ti, yi). So, -2*T (i) *Y (i) that happens to be the same as fi, that we had over here, that
is actually fine okay. tNew, let us call this particular guy as tNew. So tNew = T (i) + h, yNew is Y
(i) + h*k1 okay. And next thing, we want to do is, calculate our k2. So, k2 is function of
calculated at, -2 * tNew *yNew. And let us see Y (i+ 1) is Y (i) + h /2 * (k1+ k2) okay.

And that is our Y (i+1). Everything else remains the same, we do not need to change anything.
So majorly the change that happened, was only in this part. Where, we changed from Euler’s
explicit method to Heun’s method. Let us save this and hopefully when we run it, we will not see
any more errors. We have run and now we have the solution, using the Heun’s method. If you
recall from lecture 7.1, the error, the maximum error was 0.033. Let us find out what the err is, in
using Heun’s method.

Max err is 0.002 instead of 0.03 that is 3 * 10 ^ -2. The error using Heun’s method is, 2 * 10 ^ -3.
So Heun’s method, the RK 2 method, is more accurate than the Euler’s okay. (Video Ends:
07:34) So, let us go on to Heun’s method, and we will recap what we did. So, we started with the
Euler’s method code and we modified the Euler’s method code as follows. First we did, within
the for loop, we calculated k1 which is function of (ti, yi), k2 we calculated in 2 steps.

First, we calculated, tNew which is T (i) + h then we calculated yNew which is Y (i) + hk1. Then
we calculated -2* tNew* yNew that was the function that we were interested. In midpoint
method w1 is taken as zero and w2 is taken as 1. With this, this is the overall equation, k1 as

290
before is f (ti, yi), k2 as before is f computed at, sorry, k2 is different over here, I am sorry, k2 is
f(ti+ h/2, Y(i) + hk1/2) okay. We calculated this substitute in k2 and we use the midpoint method.

(Video Starts: 08:40) Let us go again to MATLAB and solve this using midpoint method. This
time I am going to solve it from scratch. Edit rk2Midpoint, solve. Copy the preamble part, and
the initialization part, those we are known, going to remain the same. We will check it anyway, t0
is 0, y0 is 1, tEnd is 5, h is 0.1 and n is tEnd - t0 the whole thing divided by h. We have
initialized our t vector, we have initialized our y vector and with y1=the initial condition okay.

Solve using RK2 midpoint method for i=1: n okay. So, first step is to calculate k1. k1 is f (ti, yi).
So k1 = -2*t*y*T (i)*Y (i) okay. k2 is going to be -2*tNew*yNew okay. What is tNew? Our
tNew is T(i)+ h / tNew ,yNew is yi+, yNew is Y(i)+ hk1 /2, Y(i)+ h * k1 / 2 and k2 is -2 tNew *
yNew and Y(i+1) is Y(i)+ h multiplied by slope. And slope, that s slope is nothing but, k2 in this
particular case. So h /k2, end plotting, plot (t, y) T. ^ 2 and err = abs (Y-Ytrue). Okay because we
are using abs function it does not matter if it is (y-Ytrue) or (Ytrue-y) both mean the same thing
okay. So we have this and let us save and let us run it. We hope that this runs without any error
okay.

Let us, there might be an error okay, y new is undefined on line 20. So let us go okay. yNew, yes,
the reason why I am getting this error is, I have defined this as small y, capital new, MATLAB is
case sensitive, so this has to be small yNew. So, let us save this, okay again to emphasize, we
need to be in order to debug a code, and we need to be able to understand what errors MATLAB
is throwing.

MATLAB has thrown the error, undefined function or variable yNew on line number 20. That
means, MATLAB does not understand or did not understand what the variable y, capital y,
capital new, stood for. And the reason, why we got that error was because there was a type error.
We changed yNew from capital y, capital new to small yNew to match yNew that we had over
here okay. So, let us clear all and let us get to midpoint, okay now this time it runs without any
error okay.

Let us now calculate the absolute error, value of absolute error, max err okay. And this is also, 10
^ -3. So, the order of accuracy, of rk2 midpoint method, is similar to the order of accuracy of rk2
Huen’s method. Both these methods are more accurate than the Euler’s method.

291
Let us reduce, the h by 1 order of magnitude and see what we get. Let us save, h was changed to
0.01 and run and max error okay. The error has dropped from 10 ^ -3 to 10 ^ -5. So, the error has
dropped by 2 orders of magnitude. Let us change this to 0.001 and let us run it, max error has
fallen, by further 2 orders of magnitude. So, from this 1 of the things that might be clear to you
guys, is that the order of accuracy for from global truncation error view point, for Euler’s method
was h to the power 1 whereas for RK method is h ^ 2.

We will consider the error formulation for RK methods, in a more formal way in a subsequent
lecture. (Video Ends: 14:49) This brings us to the end of this lecture. In this lecture, what we
have covered is introduce ourselves to, Range-Kutta family of methods and solve the ODE initial
value problem, in single variable, using 2 different rk2 methods, rk2 Huen’s method and rk2
midpoint method. There were two aims in here for us to do that.

One was to demonstrate that the rk2 method is more accurate compared to the Euler’s method.
The second thing was to demonstrate that, Range-Kutta is not a single method, but it refers to a
family of methods. For any particular order, there are various different types of methods that are
available, as well as there are different orders of accuracy. With this I come to the end of lecture
7.2. I will see you in the next lecture thank you.

292
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #07


Lecture No. 7.3
Ordinary differential equations – Initial value problems MATLAB ode 45 Algorithm

Hello and welcome to MATLAB programming for numerical computations. We are in module 7.
In this module, we are covering ode initial value problems. We are in lecture 7.3, and in this
lecture, we will consider a MATLAB algorithm, known as ode45 in order to solve ode initial
value problems. Ode45 is a kind of a workhorse in MATLAB. It is the first algorithm that you go
to in order to solve ode problems okay.

(Refer Slide Time: 01:04)

Let us go and look at the syntax of ode45. So, typical use of ode45 is as follows okay. Now, as
we have seen in the previous lecture in rk2, we obtained the solution yi, at various values of ti
okay. Likewise, ode45 also, returns the values of yi at various values of ti, between t0 and tEnd.
The intermediate values at which computations are done, are calculated internally by ode45.
Ode45 uses a Range-Kutta fourth order method, an rk4 method, recall that we have covered rk2
method in the previous lecture.

293
Not only that, not only does it cover and uses an rk4 method, it uses what is known as adaptive
step size rk4 method. What that means is, that the step size h is not a constant value, recall that in
the previous lecture, we took the step size h= 0.1 and we later on change that to 0.01 and so on
okay. We had predetermined what the step size is going to be.

ode45, does not predetermine what the step size is going to be. But at each step, it calculates
what the optimum step size is, in order to minimize the error, to get the error among, within the
tolerance that, we have set for ode45. These are rather advanced concepts, which we are not
going to cover in this particular course. However, I wanted to point this out to you, because this
is something that you will come across again and again, if you are going to use ode45 or any
other MATLAB odes solvers.

All the solvers in MATLAB are variable step size, or adaptive step size solvers where, MATLAB
internally decides the step size to get the errors within the required tolerance intervals. Okay, so
the typical way how we will use ode45 is ode45 at (t, y) recall, we go to our previous lecture,
lecture 1 okay.

(Refer Slide Time: 03:20)

294
We were given ode in the form of dy /dt = f (t, y) okay. So, the function, f is a function of both t
and y and we have to provide that function. So, that function takes in 2 arguments, time t the
dependent variable y. And returns, the vector dy okay. In this case, because we are interested in
looking at single variable problem, it will return just a scalar okay.

So ode45 gives (t, y) as the arguments, which are passed on to the function fName okay. The
initial time t0, and the final time tEnd, and the initial solution y0, are also passed on to the solve
okay. So the fName, is the name of the function that returns dy /dt as a function f (t, y). (Video
Starts: 04:24) Let us look at the examples that we have been covering so far, in this module using
ode45. Let us look at rk2heuns method. This was our code for rk2heuns method. You will now
solve it, using ode45. Edit runODE, solve ode ivp using okay.

So t0 = 0, y0 = 1, tEnd = 5. We do not need h over here okay, solving using ode45. Let us look at
the syntax, the syntax is, (tSol, ySol) = ode45 (tSol, ySol) = ode45 @ (t, y). So t and y are the
variables that are passed on to the function that ode45 code is going to call. Let us call this
function, my firstODEfun (t, y), again the order of t and y is important, it is it has to be (t, y) and
not (y, t). This is one area where some students make a mistake, that mistake is not very common
but some students do make that mistake.

Next one, is in square brackets, we need to give that tspan, the time span (t0, tEnd) okay, next we
need to give y0, y0, that should be a and plot okay. So, let us run this and see what happens.
Okay, it did not run, probably there were some error, undefined function firstODEfun. Why did
we get that, because we have not yet defined our function firstODEfun? So let us go ahead and
do that. Again, see when you run MATLAB functions, in order to solve ode or to solve nonlinear
equations, solve whatever, you get a bunch of errors. And the errors with trace back to the calling
functions.

Let us make that function, firstODEfun function dy = firstODEfun (t, y) okay. And dy is nothing
but -2*t*y okay, that is all there is to it, okay. And if we save this, we run this, we will be able to
solve this particular problem. So, let us not run firstODEfun, but run odescript okay. So let us
clear all and run odescript okay. And we get the solution as seen over here and the solution
smoothly goes on to y = 0 okay. That is the solution that we get.

295
Let us go on to firstODEfun and may be make a small mistake somewhere. So, let us make this
as t1. Here you want to pass how the errors look like, let us run ode and see how they are going
to look like okay. So undefined function or variable t1, the error is noticed in firstODEfun on line
number 2. So, let us go to firstODEfun at line number 2 and see what that error is t1 is
undefined, because our input variable v sec was t okay.

Keep in mind, our t and y are basically dummy variables. So, could have very well named this as
t1 and that would not be a problem at all okay. For that matter we could have named this as
MATLAB and that would also not be a problem course okay. Let us call this that is also not
going to be a problem at all. So, let us run this, run ode and see whether we get the solution or
not clear all okay. The first ode we have just called the first argument as MATLAB, the second
argument as course, of course it makes absolutely no sense.

But, I am showing you, again this is something that some people do get stomp on and that is the
reason why, I am showing you this particular crazy example. Let us run this and we get the exact
same solution okay. This is demonstrate to you is that, the arguments of this what you call them
is not important. The order of these arguments is very important. This has to be y and this has to
be t.

You cannot replace t and y, you cannot switch t and y, the t argument has to come first and y
argument has to come second, you can call it by whatever name you wish to call it and it will not
be a problem, as long as the order of the arguments is maintained the same okay. Let us save this
and just for our sake, we will run this once again and we see that we get same solution okay. So,
that was the example, for solving ode of the type -2(t, y) okay. (Video Ends: 11:05)

(Refer Slide Time: 11:07)

296
Let us take one more example, and this example was covered in computational techniques course
in module 7 of the computation techniques lecture course. And this one is to find the
concentration along a plug flow reactor. dc /dv is going by -1/2*c ^ 1.25. With the initial
condition c at v =0 was equal to 1. So we want to solve to find c at values of volume 1, 5 and 10.

(Video Starts: 11:36) So let us go over here and editPFRode, solve plug flow reactor simulation
okay. So, our c0 was equal to 1.0, our v0 was equal to 0, our Vend was we had 1, 5 and 10 okay.
And what else do we need, that is pretty much it that is all that we need. We will need, vSol, cSol
okay. C is the dependent variable, V is the independent variable equal to ode45 @ the
independent variable t, y. t is the independent variable that is v, c pfrFun okay. We can call this as
(t, y) also. We just want to call this as V, V, VSpan, c0. Okay we will come back. Using ode45
okay so over here clear all and help ode45 okay.

So, ode45 TOUT, YOUT = ode45 (ODEFUN that is function name, TSPAN, y0). TSPAN = (t0
TFINAL) integrates from time t0 to tfinal. To obtain solutions, at specific times t0, t1, t2 up to
tfinal, all increasing, all decreasing okay, for our purpose we will just use increasing. Use
TSPAN = (T0, T1 upto TFINAL) okay. So, what we want is, we want a solution at 1, 5 and 10.
So our VSpan is going to be v0, 1, 5 and 10 which is nothing but Vend. So, that is going to be our
VSpan okay. So we have this we save this okay.

297
And next what we need to do is we want to create the pfrFun. So let us new function dc = pfrFun
(V, C). dc = -1/2 *c ^ of 1.2, 0.5 * C ^ 1.25. That is it and we need to call it as pfrFun.

So just check, pfrFun is wanted to be V and C are the order of the arguments. And so, and vspan
is v0 followed by the values of v at which we want the solution okay. So let us run this and we
have got the solution. So vSol you will see is 0, 1, 5 and 10. And cSol is the concentration at
these values. We start with the concentration of 1.0 the concentration falls to 0.6 where, the
volume is 1 liter and the volume is 5 liters, the concentration has fallen to 1.4 and when the
volume is 10 liters, the concentration has fallen to 0.039 okay. So, this is how we will use ode45.
(Video Ends: 16:02)

With that, we come to the end of lecture 7.3. What we covered in this lecture is, method to use
MATLAB ode 45 code, to solve ode initial value problem, we used two different examples, to
use for ode45. And using the two examples, we showed how simple it is in order to run ode45 for
single variable problem. With this, we come to the end of this lecture 7.3 and I will see you in the
next lecture. Thank you bye.

298
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #07


Lecture No. #7.4
Ordinary differential equations – Initial value problems Runge Kutta Methods

Hello and welcome to MATLAB programming for numerical computations. We are in week
number 7, in this module we are covering ordinary differential equations- initial value problems.
So, far we have looked at, Euler’s method and Runge-Kutta second order method and we have
solved a couple of problems using them. We have also looked at ode45 algorithm that MATLAB
provides in order to solve ode initial value problem.

(Refer Slide Time: 00:48)

In this lecture, we are going to go further and use higher order Runge-Kutta methods. Before, we
do that, let us revisit RK-2 Heun’s method. What we are going to do today, is the RK-2 Heun’s
method, that we had developed, for a specific problem y`= -2ty. We are going to modify that, to a
generic problem that takes myFun (t, y) as an argument.

So myFun (t, y), basically generates, this f (t, y) and passes it on to RK-2 Heun’s method. Let us
go ahead and modify the code in order to do that. First we will do this, for the mod for the

299
system y `= -2ty then we will extend it to the pfr problem. Let us go to MATLAB and do this
okay.

(Video Starts: 01:38) So, this is the RK-2 Heun’s method, that we had written in lecture 7 .2
okay. So, what we are going to do is, instead of this guy, be hard coded into this, what we are
going to replace it with is, through using a function myFun okay.

So, let us first create a function myFun, function dy = myFun (t, y) and dy = -2*t*y okay. Let us
save this as myFun okay. So, k1 should be replaced by myFun (t, y) okay. Right now k1, is
basically going to be (ti, yi). So, I am just going to replace that (ti, yi) okay. So k2, is also
nothing but f (tNew, yNew). The function f, is obtained through, again the same function myFun,
so, we will just do this okay. And that should complete our problem.

Now, what we also want to do is, we probably do not need this additional (tNew, yNew) over
here. So, I will just remove this and directly replace tNew with T (i) + h and I will replace yNew
with Y (i) + hk1 okay. And this becomes, our RK2heuns method, which uses the function
myFun. Let us save this and let us run okay. And it shows the overall curve, which looks exactly
similar to what we had earlier okay. So let us, also look again at, max error, max error was 0 .
0021 similar to what we had gotten earlier in lecture 7 .2 okay (Video Ends: 04:43)

So, what we have done is, we have generalized RK-2 Heun’s method to use function myFun.
Now, what we are going to do is, to extend it to the PFR problem okay. (Video Starts: 04:56) So,
runPFRode was the code that we had used earlier in lecture 7 .3, in order to run the pfrode. What
I am going to do now is, in addition to solving using ode45. I will solve, using RK-2 Heun’s
method okay. So, for doing this, for all I am going to do, mostly, what I am going to need is this
part and this part.

So, let us just take this. Copy and paste this over here okay. Prior to solving using Heun’s
method, we need to initialize. Initialization, is we just need to copy this part and paste it over
here okay. So, our t, capital T is our independent variables, Y is our dependent variable. In this
case Y has to be replace with concentration C, which are dependent variables okay. So, those are
the only changes that we need to make okay. Let what, we shall do now is, just run that particular
code okay. Let us run this code and we are going to start getting errors right from over here.

300
So, let us go ahead and do that, without worrying too much about errors. What we will do over
here is, just change this end volume to V = 5 okay. And let us run this okay. We got an error,
undefined function of variable t0. Keep in mind, that our independent variable was V or V0. So,
that is what needs to be put over here. So, this will go from V0 up to Vend okay. That is our t in
steps of h. So, let us also say, what h is going to be. The h is going to be 0.1. So, N as got to be
vVend-V0, divided by h that is our n.

We have our y equal to zeroes (N+1, 1). And Y1 is the dependant variable in this case the
dependent variable is C0. So I will just replace this okay. And I will say initialization, solving
okay. And, what I we will do is plot. Plotting, results, plot (VSol, CSol) as blue line and (t, y) as
dash red line okay. And the other thing that, we need to do is replace this with an appropriate
function pfrFun okay. Let us clear all close all and run the runPFRode okay. And as you can see
the red line, the red dashed line lies exactly or almost exactly on top of the blue solid line.

The red line is our Heun’s method solver. Whereas, the blue line is our ode45 solver. So, as you
can see over here, so this is the overall code that we have okay. Using the ode45 solver the code
was very simple to obtain, using RK-2 Heun’s method if you recall, this was the main code that
we copy pasted from RK-2 Heun’s over here, this was the main code.

And in addition to that, we had to do some initialization, we had to initialize our h, we have to
initialize our independent variable T, we have to initialize our dependent variable Y. Once we did
that, we were able to solve this using RK-2 Heun’s method okay. (Video Ends: 10:17) Okay, so
what we have done is, revisited our RK-2 Heun’s method and generalized it, to use a general
function myFun and the pfr function pfrFun. Next, we are going to extend this to, RK-4 method.

(Refer Slide Time: 10:32)

301
The RK-4 method, is usually the most popular Runge-Kutta method. The RK-3 method uses
three weights w1 k1+w2 K2+w3 k3 whereas; RK-4 method also uses a fourth weight + w4 k4.
The RK-3 method has a local truncation error (h ^ 4). Whereas, RK-4 method has a local
truncation error (h ^ 5). The best rk5 method also has an error (h ^ 5). As a result of this, we do
not get a big advantage by using rk5 method instead of an RK-4 method. Therefore RK-4 method
is arguably, the most popular rk method out there okay.

(Refer Slide Time: 11:32)

302
We will solve the first problem y`= -2ty using RK-4 method. The RK-4 method is given over
here Y (i+1) = Y (i) + h / 6 * k1+2k2+2k3+k4 etcetera, etcetera okay. So, let us go to MATLAB
(Video Starts: 11:45) let us open our RK-2 Heun’s method. And save this as RK-4 standard okay.
Solve ODE-IVP using RK-4 using Standard RK-4 method.

The initialization part remains the same. Initialize the problem definition remains the same. The
initialization remains the same, solving using RK-4 method. k1 = this, k2 = myFun of something,
k3 = myFun of something, k4 = myFun of something and Y (i+1) = Y (i) +h /6 * k1. Let us go
here and see k1+2k2+2k3+k4. k1+2*K2+2*k3+k4. Our k2 was function (T (i) + h/2, Y (i) +
h*k1/ 2) okay.

I will just copy this and paste it in k3, and we will do new modification as required. k3 is T(i)
+h/2 again but Y(i)+h*k2/2. Instead of k1 /2, we now have k2 /2 okay. So, let us paste and the
only thing that needs to change, this k1 is to be changed to k2 okay. Same thing, we will paste
for k4. And let us see what change needs to be done. k1 as k4 sorry, is a function (T(i)+h, Y(i)
+hk3). So, let us paste it T (i) + h and not T (i) + h/2. And Y (i) + h*k3 and not divided by 2.

So, we will delete, change k1 to k3 and remove divided by 2. And this is what Y (i). Let us save
this and let us first clear everything and let us run our RK-4 standard method okay. And we see
the, same curve and the curve looks similar to what we had earlier. Let us check on errors or let
us check on max error, the max error is 7.5*10 ^ -6 as we see. When we went to RK-4 standard
method, there was significant drop in the error. With the error drop from around 2*10 ^ -3 to 7
into 10 ^ -6 okay. (Video Ends: 15:27)

So, for the same step size, RK-4 method is significantly more accurate than RK-2. And that is
one of the reasons for the popularity of RK-4 method okay. So with that, we come to the end of
this lecture. What we covered in this lecture were two things. First we improved our RK-2 code.
So, that we are able to use a generic function myFun (e, y) instead of hard coding it in inside the
code itself. Next, we extended our RK-2 Heun’s method to RK-4 standard method. So with that I
come to the end of lecture 7.4, and will see you in the next lecture. Thanks and bye.

303
MATLAB Programming for Numerical Computation
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #07


Lecture No. #7.5
Ordinary differential equations – Initial value problems – Error analysis of Runge Kutta
Methods
Hello and welcome to MATLAB programming for numerical computations. We are in week
number 7. In module 7 we are covering ordinary differential equations initial value problem. We
are in lecture 7.5. This is the last lecture of this module. In this lecture we are going to present
some error analysis of Runge-Kutta methods.

So far we have covered Euler’s method which can be considered to be first order Runge-Kutta
method. We have covered a couple of RK-2 methods. Specifically, Heun’s method and midpoint
method. And in the previous lecture, lecture 7.4 we have covered RK-4 method, standard RK-4
method. Today we are going to present error analysis of RK methods okay.
(Refer Slide Time: 01:02)

So first let us talk about the local truncation error. We will not demonstrate this in today’s lecture,
what I will show you is, with the global truncation error.
(Refer Slide Time: 01:20)

304
Now if we were to compare global truncation error instead of local truncation error, all of the
odes solvers they drop bit overall order of accuracy by 1 power of h. What that means is Euler’s
method, the global truncation error is h ^ of 1, Heun’s is h ^ 2, RK-4 is h ^ 4. Today’s lecture,
this is what we are going to demonstrate and compare. We are going to see how the error of
Euler’s method compares with that of Heun’s method and RK-4 method. Specifically we are
talking about the global truncation error.

(Video Starts: 02:02) Let us now go to MATLAB not to do this okay. What we will do is, we will
bring up the codes that we had written earlier. We had written RK-2 Heun’s method code, RK-4
standard method code and Euler’s explicit method code okay. So RK-2 Heun’s method which
uses myFun, which we had done in lecture 7.4.

And this was the function myFun okay. And then we have, we had the RK-4 standard method
okay. Which also use myFun. What I will do also is, open Euler’s explicit method okay. So this
was the Euler’s explicit method RK-2 and RK-4 methods. Euler’s explicit method also we are
going to change this instead of -2 t * y, I am going to use myFun (ti, yi) okay.

Exactly in the same way as we had done before, as we had done over here. That is what we have
done again with our Euler’s explicit method okay. Save this, okay. And what we will do is, we
will do a comparison of Euler’s explicit, RK-2 Heun’s and RK-4 standard method. Edit,

305
compareODEs. Compare global truncation error of Euler, Heun and RK-4 methods okay. What I
will do, I will copy this part, t0 is 0, y0 is 1.

Let us take tEnd = let us say 1 also. h we are going to keep varying so h, HALL, let us say is 0.1,
0.05, 0.01 and 0.005. Let us say these are the 4 h values that we have okay. for i = 1 :4, h =
HALL(i), n equal to what we had earlier tEnd - t0 / h okay. With this we are going to call Euler’s
explicit, RK-2 Heun’s and RK-4 standard method okay.

So what we are going to do is we will call the RK Euler’s and Heun’s method but we do not
require everything. We only require the Yend at tEnd, so Yend myEuler okay. And we are going
to call it with our y0, t0, h and N okay. So Yeuler is this Yheun equal to myHeun (y0, t0, h, N)
and Yrk4 = myRK4 (y0, t0, h, N) okay.

So let us go to Euler’s explicit and convert this into a function. Remember what we had said in
module 1 where, we discussed functions versus scripts. Scripts are something that we are going
to use in order to complete a particular task in MATLAB. We are going to use functions if we
want to compute something as a function of certain input variables or if we want to repeat a
particular task multiple number of times using parenthesis.

That is why we are going to convert all of these 3 odes solvers. Our Euler’s method Heun’s
method and RK-4 standard method into a corresponding functions okay. The first function is
myEuler okay. Function yEnd = myEuler sorry, compared odes myEuler (y0, t0, h and N). So we
do not need t0, we do not need y0, we do not need tEnd, do not need h and we do not need n. So
everything that was in problem definition.

We did not need. Why because the problem definition was given in the compare ode script. So
there is just 1 script which is the compare ode script myEuler, myRK2 and myHeun and myRK4
are going to be 3 functions okay. We will need to calculate tEnd also. So tEnd is going to be t0+
N*h okay. So that is something that we have added okay. We do not want plot or anything of that
sort. We want yEnd is nothing but Yend and that is the only thing that we need to do okay.

306
Likewise we are going to do the same thing with RK-2 Heun’s and RK-4 standard as well.
Before doing that let us go to compare odes and see whether this works or not. And ERR (1, i) =
abs (Yeuler-Ytrue). And the true value which I will again quoted over here, yTrue=exp (-tEnd^2)
okay. So let us just run this and see whether this works or not okay. We do get certain error. Ytrue
is undefined. Let us go and see why we are getting that Ytrue okay.

That is again, it is a matter of MATLAB being case sensitive and I was not careful about the
cases. I need to initialize ERR=zeroes (3, 4), 3 methods and we have our 4 errors okay. So let us
save and run this okay. So now we have our errors, the first row is the errors in Euler’s method.
So let us look at ERR, we only focus on row number 1 and these are the errors in Euler’s
method.

So let us actually have HALL up to 0.001 as well okay. And let us now do this for Heun and
RK-4 and likewise error (2, i) is for Heun, Yheun and RK-4 okay. I will quickly go to RK-2Heun
and save as myHeun okay. We remove this part and replace it with function yEnd= myHeun and
let us just copy paste this okay. And RK-4 standard also save as myRK4 okay. Function yEnd =
myRK4 and just paste this over here and delete this part okay.

Again from myEuler, we will take this tEnd and paste it in both of these functions okay.
Remember that is what we had done earlier okay. Now the next task is going to be delete the
output part and okay and save this. And likewise this also, we will do the same thing okay. So
what we have done is we have converted, I have gone carefully over how to convert explicit
Euler into myEuler.

A script which was explicit Euler, we have converted into a function that was myEuler. The
function takes the initial conditions step size and number of steps. And returns the value at the
end point. That is what all the 3 functions are intending to do okay. We use all of these functions
in our compare odes file for. And run it for different values of h from 10 ^ -1 to 10 ^ -3.

And using plot for comparison. And what we are going to do is we will plot log logarithm (H)
against logarithm (ERR) okay. And let us run this. I have certain error okay. That it should be

307
plot (HALL) and plot of log (HALL) and log (ERR). So these are 3 plots, this 1 is for RK-1 or
Euler’s method, this 1 is for RK-2 method, this 1 for RK-4 method.

All of these are straight lines though we are the way we are going to compute the slopes are LE =
log (err) and LH = log (h) sorry, log (HALL) okay. So our denominator is always going to be
denominator, is going to be LH (end) - LH 1 okay. Our numerator is going to be LE all the rows,
last column-LE all the rows first column. The slope is going to be numerator divided by
denominator okay.

So the slope for RK-2 method sorry, slope for Euler’s method is 1.0, slope for RK-2 method is
2.0 approximately and the slope for RK-4 method 4.0. So this demonstrates the order of accuracy
of the 3 RK methods from a global truncation error perspective. So we have the 3 straight lines
with slope of 1, slope of 2 and slope of 4 okay. (Video Ends: 17:15) So this was the discussion
about global truncation error.
(Refer Slide Time: 17:19)

Global truncation error is 1 order greater accurate than the global truncation error just like what
we had seen earlier in numerical integration. The global truncation error of RK-2 method gives
us the method as h ^ 2 accurate, RK-4 method is h2 ^ 4 accurate. What you can do as an
assignment is you take the previous example and demonstrate it with the midpoint rule and see

308
for yourself that like the Heun’s method midpoint also gives us order of x square accurate global
truncation error.

With that I come to the end of this lecture and indeed to the end of module 7. The module 7 we
considered ode initial value problem. We first started with Euler’s method considered both
explicit and implicit Euler’s method. Later we said that in this module we are going to focus on
explicit methods. Only in lecture 7.2 we introduced ourselves to Runge-Kutta second order
method and extended it to Runge-Kutta 4th order method in lecture in 7.4.

In today’s lecture, lecture 7.5 we did an error analysis of all the 3 methods. In lecture 7.4 we
covered ode 45 which is the most popular algorithm that is provided by MATLAB in order to
solve the ode problem. With that I come to the end of this module. I will see in module 8. Thank
you and bye.

309
MATLAB Programming for Numerical Computation
Dr.Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #08


Lecture No. #8.1
Practical aspects of ODEs – Multi variable ODE

Hello and welcome to MATLAB programming for numerical computations. We are in the last
week of this particular course. In this week we are covering module 8 practical aspects of solving
ordinary differential equations.
(Refer Slide Time: 00:26)

So what have we covered in module 7 is listed over here. We started with Euler’s method both
implicit and explicit method. We showed that Euler’s explicit method is going to be stable only
for a small range of step size. Thereafter we talked about Runge-Kutta methods. Then we talked
about MATLAB solver ODE45. However we considered problems in single variable only.

Finally in the last 2 lectures of that module we covered higher order Runge-Kutta methods and
error analysis. With higher order Runge-Kutta method we specifically covered the RK-4 method.
In this module we are going to cover extension to multivariable case. We are going to cover how
to solve difficult stiff ODEs before we go on how to solve the stiff ODEs we will cover what stiff

310
ODEs actually mean. And finally we will finish off with some practical examples in the last 2
lectures of this module.

(Refer Slide Time: 01:27)

So let us get started with an example. This is an example, a textbook example for numerical
methods courses. The overall equation as you know is given by this. This is a mass and we
displace this mass by a certain amount and there is a spring and there is a damper and because of
this displacement this particular mass is going to oscillate.

What we are going to consider is this particular model, the initial condition that is required is as
stated over here. At time t = 0, we have displace this mass by a distance of 1, the velocity is 0
and we release the mass. So this as you see is a second order ordinary differential equation. What
we need to do is to convert this into a system of first order ordinary differential equations. The
way we do this is we realize that dx /dt can be represented by another variable v and we can
write this as m dv /dt + cv + kx = 0.

So when we write that we are going to get 2 first order ODEs. The first equation rather is dx /dt =
v. The initial condition is at t=0 x is 1. At t=0, v is=0. We are going to convert this into matrix
differential equation and that is given over here.
(Refer Slide Time: 03:02)

311
We are going to define our vector y as x v. As we have been doing throughout this course, we are
going to define all our vectors as column vectors. That means we are going to have n rows and a
single column. In this case there are 2 variables and therefore we have 2 rows and a single
column and we have y is defined as x and v and dy / dt is the first guy is v. The second guy is –cv
+ kx whole thing divided by m. So let us go on to MATLAB and try to solve this problem.

(Video Starts: 03:44) Okay, so the first thing that we are going to do is to create a function that
will calculate dy for given values of y and t. springFun okay and as we have been doing so far.
function fval=massSpringFun (t, y) okay. If you recall, this line remains exactly the same.
Function for mass spring system using ODE45 okay. So what is the first thing that we are going
to do is we are going to extract our x and v.

So our x = y1, v = y2. Calculating our fval. fval1=v. And fval2, fval2 is going to be negative of
(cv + kx) / m. It is negative c * v + k * x / m okay. The other thing that we are going to do is we
need to define our fval as a column vector. So rather than writing f1=v and f 2=this right hand
inside, we are going to write (f2, 1) is this and (f1, 1) is this. We have not yet defined our
constant c, k and m okay. So let us go and do that. Define constants c = k = m =. Define dy / dt
okay.

312
(Refer Slide Time: 06:02)

So let us go to MATLAB and sorry, let us go to power point and see what values we have. So
this is what we are going to solve it for m=10, c=5, k=15. So m=10, k=15, and c=5. K=15 and c
=5. So we will just save this okay. So now let us make a driver function that runs this mass
spring damper system. So let us call this, edit solveMassSpring and it will create a new script. To
run the mass spring system okay.

And what do we have over here we have we need to give the initial conditions. The initial
conditions is y0=1,0. y 0=1;0 because that is our column vector. And we want to run this from
t=0 to t=10. So as we have done in the past t= (0 10) okay. And then what we need to do is we
need to use ODE45. So how do we solve using ODE45 tSolution, ySolution= ODE45 @ (t, y).

Remember this is going to be the syntax that we are always going to use for ODE45. (t, y) space
the name of the file that is massSpringFun. MassSpringFun (t, y) okay. We want to run this. Let
us call this as tSpan instead of t. To make it very clear that it is a span of times. (tSpan, y0) okay.
And finally we need plot. So plot (tSol, ySol). We actually need to solve to sorry, we actually
need to plot the locations versus time.

313
So the location is in all of the rows and it is going to be in the first column okay. And let me
comment solve using ODE45.Plot the results. Let us run this okay so we have run this and this is
an under damped mass spring damper system. We start with location at 1 and it oscillates before
it will finally go to steady state. Instead of 5, let us save it increase it to 50 and let us run the
system and see what we are going to get okay.

As you can as we increased the damping coefficient we do not get oscillatory response but we
get a response like this. What we will do next is we decrease the c instead of 5 or 50. We will
decrease it to 1 and see how the response is going to be. As you all might remember from your
physics classes. The response is going to become more oscillatory. So let us run this and see. As
you can see this compared to what we got at c=50 this is significantly more oscillatory.

So what we have done is the overall ODE45 the way it works for a single variable and multiple
variables is nearly exactly the same. You are you need to define the function exactly as we did in
single variable case. Name of the functions (t, y). t is going to be scalar as it was before.
However y is a vector. Keep in mind that y is going to be a column vector. We want this to be
column vector just for uniformity.

And it should return fval. In the previous module we have returned fval as a single value. In this
particular multivariable case we are going to written fval as a 2/1 vector. The size of fval has to
be the same as the size of our vector y. Why because fval returns dy /dt. What we are going to do
next is to what we, what to do if we have RK-4 method not single variable RK-4 but if we had to
do a multivariate RK-4.
(Refer Slide Time: 10:47)

314
Until this point, what I have covered is something that I want you to play pay close attention to.
Because that is something that you are going to use in multiple real scenarios. RK-4 method that
I am going to show in the next few minutes is just for demonstration purpose. It is just for you to
see how everything in Runge-Kutta method or in most of the numerical techniques follows
similar rules for multivariable case just the way we had for a single variable case.

The only difference is now instead of a single variable you have to be just a little bit careful in
tracking a multivariate case okay. And edit my RK-4 okay. So this was the solver that we had
generated in our previous module okay. So let us go ahead and change this power y0 is (1; 0). So
let us make that change y0 is (1; 0). Our t0 was 0 and tEnd is 10. So tEnd, let us change that from
5 to 10.

So this is our t0, this is our tEnd, this is our y0 okay. Let us keep our h=0.1 as before and our n as
before is going to be this. Now when we are to initialize the solutions, our time vector is going to
be just a single vector as we have over here okay. What we are going to do with y is that y, we
are going to denote this as a 2 by n+1 vector.

Rather y at times 0 will be in the first column, y at times 0.1 will be in the second column y, at
0.2 will be in the third column so on and so forth. So that is the reason why we are initializing y

315
in this manner okay. Next thing we are going to do is populate the initial value of y and we are
going to do that with (y:, 1).

What that means is all the rows in the first column are going to be populated by our initial
condition (1, 0) okay. Now solving using RK-4 method but we need yi. So yi we will say = yi
okay. So k1 is going to be equal to myFun now the name of the function has changed to
massSpringFun okay. So let me just copy this and paste it everywhere okay. So what I have done
over here is, I just said Yi=yi okay and then replaced all of Yi’s with yi okay.

There is going to be an error with this and I will come to that in a minute but let us write this as it
is okay. And our y i+1 we will just leave it as this okay. So let us keep this and let us run and try
to see what is going to happen. This is going to give us an error and that is because I have not
kept track of the fact that yi’s are now vectors. So let us run okay what we get is that index
exceeds matrix dimension and that is because over here.

We have taken our yi as scalar. If we type yi, yi is 1, what I should have been doing is to give the
first column of the capital y vector sorry, capital y matrix. So the first column is going to be :, i
when i=1 okay. So as you can see the thing that has changed from RK-2 sorry, from RK-4 single
variable to RK-4 multivariable is that instead of taking a scalar value, now I have to take that
entire column. That is 1 thing that has changed nothing really has changed significantly over
here.

The other thing that I will change is this, I will write this as yNew. I will write this as yNew=Y
(i) +h/6 into weighted some of case. And then I will write y all of the rows in the i+1th column
will be equal to yNew. And that is going to be what I am going to do and Yend that is y at the last
point is going to be the last column. Last column is all the rows in the end column and that is
going to be :, end that is going to be our y end and plot (t, y) okay.

So let us save this and run it and hopefully this will run without any error okay. And this is how
our solution looks like with RK-4 method. I have plotted x as well as the velocity. Let me just
plot x over here by saying the first row and all the columns. The first row has the location; the

316
second row has the velocity. So if I have to plot only the location, I am sorry, if I have to plot
only the locations then I will have to give this command.

And let me run this okay and this is the result that we get using RK-4. Let me click over here and
say hold on. So that this plot is held. And I will solve this using ODE45 and compare the results
by saying plot dashed red color line. So let us save this and let us run this let us look at the plot.
As you can see the solution using ODE45 falls exactly on top of the solution using RK-4.

And the reason it does that is RK-4 is a higher order method and its accuracy is similar to that
you will see in ODE45. (Video Ends: 17:50) Okay with that I come to the end of this particular
lecture. What I have covered primarily in this lecture is to extend how you will solve ODEs from
single variable to ODEs in multiple variable using ODE45. Thereafter I covered something that
is beyond the scope of this particular course.

What I covered is how you can simply extend single variable RK-4 method to a multiple variable
RK-4 method. But the take home message primary message from lecture 8.1 is if you want to use
MATLAB solvers ODE45 for multivariable case going from a single variable to multiple
variable case is fairly straightforward. With that I come to end of this lecture and I will see you in
the next lecture thanks and bye.

317
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #08


Lecture No. #8.2
Practical aspects of ODEs – Stiff systems and solutions using ode15s

Hello and welcome to MATLAB programming for numerical computations. We are in module 8
and this module we are covering practical aspects of solving ode initial value problem. In the
previous lecture we extended ode45 to a general multivariable case. In this lecture we are going
to take up examples where ode45 is going to find it very difficult to solve the problems. The
reason is ode45 is a Runge-Kutta solver which is an explicit solver.

An explicit solver will have numerical difficulties when face with what is known as stiff systems.
So what I am going to cover in this lecture is what stiff systems are and if ode45 fails how you
can instead use ode15s. What we are going to is we will first start of with the same problem that
we looked at in the previous lecture. So let us go to MATLAB and do that okay.

(Video Starts: 01:12) So this is the solveMassSpring example that we took earlier. What we have
done in the previous lecture is this. What we did was, we said that the way we are going to write
our functions that we are going to use for ode45 in multivariable case is going to be exactly same
as we did in the single variable case. The only difference is that 1 our y is an n by 1 vector, n
rows and a single column and our fval is also an n/1 vector, n rows and a single column again.
We solve this using ode45.

Now if we want to solve it using ode15s, I can show you how simple it is to solve using ode15s.
If we were to solve this using ode15s all we need to do is change ode45 to ode15s.That is all that
we need to do okay. I click on run okay and I get the results the same as what we had seen
before. So, take home message over here is that ode15s for an external user like us works in the
same manner as ode45.

318
If ode45 works then why do we need ode15s and the reason for this is ode45 may not work for
all the cases. So let us go on to power point and see okay. (Video Ends: 02:52)

So the cases where we need to look at ode15s is what is known as stiff systems.
(Refer Slide Time: 03:01)

So let us consider the first consider the ode x`= -100 * x1 okay. (Video Starts: 03:12) We can
solve this problem as (tSol, xSol) = ode45 @ (t, x) -100 * x1, negative 100 * x. Let us take the
tSpan for this as say (0:1). And let us and take the initial value as 1. Let us also plot (tSol, xSol).
And let us see what we get okay. When we solve (tSol, xSol), we see that the value of x or rather
the value of x1 drops rapidly and goes on to equal to 0 okay.

So within pretty much .01 seconds the xSol value has dropped to its steady state value of 0 okay.
So let us hold this. Now what we will do is will consider the other ode -100 * x. So (tSol, xSol)
sorry, not -100 * x, -0 .01 * x okay. So -0.01 * x is what we need to solve okay. And we will plot
this result as well. And let us look at the graph. As you can see in the red color line over here this
particular value remains nearly constant at 1.

And let us now solve the same problem again but instead of tSpan being from 0 to 1 let us make
tSpan as 0 to 3000 and then plot this and see what we are going to get. Let us look at that figure
okay. As you can see here the original the earlier graph goes from 1 to 0 within 0.1 seconds.

319
Whereas, the second system goes from 1 to 0 takes almost 500 seconds in order to do that.
(Video Ends: 05:30)
So when this coefficient is a very large value the system responds very quickly, if this coefficient
is a very small value then the system responds very slowly. Now the question is what if the 2
variables were together in the same equation. If we have dx in the same vector equation rather. If
we have dx1/ dt = -100*x1 and dx2/ dt = -0.01*x 2 what we are going to get is this.

So if we have an explicit solver such as Runge-Kutta solver and we have this system and this
system together as shown over here okay. We need to take really small steps because of this
particular equation. The steps that we need to take would be of the order of 0.001. But in order to
get the evolution of this particular equation. We need to solve this problem for a large number of
times which means we need to take a step of .001 but need to go almost to 100 or 1000 steps.

That is the reason why stiff systems are going to be difficult to solve. The reason is we need to
take small steps in order to ensure stability but we want to go to a large amount of time. (Video
Starts: 06:56) Let us see what happens when we try to solve this using ode45 and ode15s. edit,
multiVarFun. Let us create this function fval = multiVarFun (t, y). We will do a = (-100, 0) (0,
-0.01) that is our A matrix. And fval = a * y okay. So we have done this.

So now let us go and run this using ode45. (tSol, ySol) is going to be equal to ode45 @ (t , y)
multiVarFun(t, y). For tSpan from 0 to let us say 0 to 200 that should be sufficient for us to show
our show what we have. And for initial condition of 1 and 1 okay. And we have run this using
ode45 okay. Now MATLAB has solved as using ode45. Look at the number of time steps
MATLAB has taken.

It has taken 24157 times steps in order to solve this rather simple problem using ode45. So let us
plot (tSol, ySol) and see what we are going to get okay. This particular guy responds very
quickly whereas this guy is responding extremely sluggishly. Let us look at our vector tSol and
see what we get okay. So as you can see our solver is taking very, very small steps it started with
steps in the order of 10 ^ -4 but as things progress okay it could not take large steps.

320
It is still taking steps of the order of 10 ^ -3 as you can see over here. The reason why it needs to
take steps in the order of 10 ^ -3 is because an explicit method will become unstable if we take
larger steps. On the other hand ode15s is an implicit method and there is no restriction on the r.
Typically there is there is a very small restriction on that step sizes that ode15s will take. So let
us try to solve the same problem using ode15s.

Let us call this as t2 and ySol 2 = ode15s multiVarFun from (0 200) and initial condition is (1; 1)
okay. And let us solve this and let us plot (t2, y2) okay. As you will see (t2, y2) has the exact
same behavior as (tSol, ySol), using ode15s we are going to get the exact same behavior.
However, when you look at the Val, the number of points at which ode15s has run it has run only
at 88 points instead of 24000 points.

Let us click on our t2 and see the points at which it has run okay. Initially when our first guy our
x1 is still evolving. It the ode solver needs to take small steps in order to maintain accuracy. So
in order to maintain accuracy ode15s has taken small steps 10 ^ -4, 10 ^ -4, 10 ^ -4. Now, it has
started taking steps of the order of 10 ^ -3 but as we go further okay.

Now it has started taking steps of the order of 10 ^ -2. We go even further beyond say time 2 it
has started taking steps in the order of 1. Now it has started taking steps in the order of 10
accuracy dictates that the steps need to be taken in the order of 10 ^ -3 till the response of x1
stabilizes. But after the response of x1 stabilizes we do not have to worry about x1 we should be
able to take larger steps.

We are unable to take larger steps in an explicit solver because this is a stiff system but we are
able to take larger steps in an implicit solver which is ode15s. So the question is what the
definition of stiff system is. (Video Ends: 12:08) The working definition of stiff systems for this
course is going to be a stiff system is a system where you have a highly fast dynamics and highly
slow dynamics couple together in the same ode.
(Refer Slide Time: 12:25)

321
(Video Starts: 12:36) So question is does the stiff system needs to be diagonal or linear. So let us
take this particular example where a is -5.7, 1.85 okay, 13.2 and -4.3. And then let we click
evaluate selection so that now we have a okay. So now let us solve this using ode45 okay.
Everything else remains the same we are going to solve it using ode45 okay. And when we solve
it using ode45 we see that we take 2400 steps. We now solve it using so we now solve it using
ode15s.

When we solve it using ode15s let us run this ode15s and see how many steps ode15s takes.
Ode15s requires only 40 steps. Question is why is this happening? The reason why this is
happening is let us take the Eigen values of our A matrix. The Eigen values of a matrix are of the
order of 10 and of the order of 0.01 okay. So the ratio of the 2 Eigen values ans1/ans2 is a very
large number.

It is of the order well it is actually not very large numbers it is reasonably large number the ratio
of the Eigen values is of the order of thousand. So this matrix A is an NILL condition matrix.
(Video Ends: 14:01) We are going to have stiff system and it might be necessary to use ode15s
okay. Let us look at an example of a nonlinear system.
(Refer Slide Time: 14:12)

322
This is a solved example in MATLAB help files, this is known as a Van der Pol oscillator, this is
the overall equation x``- mu *(1-x ^2)* x `+x = 0 and these are the starting points okay. (Video
Start: 14:31) So let us go to MATLAB. Edit, myVDP okay. So function fval = myVDP (t, y). Let
us call mu = 1 okay. Our first guy is going to be dx / dt = x`. So our fval (1, 1) is going to be
nothing but so let us call x = y1 and v = y2 okay.

So, fval is nothing but dx/dt and dx/dt is equal to v and fval (2, 1) is going to be equal to square
mu *(1-x^2)*v-x. And now let us run our (tSol, ySol) = ode45 @ (t , y) myVDP (t , y) okay. Let
us run it from 0 to let us say 10 that should be sufficient with an initial condition as x0 = 2 and x’
0 = 0 so that is our initial condition okay. We solve this and let us also do plot (tSol, ySol) (1, :)
okay (tSol : , 1 ). So let us do this okay.

So this our Van der Pol oscillators solution for mu = 1 for mu = 1 Van der Pol oscillation.
Oscillator is not a very stiff oscillator and therefore we get the solution very quickly. So the first
practical tip is that when you want, when you know that the system is non stiff or you are not
sure about the stiffness of the system you should first use ode45.

If ode45 does not work or if you know that the system is a stiff system that is when you should
use ode15s. So let us make the system stiff by changing our mu to 100 and let us try to solve this
using ode45 okay. Let us solve it up to from 0 to 1000 and let us solve this. And let us see, we

323
will see that this particular system behaves rather sluggishly it takes a fairly long time in order to
solve okay.

I am still waiting for ode45 to solve and finally ode45 has solved this okay. It has taken almost 2
lakhs step. Let us now solve this using ode15s instead because this is the stiff system. As you
will see that solving this using ode15s is going to be much faster and instead of 2 lakh points it is
going to solved as in only 1725 points. And the result is the same as before.

Now if we were to increase the mu to 1000 and this is what your MATLAB help example gives
you. If you increase this to 1000 you will be not able to solve this problem using ode45. So let us
try to solve using ode45 and let us give this I have waited for nearly 1 minute and it has not yet
solved. So let me just cancel this by pressing control c. As you can see in a reasonable amount of
time ode45 has still not solved this problem okay.

Instead if we were solve this using ode15s, we are going to get the solution fairly quickly okay.
Let me solve this say up to 40 span = 5000 and then I will show why this is an extremely stiff
system. As you can see the system responds slowly at first till a certain point and at this point
that the system response is very rapid okay. So this is because of the overall stiffness of the
system.

Again you have slow time scale then a very rapid time scale, slow time scale then a very rapid
time scale. So as you can see over here in this practical example also you have case where, a
slow time scale is mixed with a fast time scale. (Video Ends: 19:40) So with that I come to the
end of this lecture.

What I have covered in this lecture ode45 and ode15s. Ode45 is a Runge-Kutta explicit solver. If
you know a priori that the system is the stiff system you want to use ode15s instead. If you do
not know whether the system is stiff or not, it is always a good idea to first try ode45. If ode45
does not work then you would switch to ode15s. With that I come to the end of this lecture and I
will see you in the next lecture.

324
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #08


Lecture No. #8.3
Practical aspects of ODEs - Method of Lines for Transient PDEs

Hello and welcome to MATLAB programming for numerical computations. We are in module 8
and this module, we are covering practical aspects of solving ordinary differential equations,
initial value problems. Specifically, we have been looking at odd odes with multiple variables. In
both the lectures, we had higher order odes initial value problems, which were converted into a
set of first order odes.

In today’s lecture, what we are going to cover is, how we can convert partial differential
equations, which are partial differential equation in time and space into ordinary differential
equations in time, using a method called method of lines. So, this was an example which we had
covered in lecture 4.5. In lecture 4.5, we had covered this in context of tri diagonal matrix
algorithm.

(Refer Slide Time: 00:55)

325
In that case, we had considered a steady state solution, in which dT /dt was equal to 0. However,
in case of transients, the model is going to look like this. So, consider, that we have a rod which
is 1 meter long. Let us say, that the rod initially is at the room temperature, which is 25 degree
Celsius. At time 0, we start heating one end of the rod and maintain that end at 100 degree
Celsius.

The other end, is maintained at room temperature of 25 degree Celsius. In addition to this, the
rod is also heat, losing heat to the surroundings. The heat loss is based on this particular term, the
heat conduction is based on this term, and overall the transients, how the temperature goes from
25 degrees Celsius uniformly in the rod to the final temperature profile with 100 degrees at one
end and 25 degrees at another end, is defined by this partial differential equation.

(Refer Slide Time: 02:20)

Now, how to solve this partial differential equation? One way to solve this is, what is known as
method of lines, as we had done in lecture 4 .5. In method of lines, what we are going to do is,
use central difference formula for differentiating in space. We leave the differential part in time
as it is. So, let us say, we are going to take that rod, which is 1 meter long and divide it into 10
intervals. We are going to have temperature, going from that initial point of 100 to the final point
of 25 in some way.

326
The central difference formula will result in at any point i d square t/ dx square is written in this
form. If the 1 meter rod is divided into 10 zones, out delta x is going to be equal to 1 /10. If we
have 10 zones or 10 intervals, we will have 11 nodal points. The first one is t= 100, the last one is
going to be t= 25 and for the all middle points from t2 to t10 we are going to have the equation,
this particular equation satisfied, once we substitute this guy over here.

(Refer Slide Time: 03:40)

When we do that the method of lines is going to yield us dT/dt equal to this equation for all the
center points and t1= 100 and t (n+ 1) = 25 at the 2 end points. Now what is changing with time
is, ti for (t2: tn). So, what we are going to do is, define the solution vector y as t2, t3, t4 and so on
up to tn. We do not need to solve for t1 or tn +1 because those values are already known to us. So
let us go on to MATLAB and try to do this problem okay.

(Video Starts: 04:22) Edit, let us call this rod conduction, rodcon or rodConduc and function
okay. As we have always been doing this, fval equal to file name rodConduc (t, y) okay. Getting
temperatures, t1 is going to be equal to 100. We know that okay. We also need to find out, what n
value is. So n is going to be nothing but length(y+ 1). Why is that so, because y goes from 2 to n.
Okay, so the length (y) is n- 1 and therefore n is going to be equal to length (y+ 1). t2 to n is
going to be equal to y and tn+ 1 is going to be equal to 25.

327
That ambient temperature, parameters, ambient temperature ta is 25 and tn+ 1. We will replace
this as ta. So, now we have our temperature vector that is completely defined okay. Let us define
dT /dt okay. dTdt is going to be equal to let us say zeroes (n+ 1, 1) okay. So, dT/ dt we are going
to again define for all of the ts. However there is no way to define dT1/dt and dT (n+ 1)/dt. So
we are going to initially make all of this but eventually we are going to discard dt1 and dT (n+1)
okay. So, we will do that in a bit okay.

We also need to define various other parameters. The other parameters, that we need to define are
alpha= 0.025 and beta= 0.1. We also need to get the step size h or delta x okay. If n is the number
of intervals, the step size is length / n, 1/ n. Why because the length of the rod is 1 meter okay.
So, now we have to do the final part and that is to define dT / dt and hence to define fval okay.

From i= 2: n, so, that is what we will write, so for i= 2: n. dTdt (i) is going to be equal to alpha
multiplied by this guy, the central difference formula. So alpha / h^2 * T (i+ 1) - 2 * T (i) + T (i-
1) okay. So, that is the first part, that is alpha / h ^2* T (i + 1) - 2*T (i) + T (i- 1) - beta multiplied
by something. So, we will write that part also -beta *T (i) - Ta okay. We also need to define,
alpha and beta which we have done already. We also need to define h that also has been done
already okay. So, that should complete our definition of dT/ dt.

Finally, we want to extract fval from dT / dt and to do that, we will say fval is going to be equal
to dTdt (2: n) okay. And that is our fval and thinks we have done over here. Let us save this okay.
In order to run this, runRodProblem. We will create a script, to run transient heat conduction in a
rod okay. Let us say, the number of intervals was 10 okay. Our initial values are going to be T (1)
= 100, T (2 : n) = 25 and T (n+ 1)= 25.

We also need to ensure that is the column vector. So let us write this as T (1, 1), T (2: N, 1) and T
(N+1, 1) okay. Let us call tSpan as (0:20) okay. And (tSol, TSol) solutions is going to be ode45
@ (t, y) and rod conduct (t, y). I think we should just put a space over here. We have tSpan and
our T0. Let me just write this also, as T0, to ensure that, we know that this means our initial
condition. Plotting results, so, what we want to do is, p let us plot tSol and ySol there are going
to be nine lines over here, but that is actually fine no problem.

We will just do that. While our T0 goes from (1; n+ 1), our y0 actually should go from T (2: n)
that was what our definition was. And this is the change that we need to make okay. And plotting

328
our result is going to be this okay. So, let us run this and we get an undefined variable that is
because the variable name that we have used over here. We have used the T0. So this also should
be changed to T0. So, let us save this and run this okay.

So, the point that is closest, to the hot end has reaches a temperature of around 85 degree Celsius,
after 20 minutes, the second point reaches a temperature of about 75 degree Celsius, some 62
degree Celsius so on so forth. So, this is the point closer to the hot end and this is the point closer
to the cold end. So, this is how the temperature varies from the initial value of 25 degree Celsius
to the final steady state value okay. (Video Ends: 12:46)

So, what we have done over here, is to use method of lines to convert a partial differential
equation with transients, into an ordinary differential equation in time. And we have used ode45
to solve the resulting ordinary differential equations. (Video Starts: 13:05) Finally, what I am
going to show over here is the power of ode45. So, let us say, instead of 10 intervals, if we were
to divide the overall domain in 100 intervals, let us just plot everything.

Let us say, we want to plot the middle point okay. So, let us just do that or midpoint. Let me just
say n/2. Let me just run this okay. It does take a little bit of time in order to solve, but finally it
has solved the problem and you will see, this is how the temperature varies at the middle of the
rod. If we want to check how the temperature varies at closer to the hot end of the rod. And we
also want to see how it varies closer to the cold end of the rod okay.

So, we are going to plot these multiple lines and let us see okay. So this is how the temperature is
varying closer to the hot end of the rod and this is how the temperature varies closer to the cold
end of the rod. (Video Ends: 14:47)

So, with that we finish this example, of using method of lines to solve of partial differential
equation. And indeed we come to the end of lecture 8.3. What we have done in this lecture, is
taken a partial differential equation and converted it into a series of ordinary differential
equations and solves the resulting ordinary differential equations using ode45. We also showed
that, the ode45 is a fairly versatile method and it is has, the ability to solve a really large set of
odes. With that, I come to end of lecture 8.3 and I will see you in the next lecture. Thanks and
bye.

329
MATLAB Programming for Numerical Computation
Dr Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #08


Lecture No. #8.4
Practical aspects of ODEs – Some practical Examples

Hello and welcome to MATLAB programming for numerical computations. We are in module 8.
In this module we are covering practical aspects of ordinary differential equations initial value
problem. We have looked at a several examples of ordinary differential equations initial value
problems from practical interest. This is the last lecture of this course and we are going to finish
of this course by taking a couple of practical examples that we started of this course with.
(Refer Slide Time: 00:46)

Before we do that let us go to the type of problems that we have considered in this module. The
first example that we considered is a damped spring-mass system model for which is given over
here okay. What we did was we wrote v = dx /dt and then we obtained the 2 odes of the form dx/
dt = v and dv /dt equal to this guy. This we wrote as a multivariate ode.
(Refer Slide Time: 01:16)

330
Thereafter we looked at the Van der Pol Oscillator. Again as before we wrote dx /dt = v and we
got 2 equations in 2 variables. These 2 equations we were able to solve for a low value of mu
such as mu = 1 we were able to solve using ode45. However for a fairly large value of mu such
as mu = 1000 this model becomes very stiff. And we need to resort to ode15s.

We saw what stiff systems means stiff system basically means a system where there are slow
dynamics and fast dynamics mixed together in the same ode set sequence of odes.
(Refer Slide Time: 02:05)

Then in lecture 8.3 we considered a transient heat conduction problem which results in a
parabolic partial differential equation. The partial differential equation was written in this form.

331
We used what is known as method of lines in order to convert the pde into ode in time. We did
that by writing d2 t / dx2 as a central difference formula.
(Refer Slide Time: 02:34)

As a result of this when we obtained the central difference formula at various locations in the
rod. Let us say from going from t2 to t10 these are the 9 equations that we got. The first equation
is dt2 /dt = alpha multiplied by the central difference formula - beta * t2- ta. Likewise we did the
same thing for t3, t4, t5 up to tn. That is the example that we considered in the previous lecture.
So this is what we covered in the previous lecture, lecture 8.3.
(Refer Slide Time: 03:07)

332
So, so far the practical examples that we have considered is the damped spring mass system, the
van der pol oscillator and transient heat conduction problem. These 2 were 2 equations, 2 odes
whereas, the transient heat conduction problem resulted in 9 odes. I also showed you that going
an increasing n can result in a larger number of odes which ode45 has no problems at all solving.
(Refer Slide Time: 03:37)

So finally in this lecture, we are what I call this is back to the beginning. Right in lecture 1.1, I
had given a motivating example. The example was Indian captain Mahendra Singh Dhoni hitting
a ball with an initial velocity of v0 at an angle given by theta0. And we need to find out where
the ball is going to land okay. So to setup the problem what we have is we have initial velocity
and initial angle and therefore we have the velocity in the horizontal component and in the
vertical component.

We are starting at x = 0, y = 0 with a velocity of 35 in this direction okay. In the vertical direction
we have gravity acting and in the horizontal direction we have air drag that is acting okay.
(Refer Slide Time: 04:29)

333
The acceleration due to gravity in y direction is given by d2y / dt2 = - g. And at air drag in x
direction will result in d2x / dt2 = - kappa * velocity u. Where u is the x component of velocity
and v is the y component of velocity. The initial velocity and initial location are given the initial
angle is also given because the angle is 45 degrees or pi /4 radiance, we get the horizontal
component of velocity is 35 * cos pi /4 and the vertical component is 35 * sin pi / 4.

The system of odes that we want to solve as before we are going to write dx /dt = u and dy /dt =
v. So we have dx / dt = u, dy /dt = v, d 2x / dt2 which is du /dt = - kappa u and dv / dt = - g. So
these are 4 odes that we need to solve okay. And the overall odes will look in the vectorial form
as shown over here. We will call this guy as vector capital y okay.

(Video Starts: 05:56) So let us go on to MATLAB and try to solve this problem. CricketFun and
as we have been doing function fval = cricketFun (t, Y) okay. Get variables small x is y1, small y
is y2, small u is y3, and small v is y4. Constants gravity is 9.81, the value of kappa we choose as
0.006 okay. This was the value that we had taken in lecture 1.1. So we will stick to that okay.

Describing dy / dt okay. So fval we are going to initialize that as a column vector we have 4 rows
in a single column dx/dt, dy/dt, du/dt, and dv/dt. So fval we will initialize as zeros (4, 1). fval (1)
=, fval (2) =, fval (3) =, and fval (4) = sorry, fval in brackets4 = okay. So what is dx/dt, dx/dt is

334
nothing but u, dy /dt is nothing but v okay. du / dt is, du /dt is -kappa * u,- kappa * u and dv / dt
is - g okay. And that is all there is to it in cricketFun. So we have saved this and now we will edit
1 more script file that will run this file for us.

So we will call as edit, let us call this as dhoniSixers okay. To solve trajectory of a ball hit with
initial velocity okay. And v let us see our vNet was 35. vNet = 35, theta= pi / 4. So our u0 is vnet
* cos theta and v0 is vNet * sin theta okay. We will also need to write x0 = 0 and y0 = 0 as well
okay. So we have all of these and let us initialize. The initialization is our y0 is going to be equal
to x0, y0, u0, v0 okay. So let us write this over here.

Initialization, executing ode. So we do not know what tSpan we need to use so let us say we are
going to use tSpan as (0 10) okay. So let us say tSpan is (0 10) and our (tSol, ySol) is going to be
equal to ode45 (@ (t , y )), the function name is cricketFun sorry cricketFun (t , y) again our
tSpan is (0 10) and y0 okay. Verifying the results. Verifying the results so what we want to do is
we want to plot (y, x) versus time. So let us do plot (tSol, ySol).

Let us plot all of that first and then we will see what is going to happen. So let us run this oops!
Undefined variable ySol because we need to have Y over there. So now let us run this and see
what happens okay. So the blue line is our x, the red line is our y. So y is indeed going to 0 or
going below 0. So what exactly is happening over here so what exactly is happening over here is
this.

Is that the ball trajectory it goes through a maxima and then it falls. Now in reality what happens
is when the ball is going to hit the ground the model is no longer going to be applicable because
that ball is going to bounce back. That particular bouncing motion is not incorporated in our
model. So model is correct only till the point that y > 0.

So it looks like at approximately 5 or so y value has been gone less than 0 right. So what we are
going to do next is we will just solve it for tSpan from 0 to 5 and let us run this okay. Yes, so now
we have got all the plots. Let us find out what is the value of ySol at the end, the value of vertical

335
location at the end. So ySol (end, 2). So that is at the last time, the second row the second
column, the second column represents this location y from in the vertical direction.

So when we see this okay that value is still 1.11. So let us go and do this solve this until tspan = 0
to 5.5. We will run this okay and we will again find out ySol so, ySol has gone below 0 so it is
gone substantially below 0. We do not want that to happen. So we will let us just try it up to this
point okay. And this is negative. So the ball has finally reached the ground at this particular time
okay.

So in order to verify the results what we want to check is we want to plot our x location and our
y location. So what we will do is to plot all the rows that means at all times the x location. And
we will also want to plot the y location. So 1 to 2 okay. So this is what we are going to plot. So
what that is going to give us is how the x location changes and how the y location changes. What
we will do further is we will show the property editor and we will ensure that the y axis is plotted
from 0 to the highest value okay.

Okay, so this is how the y location changes and this is how the x location is changing. So the ball
has indeed reached a point which is beyond 75 meters okay. So this is the way we are plotting
our x location and our y location. Let us say we wanted to and we have plotted this against the
time. So let us say we want to plot what the trajectory that ball takes.

And in order to do that we want to plot y in vertical axis and x in the horizontal axis. So what we
will do is that we will say xlabel time and ylabel location in meters okay. We want to make
another figure. So plot tSol sorry, not plot tSol plot (ySol). So that is the x location we want to
plot at all times. So all the rows and the first column will be the x location and we want to that x
location will be plotted on the x axis, y location will be plotted on the y axis.

So that is going to be (:, 2) and we want to plot that as circles okay. And xlabel is x in meters and
ylabel is y also in meters okay. Let us save this and let us run this okay. And this is how the ball
trajectory has taken. So this is how the ball goes when it was hit by Mahendra Singh Dhoni okay.

336
(Video Ends: 16:33) So let us go back to power point and see what we have done so far. So what
we have done today is we have taken the example that we started this particular course with.

This particular course we started with showing an example of Dhoni hitting the ball at a
particular angle, at a particular given velocity and we want to find the trajectory that the ball
takes okay. At that stage in lecture 1.1, I had said that I will cover this particular problem at a
later stage and today what we are doing is we are finishing of this particular course by taking up
the same problem and solving it with our ode solver ode45.

So with that I come to the end of this particular lecture and indeed into the end of this particular
lecture series. I hope you have enjoyed this lecture series and you have learned how to do
numerical computations using MATLAB. Thank you and see you later bye.

337
MATLAB Programming For Numerical Computations
Dr. Niket Kaisare
Department of Chemical Engineering
Indian Institute of Technology, Madras

Module No. #06


Lecture No. #6.4a
Tutorial on Regression
Hello since there have been several questions on regression linear or non linear regression on the
forum I thought I would make a quick tutorial today. So this tutorial will take up 1 example and
see what we mean by non linear regression and what we mean by linear regression okay.
(Refer Slide Time: 00:26)

So, the example that we take is let us say the model is r = a / b + t okay. a and be are the things
that are unknown and the data is collected through experiments for the values of r for different
values of t. So these are the values of t and these are the corresponding values of r.
(Refer Slide Time: 00:50)

338
Now as it will it might be obvious by looking at this okay. Right at the outside, this particular
functional non linear in a and b. Because it is non linear in a and b, we need to use non linear
regression method such as lsqnonlin. (Video Starts: 01:05) So let us go to MATLAB and solve
this using lsqnonlin. fun okay now what does this function.

What does this function supposed to return? So for that lets do help lsqnonlin okay. What we
want to do in non linear or linear least squares is find out the values of a and b such that the error
between this data and this model is minimized okay. If we see the help of lsqnonlin let us see
where it is, what it says is lsqnonlin attempts to minimize the square of fun.

So what should fun return? fun should return the errors so that a and b that minimized the sum of
square errors are formed. So function fErr = lsqFun and what should be the argument. We want
to find out the phi’s we want to find out a and b for which this value is minimized. So let us write
down the data. The data is t =10, 20, 30, 40 and 50 okay.

That the first thing I have said multiple times in this course is when we have a vector we want
those vectors to be column vectors. So multiple numbers of rows and single column. So,
therefore as return t and r we are going to transpose. Next is, get the parameters from the phi. So
get parameters okay. a is phi1 and b is phi2. Compute the error. In order to compute the error we
first need to compute r model and r model is given by this equation.

339
So rModel is given by a / b + t okay. a / b + t. Now t is a vector and therefore we need to do this
as ./ b + t. And our error which is fErr is nothing but the difference between r and rModel okay.
And we save this and we are done with the function for lsqFun okay. Next what we want to do is
say make out regress solve regressionSolving okay.

This is going to be our script. So for solving using lsqnonlin okay. So what do we need in order
to solve using lsqnonlin we want we need the data and we want the initial values. So let us say
the initial values were 10 and 10. So that is our initial values and our final solution is going to be
equal to lsqnonlin okay, function and phi0. So the function is going to be @ phi, (@p lsqFun (p),
phi (0)) okay.

And when we do that that is all we need to do we don’t need to do anything else. We run this and
we are get we got these values of phi. So, if we write down the phi, phi is 9.4 and 22.4. Now we
can also solve this using linear regression but not in the form that it is written.
(Refer Slide Time: 05:17)

In order to use linear regression it has to be converted into a form which is linear in the
parameters. To do that we can invert this model and we will get if we represent y=1 /r then we
will have y = a0 + a1 * t okay or rather a1*x. So linear least squares so our y is 1 /r and our x is t.
So our x is just t okay.

340
What did we do in linear regression is we created a matrix x okay. For in the case of y = (a0*1) +
(a1*x). We created the matrix x was ones (n, 1) okay that represent this. And x that was our x
matrix okay. So we are going to make an x matrix in a similar way. Our x matrix is going to be
similar ones ((n, 1), x). We need to also specify what n is going to be. And n is going to be length
(t) okay. philin for linear is going to be equal to (xT x)-1 * xT y.

That is going to be our philin. Once we get that phi we can calculate the values of a and b okay. a
is nothing but1/phi2. a is 1 and what is our b? b is going to be philin1*a. So let us save this and
let us run this okay. There is an error on line number 10, column 22 okay we have forgotten to
put this multiplication sign. So (xT x)-1 * xT y. So let us save this now let us run this undefined
variable r okay.

That is because we have not taken this data so let us copy this data and paste it over here. A
linear least squares and for solving using lsqnonlin. So now hopefully this should work. Linear
transformation and obtain original parameters. Let us save this and let us run this okay. And we
will see our phi is 9.4 and 22.4. Our a is again 9.4 and our b is 22.6. (Video Ends: 08:39)

So what we have done is solve the same problem using non linear regression as well as using
linear regression so hopefully this was helpful for you thanks.

341

You might also like