Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (3 votes)
392 views

Matlab

This document provides an introduction to MATLAB programming. It discusses starting a MATLAB session, defines key blocks of a MATLAB code including parameter definition, computation, and output blocks. It also demonstrates a sample MATLAB code that simulates the trajectory of a ball hit at an angle and velocity. Basic MATLAB data types and operations such as arrays, variables, and mathematical expressions are also introduced.

Uploaded by

TR ROHIT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
392 views

Matlab

This document provides an introduction to MATLAB programming. It discusses starting a MATLAB session, defines key blocks of a MATLAB code including parameter definition, computation, and output blocks. It also demonstrates a sample MATLAB code that simulates the trajectory of a ball hit at an angle and velocity. Basic MATLAB data types and operations such as arrays, variables, and mathematical expressions are also introduced.

Uploaded by

TR ROHIT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 134

INTRODUCTION

TO MATLAB PROGRAMMING
Lec 1.1: MATLAB Basics
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-1


About this Module

• We will cover the following topics


• MATLAB basics

• Arrays: Unlocking potential of MATLAB

• Loops and Execution Control

• MATLAB files: Scripts and Functions

• Program Output and Plotting


Starting and Exiting MATLAB

• We will go over starting a MATLAB session, layout of MATLAB window,


MATLAB editor, etc.
• Also see video “Getting Started with MATLAB” on MATLAB site
http://in.mathworks.com/videos/getting-started-with-matlab-68985.html
MATLAB Programming Example

Indian captain, Mahendra Singh Dhoni, hits a ball with initial velocity of 35
m/s and angle of 45○. If the boundary is at a distance of 75 m, will he score
a six?
• Setting up the problem:
• !"#$ = 35; *+ = !"#$ cos //4 ; !+ = !"#$ sin //4

• 4 5 = *; 6 5 = !

• * 5 = −8*; ! 5 = −9;
Result
MATLAB Code
%% Define Parameters and Initial Conditions
param.g = 9.81; % gravitational acceleration
param.kappa = 0.006; % air drag coefficient
u0 = 35*cos(pi/4);
v0 = 35*sin(pi/4);

%% Setting up and Solving the problem


X0 = [0; 0; % starting position is the origin
u0; v0]; % starting velocity is given
tSpan = [0 20]; % simulation time
[tOut, XOut] = ode45(@ballTrajectoryFun,tSpan,X0, [], param);

%% Displaying the results


figure(1);
plot(XOut(:,1),XOut(:,2),'bo');
xlabel('x (m)'); ylabel('y (m)');

%% Animating results
exitCode = ballAnimation(tOut,XOut);
MATLAB Code: Main Code Blocks
%% Define Parameters and Initial Conditions

Computation Input block


param.g = 9.81; % gravitational acceleration
param.kappa = 0.006; % air drag coefficient
u0 = 35*cos(pi/4);
v0 = 35*sin(pi/4);

%% Setting up and Solving the problem


X0 = [0; 0; % starting position is the origin
u0; v0]; % starting velocity is given
tSpan = [0 20]; % simulation time
[tOut, XOut] = ode45(@ballTrajectoryFun,tSpan,X0, [], param);

%% Displaying the results

Output block
figure(1);
plot(XOut(:,1),XOut(:,2),'bo');
xlabel('x (m)'); ylabel('y (m)');

