SlideShare a Scribd company logo
1 of 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
Current Folder − This panel allows you to access
the project folders and files.
Command Window
This is the main area
where commands can
be entered at the
command line.
Workspace -The workspace
shows all the variables created
and/or imported from files.
Command History − This panel
shows or return commands that
are entered at the command line.
2021/22 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
• Column vectors
r = [7 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
Matrix
•
• a( 4 , : ) = []
• a(:, 5 ) = []
a = [ 1 2 3 4 5; 2 3 4 5
6; 3 4 5 6 7; 4 5 6 7 8]
a(2,5)
a(2:5)
a(:,5)
prod = a * b
inv(a)
div = a / b
det(a)
Delete the 4th row of a
Delete the 5th row of a
b = a' Transpose of a
2021/22 Eyob A. 6
Matrix
• A magic square is a square matrix
• Produces the same sum when added
• Row wise
• Column wise
• And diagonally
zeros(5)
zeros(5,6)
ones(5)
ones(5,6)
eyes(5)
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
• 12+23+25
• 35-22
• 23*14
• 23/2
• 2*3+2/4-6
1. MATLAB-Calculator
• Addition
• Subtraction
• Division
• Multiplication
• Root
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
p = [1 7 0 -5 9];
roots(p)
polyval (p,4)
polyval value at a specified value.
For example, polynomial p, at x = 4
polyvalm function for evaluating a matrix
polynomial
p = [1 7 0 -5 9];
X = [1 2 -3 4; 2 -5 6
3; 3 1 0 2; 5 -7 3 8];
polyvalm(p, X)
p2 = poly(r)
The function poly is an inverse of the roots
function and returns to the polynomial
coefficients.
The polynomial coefficients
The polynomial function
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
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
p = polyfit(x,y,n)
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)
L3 = limit(f + g, 4)
L4 = limit(f - g, 4)
L5 = limit(f*g, 4)
L6 = limit (f/g, 4)
f = (x - 3)/abs(x-3);
l = limit(f,x,3,'left’)
r = limit(f,x,3,'right')
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')
'Df = -2*f + cos(t)'
'D2y + 2Dy = 5*sin(3*x)'
der = dsolve('Dy = 5*y')
First order differential equation: y' = 5y
In using dsolve command, derivatives are indicated with a D
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)
syms x
f = x^3 - 2*x +5;
a = int(f, 1, 2) display('Area: '),
disp(double(a));
syms x
f=x^2+3*x-6
int(f, 2, 4)
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));
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)
round(a)
sum(A)
min(A)
max(A)
mean(A)
prod(A)
sort(A)
length(A)
size(a)
floor(a)
std(a)
sqrt(A)
2021/22 Eyob A. 20

More Related Content

Similar to Chapter 1 MATLAB Fundamentals easy .pptx

COMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptxCOMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptximman gwu
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functionsjoellivz
 
Solution Process of Ordinary Differential Equations with their Graphical Repr...
Solution Process of Ordinary Differential Equations with their Graphical Repr...Solution Process of Ordinary Differential Equations with their Graphical Repr...
Solution Process of Ordinary Differential Equations with their Graphical Repr...Md. Al-Amin
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsMukesh Kumar
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxagnesdcarey33086
 
Lines and planes in space
Lines and planes in spaceLines and planes in space
Lines and planes in spaceFaizan Shabbir
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentationManchireddy Reddy
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notesInfinity Tech Solutions
 
MatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On PlottingMatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On PlottingMOHDRAFIQ22
 
Math 223 Disclaimer It is not a good idea.docx
    Math 223   Disclaimer It is not a good idea.docx    Math 223   Disclaimer It is not a good idea.docx
Math 223 Disclaimer It is not a good idea.docxjoyjonna282
 

Similar to Chapter 1 MATLAB Fundamentals easy .pptx (20)

Matlab1
Matlab1Matlab1
Matlab1
 
Matlab algebra
Matlab algebraMatlab algebra
Matlab algebra
 
COMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptxCOMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptx
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
 
Solution Process of Ordinary Differential Equations with their Graphical Repr...
Solution Process of Ordinary Differential Equations with their Graphical Repr...Solution Process of Ordinary Differential Equations with their Graphical Repr...
Solution Process of Ordinary Differential Equations with their Graphical Repr...
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projects
 
Digital Logic
Digital LogicDigital Logic
Digital Logic
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
 
Matlab cheatsheet
Matlab cheatsheetMatlab cheatsheet
Matlab cheatsheet
 
presentation.pptx
presentation.pptxpresentation.pptx
presentation.pptx
 
Lines and planes in space
Lines and planes in spaceLines and planes in space
Lines and planes in space
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentation
 
