Chapter 1 MATLAB Fundamentals
Chapter 1 MATLAB Fundamentals
2021/22 Eyob A. 1
Command Window
This is the main area
where commands can
be entered at the
command line.
2021/22 Eyob A. 3
Variables
• You can assign variables in a simple way.
• x = 3 % defining x and initializing it with a value
x = 7 * 8;
y = x * 7.89
Multiple Assignments
You can have multiple assignments on the same line
a = 2; b = 7; c = a * b
a = 2; b = 7; c = a * b
2021/22 Eyob A. 4
Vectors, Arrays and Matrix
Vector
• A vector is a one-dimensional array of numbers.
• MATLAB allows creating two types of vectors.
• Row vectors
•rColumn
= [7 vectors
8 9 10 11]
c = [7, 8, 9, 10, 11]
sub_r = r(3:7)
2021/22 Eyob A. 5
Matrix-is a two-dimensional array of numbers.
• Deleting a Row or a Column in a
a = [ 1 2 3 4 5; 2 3 4 5 Matrix
6; 3 4 5 6 7; 4 5 6 7 8]
Delete the 4th row of a
a(2,5)
• a( 4 , : ) = []
a(2:5)
Delete the 5th row of a
a(:,5)
prod = a * b • a(:, 5 ) = []
div = a / b
b = a' Transpose of a
inv(a)
det(a)
2021/22 Eyob A. 6
Matrix
zeros(5) • A magic square is a square matrix
zeros(5,6) • Produces the same sum when added
ones(5) • Row wise
ones(5,6) • Column wise
eyes(5) • And diagonally
rand(4, 5)
magic(4)
2021/22 Eyob A. 7
Vectors, Arrays and Matrix
Arrays
• All variables of all data types in MATLAB are multidimensional
arrays.
• A vector is a one-dimensional array and a matrix is a two-
dimensional array.
• Special Arrays in MATLAB
2021/22 Eyob A. 8
1. MATLAB-Calculator
• Addition
• 12+23+25
• Subtraction
• 35-22
• Division
• 23*14
• Multiplication
• 23/2
• Root
• 2*3+2/4-6
2021/22 Eyob A. 9
2. Plotting
• MATLAB can plot the graph of any continuous and discrete
functions.
• Using plot and stem
• Example;
Plot the graph of polynomial, exponential and sinusoidal function
2021/22 Eyob A. 10
Roots of the polynomial
y=x4 + 7x3 - 5x + 9 The polynomial function
p = [1 7 0 -5 9]; The polynomial
roots(p) coefficients
polyval value at a specified value.
polyval (p,4) For example, polynomial p, at x = 4
p = [1 7 0 -5 9];
X = [1 2 -3 4; 2 -5 6
polyvalm function for evaluating a
3; 3 1 0 2; 5 -7 3 8]; matrix polynomial
polyvalm(p, X)
2021/22 Eyob A. 11
• Plot the simple function y = x
x = [0:5:100];
y = x; plot(x, y)
x = [0:0.01:10];
y = sin(x);
plot(x, y), xlabel('x'), ylabel('Sin(x)’),
title('Sin(x) Graph'), grid on, axis equal
x = [0 : 0.01: 10];
y = sin(x);
g = cos(x);
plot(x, y, x, g, '.-’),
legend('Sin(x)', 'Cos(x)')
2021/22 Eyob A. 12
Polynomial Curve Fitting
• The polyfit function finds the coefficients of a polynomial that
fits a set of data in a least-squares sense.
• If x and y are two vectors containing the x and y data to be fitted
to a n-degree polynomial, then we get the polynomial fitting the
data by writing
p = polyfit(x,y,n)
x = [1 2 3 4 5 6];
y = [5.5 43.1 128 290.7 498.4 978.67]; %data
p = polyfit(x,y,4)
x2 = 1:.1:6;
y2 = polyval(p,x2);
plot(x,y,'o',x2,y2) grid on
2021/22 Eyob A. 13
3. Calculus
3.1. Limit of a function
Calculate the limit of a function f(x) = (x3 + 5)/(x4 + 7), as x
tends to 0 and 1.
syms x
limit((x^3 + 5)/(x^4 + 7))
limit((x^3 + 5)/(x^4 + 7),1)
limit(x^2 + 5, 3)
syms x
f = (3*x + 5)/(x-3);
g = x^2 + 1;
L1 = limit(f, 4)
L2 = limit (g, 4) f = (x - 3)/abs(x-3);
L3 = limit(f + g, 4) l = limit(f,x,3,'left’)
L4 = limit(f - g, 4) r = limit(f,x,3,'right')
L5 = limit(f*g, 4)
L6 = limit (f/g, 4)
2021/22 Eyob A. 14
3.2. Differentiation in MATLAB
• MATLAB provides the diff command for computing symbolic
derivatives.
syms t
f = 3*t^2 + 2*t^(-2);
diff(f)
Computing Higher Order Derivatives
syms x
f = x*exp(-3*x);
diff(f, 2)
diff(f, 3)
2021/22 Eyob A. 15
Solving Differential Equations
• MATLAB provides the dsolve command for solving differential
equations symbolically.
dsolve('eqn')
In using dsolve command, derivatives are indicated
with a D
'Df = -2*f + cos(t)'
'D2y + 2Dy = 5*sin(3*x)'
First order differential equation: y' = 5y
2021/22 Eyob A. 16
3.3 Integral
• A. Indefinite integral
syms x
int(2*x)
syms x n
int(cos(x))
int(exp(x))
int(log(x))
int(x^-1)
int(x^5*cos(5*x))
pretty(int(x^5*cos(5*x)))
int(x^-5)
int(sec(x)^2)
pretty(int(1 - 10*x + 9 * x^2))
int((3 + 5*x -6*x^2 - 7*x^3)/2*x^2)
pretty(int((3 + 5*x -6*x^2 - 7*x^3)/2*x^2))
2021/22 Eyob A. 17
3.3 Integral
B. Definite Integral
syms x
int(x, 4, 9)
Find the area under the curve: f(x) = x2 cos(x) for −4 ≤ x ≤
9.
f = x^2*cos(x); ezplot(f, [-4,9])
a = int(f, -4, 9)
disp('Area: '), disp(double(a));
syms x
f=x^2+3*x-6
int(f, 2, 4)
syms x
f = x^3 - 2*x +5;
a = int(f, 1, 2) display('Area: '),
disp(double(a));
2021/22 Eyob A. 18
Integral cont…
1. Evaluate the integral from x=0 to x=Inf.
fun = @(x) exp(-x.^2).*log(x).^2;
q = integral(fun,0,Inf)
2021/22 Eyob A. 19
Built-in functions in MATLAB
A=[1.3 2.5 3.7 ; 4.1 5.8 6]
log(A)
ceil(a)
floor(a) length(A)
round(a) size(a)
sum(A) floor(a)
min(A) std(a)
max(A) sqrt(A)
mean(A)
prod(A)
sort(A)
2021/22 Eyob A. 20