%% Animating results
exitCode = ballAnimation(tOut,XOut);
MATLAB Code: Key Parts
%% Define Parameters and Initial Conditions
param.g = 9.81; Comment
u0 = 35*cos(pi/
Assignment

(Math) Expression

[tOut, XOut] = ode45(@bal

Calling a function
plot(XOu
Calling a function
MATLAB Code
%% Define Parameters and Initial Conditions
param.g = 9.81; % gravitational acceleration
param.kappa = 0.006; % air drag coefficient
u0 = 35*cos(pi/4);
v0 = 35*sin(pi/4);

%% Setting up and Solving the problem


X0 = [0; 0; % starting position is the origin
u0; v0]; % starting velocity is given
tSpan = [0 20]; % simulation time
[tOut, XOut] = ode45(@ballTrajectoryFun,tSpan,X0, [], param);

%% Displaying the results


figure(1);
plot(XOut(:,1),XOut(:,2),'bo');
xlabel('x (m)'); ylabel('y (m)');

%% Animating results
exitCode = ballAnimation(tOut,XOut);
Basic Data Types

• Matlab easily works with arrays


• Scalars, vectors and arrays

• Assigning variables

• Row vs. column vectors

• Arrays / Matrices

• Suppress “echo”

• Variables are case-sensitive


Basic Mathematical Expressions
Scalar Operations Special Variables
Variable Meaning
•+ - * / ^
pi Number /
• log, exp eps Machine precision
i Imaginary unit
• pow, sqrt inf Infinity
NaN Not a Number (e.g., 0/0)
• sin, cos, tan
ans Last displayed result
• asin, acos, atan end Last element of array
realmax Largest real number
• rem, round, ceil, floor intmax Largest integer
End of Lecture 1-1
12/17/15

INTRODUCTION TO MATLAB PROGRAMMING


Lec 1.1: MATLAB Basics
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-1

About this Module

• We will cover the following topics

• MATLAB basics
• Arrays: Unlocking potential of MATLAB
• Loops and Execution Control
• MATLAB files: Scripts and Functions
• Program Output and Plotting

1
12/17/15

Starting and Exiting MATLAB

• We will go over starting a MATLAB session, layout of MATLAB window,


MATLAB editor, etc.
• Also see video “Getting Started with MATLAB” on MATLAB site
http://in.mathworks.com/videos/getting-started-with-matlab-68985.html

MATLAB Programming Example

Indian captain, Mahendra Singh Dhoni, hits a ball with initial velocity of 35
m/s and angle of 45○. If the boundary is at a distance of 75 m, will he score
a six?
• Setting up the problem:

• !"#$ = 35; *+ = !"#$ cos //4 ; ! + = !"#$ sin //4


• 4 5 = *; 6 5 = !
• * 5 = −8*; ! 5 = −9;

2
12/17/15

Result

MATLAB Code
%% Define Parameters and Initial Conditions
param.g = 9.81; % gravitational acceleration
param.kappa = 0.006; % air drag coefficient
u0 = 35*cos(pi/4);
v0 = 35*sin(pi/4);

%% Setting up and Solving the problem


X0 = [0; 0; % starting position is the origin
u0; v0]; % starting velocity is given
tSpan = [0 20]; % simulation time
[tOut, XOut] = ode45(@ballTrajectoryFun,tSpan,X0, [], param);

%% Displaying the results


figure(1);
plot(XOut(:,1),XOut(:,2),'bo');
xlabel('x (m)'); ylabel('y (m)');

%% Animating results
exitCode = ballAnimation(tOut,XOut);

3
12/17/15

MATLAB Code: Main Code Blocks


%% Define Parameters and Initial Conditions

Input block
param.g = 9.81; % gravitational acceleration
param.kappa = 0.006; % air drag coefficient
u0 = 35*cos(pi/4);
v0 = 35*sin(pi/4);

Computation
%% Setting up and Solving the problem
X0 = [0; 0; % starting position is the origin
u0; v0]; % starting velocity is given
tSpan = [0 20]; % simulation time
[tOut, XOut] = ode45(@ballTrajectoryFun,tSpan,X0, [], param);

%% Displaying the results

Output block
figure(1);
plot(XOut(:,1),XOut(:,2),'bo');
xlabel('x (m)'); ylabel('y (m)');

%% Animating results
exitCode = ballAnimation(tOut,XOut);

MATLAB Code: Key Parts


%% Define Parameters and Initial Conditions
param.g = 9.81; Comment
u0 = 35*cos(pi/
Assignment

(Math) Expression

[tOut, XOut] = ode45(@bal

Calling a function
plot(XOu
Calling a function

4
12/17/15

MATLAB Code
%% Define Parameters and Initial Conditions
param.g = 9.81; % gravitational acceleration
param.kappa = 0.006; % air drag coefficient
u0 = 35*cos(pi/4);
v0 = 35*sin(pi/4);

%% Setting up and Solving the problem


X0 = [0; 0; % starting position is the origin
u0; v0]; % starting velocity is given
tSpan = [0 20]; % simulation time
[tOut, XOut] = ode45(@ballTrajectoryFun,tSpan,X0, [], param);

%% Displaying the results


figure(1);
plot(XOut(:,1),XOut(:,2),'bo');
xlabel('x (m)'); ylabel('y (m)');

%% Animating results
exitCode = ballAnimation(tOut,XOut);

Basic Data Types

• Matlab easily works with arrays

• Scalars, vectors and arrays


• Assigning variables
• Row vs. column vectors
• Arrays / Matrices

• Suppress “echo”

• Variables are case-sensitive

5
12/17/15

Basic Mathematical Expressions


Scalar Operations Special Variables
Variable Meaning
•+ - * / ^
pi Number /
• log, exp eps Machine precision
i Imaginary unit
• pow, sqrt inf Infinity
NaN Not a Number (e.g., 0/0)
• sin, cos, tan
ans Last displayed result
• asin, acos, atan end Last element of array
realmax Largest real number
• rem, round, ceil, floor intmax Largest integer

End of Lecture 1-1

6
12/17/15

INTRODUCTION TO MATLAB PROGRAMMING


Lec 1.2: Array Operations
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-1

Arrays are the most powerful aspect of MATLAB

• We will learn

• Building arrays
• Colon notations
• Array operations and functions

• Also view “Working with Arrays in MATLAB” on MATLAB website:


http://in.mathworks.com/videos/working-with-arrays-in-matlab-69022.html

7
12/17/15

Building Arrays
Array Building Functions
• Recall that we can build arrays as: Command Meaning
ones(m,n) Build m×n matrix of 1’s
>> A = [1, 2; 3 4]; zeros(m,n) Build m×n matrix of 0’s
eye(n) Identity matrix
diag(vec) Create diagonal matrix
• We can also build arrays from diag(A) Diagonal elements of A
rand(m,n) Uniform random number array
existing arrays (if correct size):
randn(m,n) Gaussian Random number array
>> B = [b, c]; magic(m) Magic square matrix
hilb Hilbert matrix

Basic Mathematical Expressions


“Scalar” Operations Matrix Operations
•+ – * / ^
• log, exp • logm, expm

• power, sqrt • mpower, sqrtm

• sin, cos, tan • sum,prod,cumsum,cumprod

• asin, acos, atan • min, max, mean, std

• rem, round, ceil, floor • length, size, eig

8
12/17/15

Basic Mathematical Expressions


“Scalar” Operations Matrix Operations
•+ – .* ./ .^ •+ – * / ^
• log, exp • logm, expm

• power, sqrt • mpower, sqrtm

• sin, cos, tan • sum,prod,cumsum,cumprod

• asin, acos, atan • min, max, mean, std

• rem, round, ceil, floor • length, size, eig

End of Lecture 1-2

9
12/17/15

INTRODUCTION TO MATLAB PROGRAMMING


Lec 1.2b: Array Operations Revisited
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-1

Tapping some Array Operations in MATLAB


• Also view “Working with Arrays in MATLAB” on MATLAB website:
http://in.mathworks.com/videos/working-with-arrays-in-matlab-69022.html

• Consider the following example (Marks earned by students)

Name Math Programming Thermodynamics Mechanics


Amit 24 44 36 36
Bhavna 52 57 68 76
Chetan 66 53 69 73
Deepak 85 40 86 72
Elizabeth 15 47 25 28
Farah 79 72 82 91

10
12/17/15

Some things to try


• Create a 6×3 matrix allMarks to contain marks for first three courses

• Append marks for the Mechanics course to allMarks when received

• Do the following computations

• Mechanics course was out of 50. Scale the marks to half


• Extract row 3 and give the marks to Chetan. Also calculate his total marks
We will use matrix fundaes for this:
• Extract marks of our best students, Deepak and Farah for first three courses
< = 2< 0.1=
> ? 2 0 = 2> 0.1?
• Calculate average marks obtained in each of the four courses
@ A 0 0.1 2@ 0.1A
• Scale all the marks out of 10*

End of Lecture 1-2b

11
12/17/15

INTRODUCTION TO MATLAB PROGRAMMING


Lec 1.3: Loops and Execution Control
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-1

Various Loops in MATLAB


• For Loop (commands below will • While Loop (commands below will
execute 10 times) execute if the condition is true)
for i=1:10 while i<10
<statement 1>; <statement 1>;
⋮ ⋮
<statement n>; <statement n>;
end i=i+1;
end

12
12/17/15

When to use For Loop

• For loop is used when a set of operations are to be repeated a


specific number of times
• Examples

• Find first 10 terms of Fibonacci series


• Find factorial of a number G
• …

When to use While Loop

• While loop is used when a set of operations is to be repeated if a certain


condition is met
• Find all terms of Fibonacci series less than value 200

• Location of a ball thrown upwards is given by 6 = ! +H − IJ9H K. Calculate the


location of the ball for every 0.1 seconds until it reaches the ground (i.e., 6 > 0)

13
12/17/15

MacLaurin Series

• Calculate approximate value of @ +.M using the infinite series:

<K <Q <R


@N=1+<+ + + +⋯
2! 3! 4!
These calculations are to be performed with 2 to 7 terms in the series

End of Lecture 1-3

14
12/17/15

INTRODUCTION TO MATLAB PROGRAMMING


Lec 1.4: Working with Files – Scripts & Functions
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-1

Working with MATLAB files

• Type “edit <fileName>” at the command prompt to open MATLAB


code editor with the file fileName.m.
• MATLAB files are of two types: Scripts and Functions

• More help from MATLAB website on “Writing a MATLAB Program”:


http://in.mathworks.com/videos/writing-a-matlab-program-69023.html

15
12/17/15

MATLAB Files: Scripts vs. Functions


• Scripts • Functions
Files containing sequence of Files that take certain input(s),
MATLAB commands executes sequence of steps, and
returns output(s) at the end
• MATLAB statements are executed • MATLAB statements are executed
as if typed on command prompt in function’s own variable space

Scope of Variables

• script shares the variables with workspace from where it was called

• Typically, that means MATLAB workspace

• function has its own workspace

• Variables used in a function have local scope

• Functions “talk” through input and output variables:


[out1,out2,...] = function fcnName(in1,in2,...)

16
12/17/15

Script and Function Examples:

• Write a script to calculate factorial • Write a function to calculate


G! = 1×2×⋯×G A = >+ + >T 4 + >K 4 K + ⋯ + >" 4 "

Note: Such functions are commonly used to


calculate physical properties of fluids.
Today, we will consider a simple case of:
> + = 1, > V = 1/W

When to use Scripts vs. Functions (beginners)


• Use scripts when you want to…
• Make small calculations (e.g., factorial, plotting, basic computing etc.)
• Use functions when you want to…
• Calculate values (r) as a function of variables (t,y,…): X = A(H, 6, … )
• Pass on the function values to MATLAB function for solving something; e.g.,:
\]
= A H, 6 à function dy = myODEfun(t,y)
\$
<...>ode45(@myOdefun, <...>)
• Calculate properties as a function of temperature, concentration, current, etc.

• All other purposes, you are likely to use scripts (instead of functions)

17
12/17/15

INTRODUCTION TO MATLAB PROGRAMMING


Lec 1.5: Plotting and Output
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-1

Various forms of output


• Display on the screen
• Variables will echo if command ends without semicolon

• Other options…

• Plotting data
• Using plot command

• Other options…

• More help from MATLAB website on “Using Basic Plotting Functions”


http://in.mathworks.com/videos/using-basic-plotting-functions-69018.html

19
12/17/15

Displaying on the screen

• Recall various methods we used in this module:


• Echo result on screen: >> b = [1, 2; 7 1];
• Using disp command: disp(b)
• disp some text: disp(‘Hello world’)

• More “beautiful” output:


disp([‘Factorial value is ’, num2str(factValue)])

• More advanced output using fprintf:


fprintf('Factorial Value is: %4i\n',factValue)

Plotting

• Consider the example of a ball thrown vertically upwards

• Plot location vs. time


• Labeling the axes

• Other plotting options

• Plot-ting multiple lines

• Log-Log plot

20
12/17/15

End of Lecture 1.5

MODULE – 1
INTRODUCTION TO MATLAB PROGRAMMING
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-1

21
12/17/15

Summary of Module-1

• MATLAB basics

• Familiarized with MATLAB command window and editor


• Variables: scalars, vectors and arrays
• Mathematical operations: both scalar and matrix operations

• Arrays: Unlocking potential of MATLAB

• Array operations vs. elemental operations


• Using arrays for more efficient use of MATLAB

Summary of Module-1

• Execution control
• for and while loops
• if-then statements

• MATLAB files

• Scripts and Functions


• When to use scripts vs. functions

• Plotting in MATLAB

22
ERRORS AND APPROXIMATIONS
Lec. 2.1: Errors in Numerical Methods
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-2

Maclaurin Series for ex

• Maclaurin series until nth order term:

&' &* &-


!" = 1 + & + + + ⋯+
2! 3! .!
• Compute ! /.1 and compare with actual value

• Error: 2 = 3 4567 − 3 "::5;<

• More number of terms è lower error


Machine Precision

• “Least count of a ruler”:


• 0.1 cm is the minimum resolution of the ruler

• A Vernier Caliper also has a “least count”


• Better precision than a ruler

• Just like these devices, real numbers also have a “least count”

• “Machine Precision”: Depends on # of bytes to store a real number

Example

• Decimal example from “Computational Techniques” course


http://nptel.ac.in/courses/103106074/2

n
0. x x x x x ×10
• Floating Point representation of a number:
2
• Example: 23.217 becomes 0. 2 3 2 1 7 ×10

• 23.218 becomes 2
0. 2 3 2 1 8 ×10
• But, 23.2172 remains 2
0. 2 3 2 1 7 ×10
• Thus, the least count of this decimal machine: ~ 0.00001 3
End of Lecture 2.1

ERRORS AND APPROXIMATIONS


Lec. 2.2: Truncation and Roundoff Errors
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-2


The Taylor’s Series

• Taylor’s series:

ℎ' AA ℎ-
? 3+ℎ =? 3 + ℎ? A 3 + ? 3 + ⋯+ ? - 3 + B ℎ-C1
2! .!

• As seen in Computational Techniques (http://nptel.ac.in/courses/103106074/):


Accuracy depends on how many terms used to derive numerical scheme

Maclaurin Series for ex

• Maclaurin series until nth order term:

"
&' &* &-
! = 1+&+ + +⋯+ + B &-C1
2! 3! .!
• The last term implies error in retaining only finite number of terms of the series
MacLaurin Series from 1 to 5
&' &
= & ×
• 1 term: ! " = 1 + & + B &' 2! 2

"D
• 2 terms: ! " = 1 + & + '! + B &* &*
=
&'
×
&
3! 2! 3
"D "F
• 3 terms: ! " ≈ 1 + & + '! + *!

" "D "F "G


• 4 terms: ! ≈ 1+& + '! + *! + H!

"D "F "G "I


• 5 terms: !" ≈ 1 + & + + + +
'! *! H! J!

Truncation Error in ea

-2
10

• More terms è Lower error


-3
10

-4
10

• Smaller & è Lower error 10


-5
error

-6

• log (2) vs. log (&) è Linear rise


10

-7
10

• Slope = . + 1 10
-8

-9
• Slope is the order of accuracy!!
10

-10
10 -2 -1
10 10
step, a
End of Lecture 2.2

ERRORS AND APPROXIMATIONS


Lec. 2.3: Round-Off Errors and Iterative Methods
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-2


Numerical Differentiation

• From definition of ? A 3 :
? 3 +ℎ −? 3 ? 3+ℎ −? 3
? A 3 = lim ⇒ ? A 3 ≈
S→/ ℎ ℎ

• From Taylor’s Series:


ℎ' AA ? 3 +ℎ −? 3 ℎ
? 3 + ℎ = ? 3 + ℎ? A 3 + ? 3 + ⋯ ⇒ ? A 3 = + ? AA V
2 ℎ 2

B ℎ

Truncation vs. Roundoff Errors

• Numerical Differentiation:
? 3+ℎ −? 3
?A 3 = +B ℎ

• Truncation error decreases with ℎ
• However, machine precision may determine how small ℎ we can use
• Roundoff error increases with ℎ
Direct vs. Iterative Methods

• Direct methods:
An algorithm (sequence of operations) to “directly” compute a solution
"D "W
• MacLaurin Series: ! " ≈ 1 + & + '!
+⋯+ -!
X "CS YX "
• Numerical derivative: ?A & ≈ S

• Iterative methods:
Improve an initial guess by repeatedly using computational steps until “convergence”

Iterative Method: Compute 2

• Héron’s Algorithm: One of the first iterative numerical algorithms


• Computes 2 starting with an initial guess and iteratively using the expression:

ZC1
1 Z
2
3 = 3 +
2 3 Z

• When do we stop and say we have a solution?


End of Lecture 2.3

ERRORS AND APPROXIMATIONS


Lec. 2.4: Step-wise Methods & Error Propagation
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-2


Multiple use of Taylor’s Series

• Again consider Taylor’s Series:


A SD AA
? 3 + ℎ = ? 3 + ℎ? 3 + ? 3 +⋯
'

• Multiple use of Taylor’s series:


3 = &, & + ℎ, & + 2ℎ, & + 3ℎ, ⋯ , & + \ℎ

• Use Taylor’s series 10 times with step size of 0.01 to obtain ? 0.1 :
& = 0, ℎ = 0.01, \ = 10

Example: e0.1
Single Term of Taylor’s Series Error decreases as \ increases

• ! <CS ≈ ! < + ℎ! < • Values of ! /.1:


• ! /./1 = ! / 1 + ℎ • With ℎ = 0.1: 1.1

• ! /./' = ! /./1 1 + ℎ • With ℎ = 0.01 (10 steps): 1.1046

• ⋮ • With ℎ = 0.001 (try yourself): 1.1051

• True Value: 1.1052


• ! /.1 = ! /./^ 1 + ℎ
Example: e0.1
Single Term of Taylor’s Series Two Terms of Taylor’s Series

• ! <CS ≈ ! < + ℎ! < • ! <CS ≈ ! < + ℎ! < + _Dℎ' ! <


• ! /./1 = ! / 1 + ℎ • ! /./1 = ! / 1 + ℎ + 0.5ℎ'

• ! /./' = ! /./1 1 + ℎ • ! /./' = ! /./1 1 + ℎ + 0.5ℎ'

• ⋮ • ⋮

• ! /.1 = ! /./^ 1 + ℎ • ! /.1 = ! /./^ 1 + ℎ + 0.5ℎ'

Global Truncation Error

• Local Truncation Error:


• Error in obtaining ? 3 with single application of numerical scheme
• Recall from last lecture for ! < : abc ∝ ℎ-C1

• Global Truncation Error


• ? & + 2ℎ affected by

• error in ? & + ℎ value, and

• error in ? & + ℎ → ? & + 2ℎ

• Multi-application of Taylor’s Series: GTE is error in obtaining ? & + \ℎ ∼ B(ℎ- )


Summary

• Machine precision: “least count”

• Definitions of error:
• True error: 2 Z = 3 4567 −3 Z

• Current error: ! Z = 3 Z −3 ZY1

• Notes:
• We used “true error” to analyze errors and approximations in this module
• We used “current approximation error” for stopping an iterative method

Summary

• Direct Methods
X "CS YX "
• Numerical Derivative: ?A & ≈ S
direct application

• Iterative Methods
ZC1 1 Z '
• Héron’s Algorithm: 3 =' 3 +< f until convergence

• Step-wise Methods
• Multi-step Taylor’s series: ! <f CS ≈ ! <f + ℎ! <f until we reach destination
End of Lecture 2.4
NUMERICAL DIFFERENTIATION
Lec. 3.1: Differentiation in Single Variable
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-3

Numerical Differentiation

• From definition of ! " # :


! #+ℎ −! #
! " # = lim
(→* ℎ
! #+ℎ −! #
!" # ≈

• From Taylor’s Series:
ℎ/ "" ! #+ℎ −! #
! #+ℎ =! # + ℎ! " # + ! # ⇒ ! " # = +3 ℎ
2 ℎ
First Derivatives
• Forward Difference Formula:
! #+ℎ −! #
!" # = +3 ℎ

• Central Difference Formula:
! #+ℎ −! #−ℎ
!" # = + 3 ℎ/
2ℎ
• Backward Difference Formula:
! # −! #−ℎ
!" # = +3 ℎ

Example
tan −1 (1+10 −4 ) − tan −1 (1) ε = 5e-5
−4
10
"d −1 %
$# tan (x)'& tan −1 (1+10 −4 ) − tan −1 (1−10 −4 )
dx x=1 ε = 2e-9
2 ×10 −4

tan −1 (1) − tan −1 (1−10 −4 )


ε = 5e-5
10 −4
The Choice of ℎ in Differentiation

• Order of accuracy:
• Previous example was done for ℎ = 1067

• Repeat for a range of values, ℎ = 10 68 ,10 6/ ,⋯ , 106;

• Make a log-log plot

• Recall Module 2: Tradeoff between Truncation and Roundoff Errors

• Repeat for a range of values, ℎ = 10 68 ,10 6/ ,⋯ , 1068*

Example-2 (for Practice)

• Example from: Computational Techniques (Module-6, Part-2)


http://nptel.ac.in/courses/103106074/21
! # = 2 − # + ln #
• True solutions
1 1
!" # = − 1, ! "" # = −
# #/
End of Lecture 3.1

NUMERICAL DIFFERENTIATION
Lec. 3.2: Differentiation in Single Variable
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-3


Higher Derivatives

• Second derivative (central difference)


f ( xi +1 ) − 2 f ( xi ) + f ( xi −1 )
f ʹʹ( xi ) =
h 2
+ O h2 ( )

• Third derivative (central difference)

f ( xi + 2 ) − 2 f ( xi +1 ) + 2 f ( xi −1 ) − f ( xi − 2 )
f ʹʹʹ( xi ) =
h 2
( )
+ O h2

Example-1

• Consider again: ! # = tan68 #


• True solutions
1 2#
!" # = , ! "" # = −
1 + #/ 1 + #/ /

• Solve the above using central differences to find ! " # and ! "" #
Example-2

• Example from: Computational Techniques (Module-6, Part-2)


http://nptel.ac.in/courses/103106074/21
! # = 2 − # + ln #
• True solutions
1 1
!" # = − 1, ! "" # = −
# #/
• Solve the above using central differences to find ! " # and ! "" #

Physical Example

• Consider:
6B
? = @A CD E8./;, @ = 1000, BC = 2500

• To find ? " at T = 600 using Central Difference Formula


• Choose ℎ~ I ×10 6K

• Try with other values of ℎ


8/R
• Optimal value of ℎ ∝ MNOP(
End of Lecture 3.2

NUMERICAL DIFFERENTIATION
Lec. 3.3: Partial Derivatives
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-3


Physical Example

• Consider:
6B
? = @A CD E8./;, @ = 1000, BC = 2500

• To find ? " at T = 600 using Central Difference Formula


• Choose ℎ~ I ×10 6K

• Try with other values of ℎ


8/R
• Optimal value of ℎ ∝ MNOP(

A Function of Multiple Variables


• Consider the following function:
#
! S = sin #8 exp −#/ , # = #8
/
• Since f is a function of two variables:

X!
= cos #8 A 6[\
X#8
X!
= − sin #8 A 6[\
X#/
A Function of Multiple Variables
• Consider the following function:
#
! S = sin #8 exp −#/ , # = #8
/
• The partial differential (è gradient in Transport Phenomena):

X! X!
]! S = = cos #8 A 6[\ − sin #8 A 6[\
X#8 X#/

“Jacobian” for a vector-valued function ^ S


• For a _-dimensional vector S, and `-valued vector function ^ S :

X!8 X!8 X!8



X#8 X#/ X#c
!8 S
X!/ X!/ X!/
! S ⋯
^ S = / b = X#8 X#/ X#c

!N S ⋮ ⋮ ⋱ ⋮
X!N X!N X!N

X#8 X#/ X#c
Practice Problem

• For the reaction rate:


f
? = @ exp − E8./;
gI
hi hi
find b = hD hj

hi k8 ℎ k8 ℎ
• To obtain è compute r at: k + 8 and k − 8
hD / 0 / 0
hi k8 0 k8 0
• To obtain è compute r at: k + and k −
hj / ℎ/ / ℎ/

End of Lecture 3.3


NUMERICAL INTEGRATION
Lec. 3.4: Newton-Cotes Integration Formulae
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-3

Numerical Integration

• Integration is area under a curve

Single application
Trapezoidal Rule
Simpson’s 1/3rd Rule
Simpson’s 3/8th Rule
Numerical Integration

• Integration is area under a curve

• Single application
• Trapezoidal Rule

Simpson’s 1/3rd Rule


Simpson’s 3/8th Rule

Numerical Integration

• Integration is area under a curve

• Single application
• Trapezoidal Rule

• Simpson’s 1/3rd Rule

• Simpson’s 3/8th Rule


Newton Cotes Integration Formulae

• Trapezoidal Rule:
On( (
∫O ! # m# = / ! k + ! k + ℎ

• Simpson’s 1/3rd Rule:


On/( (
∫O ! # m# = R ! k + 4! k + ℎ + ! k + 2ℎ

• Simpson’s 3/8th Rule:


OnR( R(
∫O ! # m# = ! k + 3! k + ℎ + 3! k + 2ℎ + ! k + 3ℎ
p

Local Truncation Errors

• Local Truncation Errors for single application of Newton Cotes Formulae:

Method Formula LTE


Trapezoid (
! k + ! k + ℎ 3 ℎR
/
Simp 1/3rd (
! k + 4! k + ℎ + ! k + 2ℎ 3 ℎ;
R
Simp 3/8th R(
! k + 3! k + ℎ + 3! k + 2ℎ + ! k + 3ℎ 3 ℎ;
p
Example

• Consider example from Computational Techniques (Module 6, Part 3)


http://nptel.ac.in/courses/103106074/22:
! # = 2 − # + ln #
• For this function,

#/
r ! # m# = # − + # ln #
2
• Use Trapezoidal and Simpson’s 1/3rd Rules and compare with true value

End of Lecture 3.4


NUMERICAL INTEGRATION
Lec. 3.5: Multi-Step Trapezoid/Simpson’s Rules
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-3

Multiple Applications of Trapezoidal Rule

• Multiple application of Trapezoidal Rule:


• For Interval-1:
(
s8 = / ! k + ! k + ℎ

• For Interval-2:
( 1 2 … … n
s/ = /
! k +ℎ + ! k + 2ℎ
a b
a+h
• And so on… è s = s8 + s/ + ⋯ sc
t6O
ℎ=
c
Multiple Applications of Trapezoidal Rule

• Multiple application of Trapezoidal Rule:


• For Interval-1:
(
s8 = / !If we write: !
k + ! k + ℎk = !8 , ! k + ℎ = !/, … , ! v = !cn8
• For Interval-2:

s/ =
(
! k +ℎ
s+= ! + 2 !/ + ⋯ + !c + !cn8
! 2k +82ℎ
1 2 … … n
/
a b
a+h
• And so on… è s = s8 + s/ + ⋯ sc
t6O
ℎ=
c

Example 1

• Consider example from Computational Techniques (Module 6, Part 3)


http://nptel.ac.in/courses/103106074/22:
! # = 2 − # + ln #
• Compute integral using _ = 2, 5, 10, 20 intervals

• Make a log-log plot of error vs. step-size


Local and Global Truncation Errors

• Local Truncation Errors for single application of Newton Cotes Formulae:

Method LTE GTE


Trapezoid 3 ℎR 3 ℎ/
Simp 1/3rd Rule 3 ℎ; 3 ℎ7
Simp 3/8th Rule 3 ℎ; 3 ℎ7

Practice Problem: Simpson’s Rules

• Write code for multiple applications of Simpson’s 1/3rd rule

• Verify global truncation errors


End of Lecture 3.5

NUMERICAL INTEGRATION
Lec. 3.6: MATLAB Functions and Application
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-3


Trapezoidal Rule: MATLAB Function trapz

• Usage of MATLAB function:


I = trapz(x,fval);

Function values fval are specified at corresponding x values

• We solve again for ! # = 2 − # + ln #

Quadrature: MATLAB Function quad

Using functions:
@(x,y,…) nameOfFun(<var list>)
• Usage of MATLAB function:
I = quad(@(x) myFun(x),x);

A function myFun is defined to return ! # for a given value of #


Reactor Problem

• Let us consider the reactor design problem from Computational


Techniques (Module 6, Part 4):
{|}~
x mz
w=r y 8./;
* 1 −z
for  = 10, @ = 5 and ÄÅ_Ç = {0.5}.

End of Lecture 3.6


LINEAR EQUATIONS
Lec. 4.1: Linear Algebra in MATLAB
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-4

Review of Linear Algebra

• Related Video: Computational Techniques, Module-3 Lecture-1


http://nptel.ac.in/courses/103106074/3

• A vector…
⎡ 2⎤ 3
• Is an ordered set of scalars x=⎢ ⎥
⎣ 3⎦ 2
• Has “dimension” (# of elements)

• Has direction and “norm” (distance)


Review of Linear Algebra: rank and Solution
!" + 2!% = 1 1 2 1 2
+= +=
!" − !% = 4 2 4 2 4
1 1
1 2 !" 1 ,=
4
,=
2

! =
1 −1 % 4

2x + 4y = 4 2x + 4y = 2
x + 2y = 1

(3, -1) x + 2y = 1 x + 2y = 1

x-y=4

Review of Linear Algebra: Condition Number

x + 2y =1
x = 3; y = −1
2 x + 3.999 y = 2.001

x + 2y =1
x = 1; y = 0
2 x + 3.999 y = 2

⎡1 2 ⎤
A=⎢ ⎥ Eigenvalues λ1 = −2 ×10− 4 ; λ2 = 4.99;
⎣2 3.999⎦
Useful MATLAB functions
command Purpose command Purpose
expm Matrix exponent inv Inverse of a matrix
logm Matrix logarithm rank Rank of a matrix
sqrtm Matrix square root cond Condition number
^ Matrix Power norm Norm of a vector/matrix

eig Eigen values and vectors lu LU factorization


svd Singular value decomposition chol Cholesky factorization
schur Schur decomposition qr QR factorization

End of Lecture 4.1


LINEAR EQUATIONS
Lec. 4.2: Gauss Elimination
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-4

Solving Linear Equations

• Example from: Computational Techniques, Module-3 Lecture-2


http://nptel.ac.in/courses/103106074/4
!" + !% + !- = 4
2!" + !% + 3!- = 7
3!" + 4!% − 2!- = 9
• Using MATLAB’s powerful Linear Algebra:
x=inv(A)*b or x=A\b
Gauss Elimination (Algorithm)

• Create matrix A234 = + | ,


• In each step
• + 6, 6 is the pivot element

• Use pivot element to create zeros in pivot column

• 89 = 89 − :;,9 8; where :;,9 = + <, 6 /+ 6, 6

Gauss Elimination (Example)

•+ 1,1 is pivot element


• :%," = + 2,1 /A 1,1 and 8% = 8% − :%," 8"

• Repeat for :-," and 8- (… and so on)

• With + 2,2 as pivot element, repeat the above steps for 8- and below
• … And so on until we obtain upper triangular matrix
Back-Substitution

> -
• !- = ? -,-

> % @? %,- AB
• !% = ? %,%

> " @? ",- AB @? ",% AC


• !" = ? ","

Assignment Problem

• Solve using Gauss Elimination + Back Subsitution:

1 2 2 1 1
2 2 4 2 0
G=
1 3 2 5 2
2 6 5 8 4
End of Lecture 4.2

LINEAR EQUATIONS
Lec. 4.3: LU Decomposition; Partial Pivoting
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-4


Gauss Elimination
• Consider example from previous lecture:
!" + !% + !- = 4
2!" + !% + 3!- = 7
3!" + 4!% − 2!- = 9
• We solved it using Gauss Elimination + Back-substitution

• In this lecture
• LU Decomposition

• Partial Pivoting

LU Decomposition

•+ = IJ, where
J is matrix obtained after Gauss Elimination, and

1 0 ⋯ 0
I = :%," 1 ⋯ 0
:-," :-,% ⋱ ⋮
Gauss Elimination + Partial Pivoting

• Idea of Partial Pivoting is to use Row Exchange to ensure the pivot


element + 6, 6 is the largest element in the column

End of Lecture 4.3


LINEAR EQUATIONS
Lec. 4.4: Gauss Siedel
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-4

Iterative Methods for Solving Linear Equations

• Jacobi:

,N − ∑9QN +N,9 !9 ;
!N;O" =
+N,N
• Gauss-Siedel:

;O"
,N − ∑N@"
9R" +N,9 !9 + ∑SNO" +N,9 !9 ;
!N;O" =
+N,N
Gauss Siedel
http://nptel.ac.in/courses/103106074/8

• Consider the examples:

!" + 2!% = 1 !" + 2!% = 1


!" − !% = 4 !" − !% = 4

!";O" = 1 − 2!%; !";O" = 4 + !%;


!%;O" = −4 + !";O" !%;O" = "% 1 − !";O"

Gauss Siedel

• Consider example from previous lecture:


!" + !% + !- = 4
2!" + !% + 3!- = 7
3!" + 4!% − 2!- = 9
• Rearrange as Eq. (2), (3), (1) and solve
Assignment Problem

• Solve using Gauss Siedel method for:

1 2 2 1 1
2 2 4 2 0
G=
1 3 2 5 2
2 6 5 8 4

End of Lecture 4.4


LINEAR EQUATIONS
Lec. 4.5: Tri-Diagonal Matrix Algorithm
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-4

Tri-Diagonal Matrix

T" U" 0 ⋯ 0 0 ,"


V% T% U% ⋯ 0 0 ,%
+= ⋮ ⋮ ⋱ ⋱ ⋮ ⋮ ,= ⋮
0 0 ⋯ ⋯ TS@" US@" ⋮
0 0 ⋯ ⋯ VS TS ,S
Example
• Example from: Computational Techniques, Module-3 Part-5
http://nptel.ac.in/courses/103106074/7
T %W
= X W − 25 , WY = 100, WY = 25
T! % ARZ AR"

• If we use central difference formula,

T %W W;O" − 2W; + W;@"


[ =
T! % ℎ%
A\

Tri-diagonal System of Equations

Discretizing in 10 intervals, with X = 4, results in following equations:

W" = 100
W" − 2 + : W% + W- = ^ : = 0.04
W% − 2 + : W- + W_ = ^
⋮ ⋮ ^ = −1.0
W"" = 25
Tri-Diagonal Matrix

T" U" 0 ⋯ 0 0 ,"


V% T% U% ⋯ 0 0 ,%
+= ⋮ ⋮ ⋱ ⋱ ⋮ ⋮ ,= ⋮
0 0 ⋯ ⋯ TS@" US@" ⋮
0 0 ⋯ ⋯ VS TS ,S

We will solve the above problem using Thomas Algorithm


(For Theory, see: http://nptel.ac.in/courses/103106074/7)

End of Lecture 4.5


2/7/16

NON-LINEAR ALGEBRAIC EQUATIONS


Lec. 5.1: Nonlinear Equation in Single Variable
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-5

A Simple Example
To solve the following equation:
2.0 − % + ln % = 0

The solution is the location where the


curve intersects the X-axis

It is possible to have multiple solutions


0.1586 3.1462 (finite) to the problem

2.0 - x + ln(x) Computational Techniques: Module-4


http://nptel.ac.in/courses/103106074/9

1
2/7/16

General Setup
Let x be a variable of interest. The objective is to find the value of x
which satisfies the following nonlinear equation:
* % =0

Example: Model for a reactor


cA0
C A0 C A kC A
− − =0
τ τ (1 + KC A )2
$!!!!#!!!! "
f (C A )
cA

General Strategy of Solution

Start with initial guess(es)

Using a chosen strategy,


move in the direction of
the solution

Verify if stopping criterion


2.0 - x + ln(x) is satisfied
Yes

Solution!

2
2/7/16

Bisection Method (Single Variable Only)

• Start with two initial guesses, % + and % ,

• Verify that signs of * % + and * % , are different


• Repeat the following steps

• New guess % (./0) is the midpoint of the previous two guesses


• Calculate * % ./0
• Replace either % (+) or % (,) with based on sign of * % ./0

Related: Computational Techniques Module-4 Part-2: http://nptel.ac.in/courses/103106074/10

Methods to Solve Nonlinear Equations

• Bracketing Methods
• Bisection method
• Regula Falsi

• Open Methods
• Secant method
• Fixed-point iteration
• Newton-Raphson

• MATLAB functions fzero and fsolve

3
2/7/16

End of Lecture 5.1

NON-LINEAR ALGEBRAIC EQUATIONS


Lec. 5.2: Using MATLAB Function fzero
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-5

4
2/7/16

MATLAB Function fzero

• Solves nonlinear algebraic equation in single variable

• Uses bracketing method

• Usage:
xSol = fzero(@(x) funName(x),x0)
• xSol is the resulting solution
• funName is a function file that we provide to calculate * %
• x0=[xL;xH]; is vector of initial guesses

Problem to Solve

• Use fzero to solve the nonlinear equation:


2 − % + ln % = 0

• Modify bisection method from previous lecture

5
2/7/16

End of Lecture 5.2

NON-LINEAR ALGEBRAIC EQUATIONS


Lec. 5.3: Fixed-Point Iteration (Single Variable)
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-5

6
2/7/16

Fixed Point Iteration

• Also known as “Method of successive substitution”

• Related: Computational Techniques Module-4 Part-2:


http://nptel.ac.in/courses/103106074/10

• Used for solving equations of the type: % = 2 %

• How are 2 % and * % related è % − 2 % = 0


3 4

Fixed Point Iteration

• Also known as “Method of successive substitution”

• Related: Computational Techniques Module-4 Part-2:


http://nptel.ac.in/courses/103106074/10

567 5
% =2 %

7
2/7/16

Example

2 − % + ln % = 0

% = 2 + ln (%) % = 9 4:;

0.1586 3.1462

Example 2

5% − 9 4/; = 0

% = 0.29 4/; ???

Module-4 Part-3: http://nptel.ac.in/courses/103106074/11

8
2/7/16

End of Lecture 5.3

NON-LINEAR ALGEBRAIC EQUATIONS


Lec. 5.4: Newton-Raphson (Single Variable)
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-5

9
2/7/16

Newton Raphson

• Popular Method

• Example: Computational Techniques Module-4 Part-2:


http://nptel.ac.in/courses/103106074/10

567 5
* %5
% =% − > 5
* %

Example

• Solve the previous example using Newton-Raphson:


* % = 2 − % + ln %
7
*> % = −1 + 4

10
2/7/16

Newton Raphson

• Derivation and Analysis: Computational Techniques Module-4 Part-4:


http://nptel.ac.in/courses/103106074/12

565 5
* %5
% =% − > 5
* %
;
• Has quadratic rate of convergence è @ 567 = @ 5

• Fixed point iteration has linear rate of convergence

End of Lecture 5.4

11
2/7/16

NON-LINEAR ALGEBRAIC EQUATIONS


Lec. 5.5: Using MATLAB function fsolve
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-5

Recap and Next Steps

• Solved nonlinear equation 2 − % + ln % = 0

1. Bisection Method
2. MATLAB function fzero

3. Fixed-Point Iteration
4. Newton-Raphson

• Next: MATLAB function fsolve

• Multivariate problems using fsolve and Newton-Raphson

12
2/7/16

MATLAB Function fsolve

• Solves nonlinear algebraic equation

• Usage:
xSol = fsolve(@(x) funName(x),x0)
• xSol is the resulting solution
• funName is a function file that we provide to calculate A B
• x0 initial guess (same dimension as number of variables)

Problem to Solve

• Use fsolve to solve the nonlinear equation:


2 − % + ln % = 0

13
2/7/16

Multivariate Example: Lorenz Equation

• Wikipedia: https://en.wikipedia.org/wiki/Lorenz_system

• First example to demonstrate “Chaos”

• Observed by Edward Lorenz for atmospheric convection

• Example problem in this Module: Find steady-state solution:


% − C = 0
2% − %D − C = 0
%C − 3D = 0

End of Lecture 5.5

14
2/7/16

NON-LINEAR ALGEBRAIC EQUATIONS


Lec. 5.6: Multivariable Newton Raphson
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-5

Multivariate Example: Lorenz Equation


567 5 5 :7 5
B =B − G A
• Steady-state Lorenz Equation:
% − C = 0
2% − %D − C = 0
%C − 3D = 0
Compute the Jacobian:

1 −1 0
F= 2−D −1 −%
C % −3

15
2/7/16

End of Lecture 5.6

16
Name : Mr. KATRAVATH SANTHOSH

Address : ALGUGADDA TANDA KUSUMASAMUDRAM,,KULAKCHERLA,Rangareddy

Consolidated View Date and Time : 30-Jul-2018 3:55 PM

All Accounts Balance Details

Rate of
S.
Account Number Account Type Branch Interest Balance
No.
(% p.a.)

1 00000062282821977 Savings Account KHARAGPUR 3.5 INR 0.00


2/15/16

REGRESSION AND INTERPOLATION


Lec. 6.1: Introduction
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-6

Example: Regression
• Given the following data:

x 0.8 1.4 2.7 3.8 4.8 4.9


y 0.69 1.00 2.02 2.39 2.34 2.83

Regression:
Obtain a straight line that
best fits the data

1
2/15/16

Example: Interpolation
• Given the following data:

x 0.8 1.4 2.7 3.8 4.8 4.9


y 0.69 1.00 2.02 2.39 2.34 2.83

Interpolation:
“Join the dots” and
find a curve passing
through the data.

Regression vs. Interpolation


• Given the following data:

x 0.8 1.4 2.7 3.8 4.8 4.9


y 0.69 1.00 2.02 2.39 2.34 2.83

• In regression, we are interested in fitting a chosen function to data


y = 0.45 + 0.47x

• In interpolation, given finite amount of data, we are interested in


obtaining new data-points within this range.
At x = 2.0, y = 1.87

2
2/15/16

What Comes Next

• This lecture (for demo):

• Linear Regression: Fit a straight line to the given data


• Newton’s Interpolation: For values at intermediate points

• L–6.2: “Curve fitting” in multiple parameters & lsqcurvefit

• L–6.3: “Parameter Estimation” (using these concepts)

• L–6.4: Interpolation (using spline and pchip)

Linear Least Squares


• Fit a straight line ! = #$ + #& ' to the data:

x 0.8 1.4 2.7 3.8 4.8 4.9


y 0.69 1.00 2.02 2.39 2.34 2.83

• Parameters #$ and #& satisfy the following equations:


(Computational Techniques, Module 5: http://nptel.ac.in/courses/103106074/15)

#$( + #& * '+ = * !+ ( * '+ * !+


+ #$ +
#& =
+ +

#$ * '+ + #& * '+, = * '+ !+ * '+ * '+, * '+ !+


+ + + + + +

3
2/15/16

Newton’s Divided Difference Formula


x y D D2
0.8 0.69 !, − !& -, − -&
', − '& '/ − '&
1.4 1.00
!/ − !, -/ − -,
2.7 2.02 '/ − ', '0 − ',
3.8 2.39 ⋮
⋮ -12& − -12,
4.8 2.34
!1 − !12& '1 − '12,
4.9 2.83 '1 − '12&

End of Lecture 6.1

4
2/15/16

REGRESSION AND INTERPOLATION


Lec. 6.2: Linear Least Squares Regression
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-6

Linear Regression for Multiple Parameters


• Data: '& ,5 &, 6& ; !& , ' , ,5 ,, 6, ; !, , ⋯ , ' 9 ,5 9 ,69 ; !9
• Model to fit: ! = #$ + #& ' + #, 5 + #/ 6

⎡1 x1 u1 w1 ⎤ ⎡a0 ⎤ ⎡ y1 ⎤
⎢1 x

⎢% %
2 u2 w2 ⎥ ⎢ a1 ⎥ ⎢ y2 ⎥
%
⎥⎢ ⎥ = ⎢ ⎥
% ⎥ ⎢ a2 ⎥ ⎢ % ⎥
(
Φ = XT X )−1 X T Y
Least Squares Solution
⎢1 x u N wN ⎥⎦ ⎢⎣ a3 ⎥⎦ ⎢⎣ y N ⎥⎦
⎣$&& N&# &&& " ! $#"
X Φ Y

(Computational Techniques: Module-5 Part-2: http://nptel.ac.in/courses/103106074/16)

5
2/15/16

Example

x 0.8 1.4 2.7 3.8 4.8 4.9


y 0.69 1.00 2.02 2.39 2.34 2.83

Using MATLAB lsqcurvefit

• Standard syntax:
phi=lsqcurvefit(@(p,xData) fName(p,xData),p0,xData,yData);
• phi parameter vector
• p0 vector of initial guesses
• xData, yData data arrays with ( rows
• fName provides !:;<=> = ? '; Φ
• lsqcurvefit minimizes the error between !<ABA and !:;<=>

6
2/15/16

End of Lecture 6.2

REGRESSION AND INTERPOLATION


Lec. 6.3: Functional and Nonlinear Regression
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-6

7
2/15/16

Example: Reaction Rate


• Arrhenius model for reaction rate:
C = D$ E 2F/HI J 1
• We will solve it in two ways:

1. Linear least squares regression taking logarithm

M 1
ln C = ln D$ + − + S ln J

N P T
R

2. Using MATLAB function lsqnonlin

Parameter Estimation using Matrix Method (OLS)


Reaction rate (in mol/(l.s)) for various C and T values
400 K 450 K 500 K 550 K 600 K
1 mol/l 1.48 1.67 1.86 1.96 2.16
2 mol/l 2.35 2.79 3.07 3.37 3.62
3 mol/l 3.28 3.78 4.24 4.48 5.00
4 mol/l 4.12 4.64 5.15 5.76 6.08

C = D $E 2F/HI J 1 log ! = #$ + #& ' + #, 5


ln C ln D $ 1/P ln J

8
2/15/16

Using MATLAB lsqnonlin

• Standard syntax:
phi=lsqnonlin(@(p) fName(p),p0);
• phi parameter vector
• p0 vector of initial guesses

• fName provides vector of errors, E+ = !+ − ? ' + ;Φ


• lsqcurvefit minimizes sum of square errors

End of Lecture 6.3

9
2/15/16

REGRESSION AND INTERPOLATION


Lec. 6.4: Interpolation Options in MATLAB
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-6

Interpolation in MATLAB

• Most popular interpolation techniques:


• spline Cubic spline interpolation
• pchip Piecewise Cubic Hermite Polynomial

• Syntax:
yInterpolated = spline(xData,yData,xval);
• xData,yData S×1 data vectors
• yInterpolated values interpolated at xVal (can be vectors)

10
2/15/16

Example: Temperature variation in a day

time 00 01 02 03 04 05 06 07 08 09 10 11 12
T 25.6 25.4 25.1 24.9 24.9 25.2 25.9 26.3 27.1 29.3 30.8 31.2 32.1
time 13 14 15 16 17 18 19 20 21 22 23 24
T 31.0 30.3 31.4 30.6 31.8 29.6 28.4 28.1 28.2 27.4 26.8 26.1

We are interested in finding temperature at various times during the day,


in addition to the ones where data is available.

We interpolate or “fill in” the missing data

Example: Vehicle speed in front of Govt. Hospital

time (s) 0 10 20 30 40 50 60 70 80 90
speed 45 32 0 0 7 12 20 15 29 55

11
2/15/16

End of Lecture 6.4

12
2/22/16

ORDINARY DIFFERENTIAL EQUATIONS –


INITIAL VALUE PROBLEMS
Lec. 7.1: Introduction and Euler’s Methods
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-7

Introduction: ODE – IVP


!" % &, ( is slope of ( vs. & curve
• Given: = % &,(
!# y
and initial condition: ( & ) = () %(&, ()

• Aim is to find: ( &*

Discussion and theory for numerical ODE-IVP:


Computational Techniques, Module-7 (http://nptel.ac.in/courses/103106074/25)

1
2/22/16

Introduction: ODE – IVP


!" % &, ( is slope of ( vs. & curve
• Given: = % &,(
!# y
and initial condition: ( & ) = () %(&, ()

(*./ − (*
lim = % &, (
5→) ℎ
t
(*./ = (** + ℎ-
ℎ%(&,
* ()
Numerical methods for ODE-IVP:
Use “best” estimate of slope - to obtain ( &

Example

• ODE–IVP:

• ODE: ( 8 = −2&(
• Initial: ( 0 =1

• Analytical solution: ( & = exp −& ?

• Euler’s Explicit Method:


(*./ = (* + ℎ% & * ,(*

2
2/22/16

Euler’s Implicit Method

• (*./ = (* + ℎ% & *./ ,(*./

Thus, use nonlinear solver (such as fsolve) to solve:


(*./ − ℎ% & *./ ,(*./ − (* = 0

@((*./)

Unlike explicit method, Euler’s implicit method is “globally stable”


Computational Techniques, Module-7 Part-5 (http://nptel.ac.in/courses/103106074/29)

End of Lecture 7.1

3
2/22/16

ORDINARY DIFFERENTIAL EQUATIONS –


INITIAL VALUE PROBLEMS
Lec. 7.2: Runge-Kutta (RK-2) Methods
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-7

Runge-Kutta Family of Methods


• Euler’s Explicit Method:
(*./ = (* + ℎ@*

• Runge-Kutta Method:
(*./ = (* + ℎ-*
• where, nth-order RK method gives slope as:
-* = A/ B/ + A? B? + ⋯ + AD BD

See: Computational Techniques, Module-7 Part-2 (http://nptel.ac.in/courses/103106074/26)

4
2/22/16

Second-Order (RK-2) Methods

• Heun’s Method
5
(*./ = (* + ? B/ + B?
B/ = @ & * ,(* , B? = @ & * + ℎ, (* + ℎB/

• Midpoint Method
(*./ = (* + ℎ B?
5 5EF
B/ = @ & * ,(* , B? = @ & * + ? , (* + ?
… etc.

End of Lecture 7.2

5
2/22/16

ORDINARY DIFFERENTIAL EQUATIONS –


INITIAL VALUE PROBLEMS
Lec. 7.3: MATLAB ode45 Algorithm
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-7

Syntax of ode45

• Typical use of ode45:


[tSol,ySol] = ode45(@(t,y) fName(t,y), [t0, tEnd], y0);
• tSol, ySol: Solution vectors; MATLAB returns ySol for each time tSol
• fName : Function that returns G( = % &, (
• t0, y0 : Initial conditions representing ( &) = ()
• tEnd : Final value until which the solution is desired

6
2/22/16

Example: Plug Flow Reactor

• Concentration along a PFR is given by:


!H /
= − ? J /.?L
!I
with J 0 = 1

• Solve to find C for reactor volumes of 1, 5 and 10 liters

End of Lecture 7.3

7
2/22/16

ORDINARY DIFFERENTIAL EQUATIONS –


INITIAL VALUE PROBLEMS
Lec. 7.4: Runge-Kutta Methods
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-7

Revisiting RK-2 Heun’s Method

• Generalize RK-2 Heun’s Method to use function myFun(t,y)

• Develop for ( 8 = −2&(


• Extend to PFR problem

8
2/22/16

Higher Order Runge-Kutta Methods

• RK-3 Method: (*./ = (* + ℎ A/ B/ + A? B? + AM BM

• Can choose appropriate weights to have different orders of accuracy


• Best local truncation error is N ℎO

• RK-4 is usually the most popular RK method, with error N ℎL

Standard RK-4 method

5
• (*./ = (* + B/ + 2B? + 2BM + BO + N ℎL
P

• B / = @ &*, (*
5 5EF
• B ? = @ &* + ? , (* + ?

5 5EQ
• B M = @ &* + ? , (* + ?

• BO = @ &* + ℎ, (* + ℎB M

9
2/22/16

End of Lecture 7.4

ORDINARY DIFFERENTIAL EQUATIONS –


INITIAL VALUE PROBLEMS
Lec. 7.5: Error Analysis of Runge-Kutta Methods
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-7

10
2/22/16

Local Truncation Error

• Euler’s Method: (*./ = (* + ℎ%* + N ℎ?

5
• Heun’s Method: (*./ = (* + ? B/ + B? + N ℎM

5
• RK-3 (Std.) Method: (*./ = (* + B/ + 4B? + BM + N ℎO
P

5
• RK-4 (Std.) Method: (*./ = (* + B/ + 2B? + 2BM + BO + N ℎL
P

Global Truncation Error

• Euler’s Method GTE: N ℎ/

• Heun’s Method GTE: N ℎ?

• RK-3 Method GTE: N ℎM

• RK-4 Method GTE: N ℎO

11
2/22/16

Local vs. Global Truncation Errors

• GTE is one order lower than LTE

• GTE for RK-2 gives the method as N ℎ? accurate

• Consider the previous example to demonstrate with Midpoint Rule

End of Lecture 7.5

12
2/29/16

PRACTICAL ASPECTS OF ODEs


Lec. 8.1: Multi-Variable ODE
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-8

Overview
• Module-7 covered ODE in single variable

• Euler’s and Runge-Kutta methods


• MATLAB solver ode45

• Higher order RK methods and error analysis

• What is needed for practical implementation

• Extension to multi-variable case


• Solving difficult to solve stiff ODEs
• Some practical examples

1
2/29/16

An example
• Model for a damped spring-mass system
" #$ "$
! #+' + ($ = 0
"% "%
• Initial condition: System is stationary @ $ 0 = 1

• Convert into two first-order ODEs: $


With 0 = - è
d$
= -, $ 0 = 1 -
"%
"
"- 0= '- + ($
! + '- + ($ = 0, - 0 =0 "% −
"% !

Solving the Damped Mass-Spring Example

• The resulting ODE-IVP:

-
" $0 1
0= '- + ($ , 0 0 = =
"% − - 0 0
!
• Solve using ode45 for ! = 10,' = 5, ( = 15 and until % = 10.

• Demonstrate using RK-4 method

2
2/29/16

End of Lecture 8.1

PRACTICAL ASPECTS OF ODEs


Lec. 8.2: Stiff Systems & Solution using ode15s
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-8

3
2/29/16

What is a Stiff System?

• Consider the following ODE:


$ 43 = −100$3 , $3 0 = 1
• Now consider the ODE:
$ 4# = −0.01$# , $# 0 = 1
• What if ODE in two variables were:

$ −100 0 $3
" 3
=
"% $
# 0 −0.01 $ #

Is this restricted to diagonal/linear systems?

• Consider:

$ −5.7 1.85 $ 3
" 3
=
"% $
# 13.2 −4.3 $ #
• Solve using Runge-Kutta solver: ode45
• Solve again using implicit solver: ode15s

4
2/29/16

A nonlinear example

• Van der Pol oscillator:


$ 44 − ; 1 − $ # $ 4 + $ = 0, $ 0 = 2 $ 4 0 = 0
• Consider: ; = 1
• Consider: ; = 100

End of Lecture 8.2

5
2/29/16

PRACTICAL ASPECTS OF ODEs


Lec. 8.3: Method of Lines for transient PDEs
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-8

Heat Transfer in a Rod


• Consider the example from Lecture 4.5:

• Heat transfer in a rod:

<= <# =
= > # − ? = − =@ , > = 0.025; ? = 0.1
<% <$

• BC: Rod is held at 100 °C at one end and at =@ = 25 °C at the other end
• IC: Initially, entire rod is uniformly at =@ = 25 °C

6
2/29/16

Heat Transfer in a Rod


• Consider the example from Lecture 4.5:

• Heat transfer in a rod:

<= <# =
= > # − ? = − =@ , > = 0.025; ? = 0.1
<% <$
• As in Lecture 4.5, we use central difference formula:

<# = =EF3 − 2=E + =EG3


D =
<$ # Δ$ #
E

System of ODEs

• This “Method of Lines” yields:


"=E =EF3 − 2=E + =EG3
=> − ? =E − =@
"% Δ$ #
=3 = 100, =I F3 = 25

=#
• Define solution vector 0 = ⋮ and solve using ode45
=I

7
2/29/16

End of Lecture 8.3

PRACTICAL ASPECTS OF ODEs


Lec. 8.4: Some Practical Examples
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-8

8
2/29/16

Examples Considered

• Damped spring-mass system: -


" $
!$ 44 + '$ 4 + ($ = 0 = '- + ($
"% - −
• Van der Pol Oscillator !
$ 44 − ; 1 − $ # $ 4 + $ = 0

• Transient heat conduction

<= <# =
= > # − ? = − =@
<% <$

Examples Considered

• Damped spring-mass system:


!$ 44 + '$ 4 + ($ = 0

• Van der Pol Oscillator -


44 # 4 " $
$ −; 1− $ $ + $ = 0 =
"% - ; 1 − $# - − $
• Transient heat conduction

<= <# =
= > # − ? = − =@
<% <$

9
2/29/16

Examples Considered

=K − 2=# + =3
• Damped spring-mass system: =# > − ? =# − =@
ℎ#
" =0
!$ 44 + '$ 4 + ($ =K = =M − 2=K + =#
"% > − ? =K − =@
ℎ#
• Van der Pol Oscillator ⋮
44 # 4
$ −; 1− $ $ + $ = 0 ⋮

• Transient heat conduction

<= <# =
= > # − ? = − =@
<% <$

Examples Considered

• Damped spring-mass system:


!$ 44 + '$ 4 + ($ = 0

• Van der Pol Oscillator


$ 44 − ; 1 − $ # $ 4 + $ = 0

• Transient heat conduction

<= <# =
= > # − ? = − =@
<% <$

10
2/29/16

“Back to the Beginning”

Indian captain, Mahendra Singh Dhoni, hits a ball with initial


velocity of 35 m/s and angle of 45○. If the boundary is at a distance
of 75 m, will he score a six?

Setting up the Problem


• Acceleration is known: gravity in y-direction and air drag in x-direction
" #$ " #0
= −NO, = −P
"% # "% #
• Velocity and initial location are given:
-QRS = 35, OT = -QRS cos X/4 , - T = -QRS sin X/4
• System of ODEs:
• $4 = O, $ O
4 " 0
= -
• 0 = -,
• O4 = −NO , "% O −NO
• -4 = −P; - −P

11
2/29/16

End of Lecture 8.4

12

You might also like