MATLAB 4 Numerical Computations
MATLAB 4 Numerical Computations
using
MATLAB
Contents
1 2 3 x1 3
Step 2: Write the equations in 4 5 6 x2 6
matrix form
7 8 0 x3 9
Matrices and Linear Algebra
Step 3: Solve the matrix equations in MATLAB
>> A = [1 2 3; 4 5 6; 7 8 0];
>> b = [3; 6; 9];
Method 1
>> x = inv(A)*b
x=
-1.0000
2.0000
-0.0000
Method 2
>> x = A\b
x=
-1.0000
2.0000
0.0000
Data Analysis and Statistics
Let us consider the marks obtained by 10
students in 3 exams
>> av_student =
>> av_subject =
mean(marks(:,2:4),2)
mean(marks(:,2:4),1)
av_student =
av_subject =
73.6667
77.9000 77.9000 79.2000
81.6667
56.6667
or
75.6667
85.3333
>> av_subject =
84.6667
mean(marks(:,2:4))
77.6667
av_subject = 85.6667
77.9000 77.9000 79.2000 85.0000
77.3333
Data Analysis and Statistics
Find the deviation from the average
>> dev_marks = marks(:,2:4) - repmat(av_subject,10,1)
dev_marks =
-17.9000 7.1000 -3.2000
12.1000 -2.9000 0.8000
-27.9000 -22.9000 -14.2000
-17.9000 -0.9000 10.8000
10.1000 -1.9000 12.8000
18.1000 10.1000 -9.2000
-7.9000 -0.9000 6.8000
12.1000 1.1000 8.8000
20.1000 0.1000 -0.2000
-0.9000 11.1000 -13.2000
repmat(A,m,n) creates a matrix consisting of an m-by-n tiling
of copies of A.
Data Analysis and Statistics
Find the maximum marks
>> max(marks(:,2:4)) highest in subject A = 98, in
ans = subject B = 89, in subject C = 92
98 89 92
min_marks =
minimum in subject A = 50, in
50 55 65
subject B = 55, in subject C = 65
min_student =
3 3 3
Third student got minimum in all
the subjects
Data Analysis and Statistics
Let A = [ 60 85 76
90 75 80
50 55 65
60 77 90 ]
ans = ans =
50 55 65 60 76 85
60 75 76 75 80 90
60 77 80 50 55 65
90 85 90 60 77 90
Data Analysis and Statistics
Random number generation
rand(n) generates a nxn matrix of random numbers
rand(m,n) generates mxn matrix of random numbers
>> rand(3)
ans =
0.9105 0.4946 0.4436
0.1164 0.0736 0.3281
0.5779 0.9285 0.8889
>> rand(2,3)
ans =
0.4931 0.6566 0.4231
0.9509 0.9477 0.6800
Data Analysis and Statistics
Random number generation
For numerical experiments involving random numbers it
becomes sometimes essential to set the state of the random
generator to make the experiments repeatable.
>> x = [2 4 6 8 10];
>> y = [0.3679 0.1353 0.0498 0.0183 0.0067];
Polynomial Multiplication
The polynomial multiplication is performed using the function conv.
Let us multiply two polynomials
p(x) = 4x3 + 3x2 + 2x + 1 and
q(x) = x3 + 2x2 + 3x + 4
>> p = [4 3 2 1];
>> q = [1 2 3 4];
>> mult = conv(p,q)
mult =
4 11 20 30 20 11 4
Polynomials and Interpolation
Polynomial Addition
>> a = [ 1 2 1 1];
>> b = [4 2 -1 -5 2 ];
>> [q, r] = deconv(b, a)
q=
4 -6 Quotient when a is divided by b
r=
0 0 7 -3 8 Reminder when a is divided by b
Polynomials and Interpolation
Polynomial Derivatives and Integrals
>> a = [4 11 20 30 20 11 4];
>> polyder (a)
ans =
24 55 80 90 40 11
>> polyint (a, 10) Constant of integration
ans =
0.5714 1.8333 4.0000 7.5000 6.6667 5.5000 4.0000 10.0000
Polynomials and Interpolation
Evaluation
>> a = [ 1 2 1 1]; x = 2;
>> val_a = polyval (a, x)
val_a =
19
Polynomials and Interpolation
Curve Fitting
Function polyfit is used to fit a least-square curve through data
points.
>> x = [1 2 3 4 5 6 7 8 9 10];
>> y = [-1 1 11 35 79 149 251 391 575 809];
>> p = polyfit(x,y,4); Fitted curve
>> xi = linspace(min(x), max(x), 50);
Data points
>> yi = polyval(p, xi);
>> plot(xi,yi,x,y,’.’)
Using function optimset to set the tolerance and to display the final
result.
>> options = optimset('display','final','tolx',0.001);
>> [x,value] = fzero(@humps,1.28,options)
Zero found in the interval: [1.2438, 1.3162].
x=
1.29933971772587
value =
0.00462447899074
Integration and Differentiation
Integration
b
quad (f, a, b, tol) (Simpson quadrature)
f x dx
a
quadl (f, a, b, tol) (Lobatto quadrature)
y max x max
f x, y dx.dy
y min x min
dx xi xi 1
xi-1 xi xi+1
Central Difference
dy xi f xi 1 f xi
dx xi 1 xi
Integration and Differentiation
Differentiation
diff (x, n, dim) is the nth difference function
along dimension ‘dim’.
The default n is 1.
The default dimension is the first dimension of x.
Example:
diif(x) will evaluate the 1st derivative of x along 1st dimension
(row difference in 2D matrix).
diif(x,2) will evaluate the 2nd derivative of x along 1st
dimension.
diff(x,1,2) will evaluate the 1st derivative of x along 2nd
dimension.
Integration and Differentiation
Differentiation
Example: Let us find the 1st derivative of sin(x)
>> dx = .01;
>> x = 0:dx:2*pi; sin x
>> y = sin(x);
>> dy_dx = diff(y)/dx; d sin x
% This determines the central derivative dx
at points (x(i)+x(i+1))/2
>> len_x = length(x); % Length of x
>> xx = (x(2:len_x)+x(1:len_x-1))/2;
>> plot (x, y, xx, dy_dx)
Integration and Differentiation
Differentiation
Example: Let us find the 2nd derivative of sin(x)
>> dx = .01;
>> x = 0:dx:2*pi; sin x
>> y = sin(x);
>> d2y_dx2 = diff(y,2)/(dx).^2; d 2 sin x
% This determines the 2nd dx 2
derivative at point x(i)
>> len_x = length(x); % Length
of x
>> xx = x(2:len_x-1);
>> plot(x,y,xx,d2y_dx2)
dy 2 xi f xi 1 2 f xi f xi 1
Note: dx 2 2
x
Integration and Differentiation
Differentiation
[fx, fy] = gradient(f, dx, dy) returns the numerical gradient of
the matrix ‘f’,
where,
‘fx’ corresponds to df/dx and fy corresponds to df/dy
‘dx’ and ‘dy’ specify the spacing between the coordinate
points.
The default values of ‘dx’ and ‘dy’ are equal to 1.
[f1, f2, f3, …, fn] = gradient (f, d1, d2, …, dn) returns the
numerical gradient of the n-dimensional matrix with the
gradients in the respective directions as f1, f2, …, fn and the
spacing between the points in the different directions to be
d1, d2, …, dn respectively.
Integration and Differentiation
Differentiation
Example: Let us determine the derivatives of the function peaks.
>> dx = 0.25;
>> dy = 0.25;
>> x = -3:dx:3;
>> y = -3:dy:3;
>> [X, Y] = meshgrid (x, y);
>> Z = peaks (X,Y);
>> contour(X,Y,Z)
>> hold on
>> [dzdx,dzdy] = gradient(Z,dx,dy);
>> quiver(X,Y,dzdx,dzdy)
The user Equations
Differential may choose from a
number of alternative functions,
Differential e.g., ode23, ode45, ode113 etc.
Equations
[t, y] = ode…(odefun, tspan, y0) with tspan = [t0 tfinal], solves
the ordinary differential equation:
y f t , y , y t0 y0
The ODE solvers accept only first-order differential equations.
To use the ODS solvers to solver equations involving higher order
derivatives, the ODE’s must be expressed as an equivalent system of first-
order differential equations.
y2 1 0.5 y 2 y2 y1 , y2 0 0
Differential Equations
Differential Equations t<0
T = Ti = 1500K
Example: The solid metal blocks are
heated to 1500K and suddenly dropped to
a cold fluid (T =300K). Estimate the
temporal variation of temperature of the
block.
Assume: h = 20W/m2.K, k = 40W/m, m =
T(t)
0.1 Kg, A = 0.005 m2, cp = 600 J/Kg.K,
= 0.9.
The governing differential equations are: T = 300K
dT
m.c p qlost qconv qrad
dt Eout Econv Erad
where, qconv h. A. T t T , qrad . A. T 4 t T4