0% found this document useful (0 votes)
4 views20 pages

Chapter 1 MATLAB Fundamentals

The document provides an overview of MATLAB, including its main windows such as the Command Window, Workspace, and Graphics Window, along with its functions for calculations, plotting, calculus, and data processing. It explains how to define variables, create vectors and matrices, and perform operations like addition, subtraction, and polynomial fitting. Additionally, it covers built-in functions and methods for solving differential equations and calculating integrals.

Uploaded by

yafetgetenesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views20 pages

Chapter 1 MATLAB Fundamentals

The document provides an overview of MATLAB, including its main windows such as the Command Window, Workspace, and Graphics Window, along with its functions for calculations, plotting, calculus, and data processing. It explains how to define variables, create vectors and matrices, and perform operations like addition, subtraction, and polynomial fitting. Additionally, it covers built-in functions and methods for solving differential equations and calculating integrals.

Uploaded by

yafetgetenesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

MATLAB

MATLAB: Matrix Laboratory


• Command window-used to enter command and data
• Graphics window-display plots and graphs
• Edit window – create and edit M-files
• Workspace window

2021/22 Eyob A. 1
Command Window
This is the main area
where commands can
be entered at the
command line.

Current Folder − This panel allows you to


access
the project folders and files.

Command History − This


panel shows or return
commands that are entered at
the command line.

Workspace -The workspace


shows all the variables created
and/or
2021/22imported from files. Eyob A. 2
Functions of MATLAB
• Used as calculator
• Plotting
• Calculus
• Image and signal processing
• Mathematical modeling
• AI-Deep learning

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

Assignments refers to assigning values to a variable

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)

The function poly is an inverse of the roots


p2 = poly(r) function and returns to the polynomial
coefficients.

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

der = dsolve('Dy = 5*y')


Example: y" - y = 0, y(0) = -1, y'(0) = 2.
dsolve('D2y - y = 0','y(0) = -1','Dy(0) =
2')

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)

2. Evaluate the integral from x=0 to x=2 at c=5.


q = integral(@(x) fun(x,5),0,2)

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

You might also like