MATLAB Programming
MATLAB Programming MATLAB Programming
MATLAB Programming
 
ML-CheatSheet (1).pdf
ML-CheatSheet (1).pdfML-CheatSheet (1).pdf
ML-CheatSheet (1).pdf
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Mathematical Modeling With Maple
Mathematical Modeling With MapleMathematical Modeling With Maple
Mathematical Modeling With Maple
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notes
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
 
MatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On PlottingMatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On Plotting
 
Math 223 Disclaimer It is not a good idea.docx
    Math 223   Disclaimer It is not a good idea.docx    Math 223   Disclaimer It is not a good idea.docx
Math 223 Disclaimer It is not a good idea.docx
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 

Recently uploaded (20)

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 

Chapter 1 MATLAB Fundamentals easy .pptx

  • 1. 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
  • 2. Current Folder − This panel allows you to access the project folders and files. Command Window This is the main area where commands can be entered at the command line. Workspace -The workspace shows all the variables created and/or imported from files. Command History − This panel shows or return commands that are entered at the command line. 2021/22 Eyob A. 2
  • 3. Functions of MATLAB • Used as calculator • Plotting • Calculus • Image and signal processing • Mathematical modeling • AI-Deep learning 2021/22 Eyob A. 3
  • 4. 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
  • 5. Vectors, Arrays and Matrix Vector • A vector is a one-dimensional array of numbers. • MATLAB allows creating two types of vectors. • Row vectors • Column vectors r = [7 8 9 10 11] c = [7, 8, 9, 10, 11] sub_r = r(3:7) 2021/22 Eyob A. 5
  • 6. Matrix-is a two-dimensional array of numbers. • Deleting a Row or a Column in a Matrix • • a( 4 , : ) = [] • a(:, 5 ) = [] a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8] a(2,5) a(2:5) a(:,5) prod = a * b inv(a) div = a / b det(a) Delete the 4th row of a Delete the 5th row of a b = a' Transpose of a 2021/22 Eyob A. 6
  • 7. Matrix • A magic square is a square matrix • Produces the same sum when added • Row wise • Column wise • And diagonally zeros(5) zeros(5,6) ones(5) ones(5,6) eyes(5) rand(4, 5) magic(4) 2021/22 Eyob A. 7
  • 8. 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
  • 9. 1. MATLAB-Calculator • 12+23+25 • 35-22 • 23*14 • 23/2 • 2*3+2/4-6 1. MATLAB-Calculator • Addition • Subtraction • Division • Multiplication • Root 2021/22 Eyob A. 9
  • 10. 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
  • 11. Roots of the polynomial y=x4 + 7x3 - 5x + 9 p = [1 7 0 -5 9]; roots(p) polyval (p,4) polyval value at a specified value. For example, polynomial p, at x = 4 polyvalm function for evaluating a matrix polynomial p = [1 7 0 -5 9]; X = [1 2 -3 4; 2 -5 6 3; 3 1 0 2; 5 -7 3 8]; polyvalm(p, X) p2 = poly(r) The function poly is an inverse of the roots function and returns to the polynomial coefficients. The polynomial coefficients The polynomial function 2021/22 Eyob A. 11
  • 12. • 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
  • 13. 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 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 p = polyfit(x,y,n) 2021/22 Eyob A. 13
  • 14. 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) L3 = limit(f + g, 4) L4 = limit(f - g, 4) L5 = limit(f*g, 4) L6 = limit (f/g, 4) f = (x - 3)/abs(x-3); l = limit(f,x,3,'left’) r = limit(f,x,3,'right') 2021/22 Eyob A. 14
  • 15. 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
  • 16. Solving Differential Equations • MATLAB provides the dsolve command for solving differential equations symbolically. • dsolve('eqn') 'Df = -2*f + cos(t)' 'D2y + 2Dy = 5*sin(3*x)' der = dsolve('Dy = 5*y') First order differential equation: y' = 5y In using dsolve command, derivatives are indicated with a D 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
  • 17. 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
  • 18. 3.3 Integral B. Definite Integral syms x int(x, 4, 9) syms x f = x^3 - 2*x +5; a = int(f, 1, 2) display('Area: '), disp(double(a)); syms x f=x^2+3*x-6 int(f, 2, 4) 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)); 2021/22 Eyob A. 18
  • 19. 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
  • 20. Built-in functions in MATLAB A=[1.3 2.5 3.7 ; 4.1 5.8 6] log(A) ceil(a) floor(a) round(a) sum(A) min(A) max(A) mean(A) prod(A) sort(A) length(A) size(a) floor(a) std(a) sqrt(A) 2021/22 Eyob A. 20