MATLAB - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

MATLAB

Description:

MATLAB. Basic operations in MATLAB. Operation Symbol Example ... tan(x) tangent of x where x is in radians. ceil(x) Round towards plus infinity. floor(x) ... – PowerPoint PPT presentation

Number of Views:84
Avg rating:3.0/5.0
Slides: 29
Provided by: MustafaK8
Category:
Tags: matlab | ceil

less

Transcript and Presenter's Notes

Title: MATLAB


1
MATLAB
2
Running MATLAB
  • Basic operations in MATLAB

Lowest
Highest
53/39
3
Display formats

4
Variables
  • Up to 31 characters
  • Case sensitive
  • Start with a letter
  • Information about variables
  • WHO
  • WHOS

5
MATLAB Environment
  • Command Window
  • Command History
  • Workspace
  • Current Directory

6
Mathematical functions
pi is a predefined variable ( 3.14....)
7
  • Vectors and vector computation
  • first_nb step last_nb
  • (y 2 0.5 4) ? y2 2.5 3 3.5 4
  • y 1 3 ? y 1 2 3 Default step is 1
  • linspace(first_nb , last_nb , n_points)
  • (y linspace(2 , 4 , 5)) ? y 2 2.5 3 3.5
    4
  • z x y
  • x1 2 3, y4 5 ? z 1 2 3 4 5
  • Accesing elements x(3), x(24), x(125), x(3 1
    4)
  • Column vector
  • x 1234 , x 1 2 3 4
  • Array operations (mult.(), div.(/), add.(),..
    )

8
Matrices and matrix computation
  • x 1 2 34 5 67 8 9, x 127 3-10
    -12
  • Special matrices
  • zeros(3,2)
  • ones(3,2)
  • eye(3) (identity matrix)
  • size(a), length(b)
  • Matrix operations
  • det(x), inv(x)
  • System of linear equations

0 0 0 0 0 0
9
Input-Output Commands
  • Input
  • variable input(text)

r input( Enter a number )
  • Output
  • disp(x), disp(the value is ),
  • disp ( The value is num2str ( x ) )

10
2D Graphics
  • plot(x , y), plot(x , y , r)

11
x -pi .1 pi y sin(x) plot(x,y)
x -pi .1 pi y sin(x) plot(x,y,m)
12
2D Graphics
  • fplot(function, xmin xmax )
  • fplot('sin(x.x)', 0 4)

13
2D Graphics
  • xlabel('x values'), title('exp(-x) and xx
    curves')
  • title(text) writes the text as a title at the
    top of the current plot
  • xlabel(text) adds text to the current plot
    beneath the x-axis
  • ylabel(text) adds text to the current plot
    beside the y-axis
  • grid on adds grid lines on the current axes
  • grid off takes them off
  • text(x,y,text) adds text to the location
    identified by the point (x, y)
  • gtext(text) text is positioned at a location
    by pressing the mouse
  • Try to run this code
  • plot(5 5 4 3 2,8 2 1 1 2,r', 4 6,8
    8,y') xlim(0 10) ylim(0 10)

14
3D Graphics
  • gtgt t0pi/306pi xt.cos(t) yt.sin(t)
    zt plot3(x,y,z)

15
Script files
  • File ? New ?M-file
  • sample.m
  • roots of the quadratic equation ax2bxc0
  • a input('Enter the coefficient a ')
  • b input('Enter the coefficient b ')
  • c input('Enter the coefficient c ')
  • discbb-4ac
  • x1 (-b sqrt(disc)) / (2 a)
  • x2 (-b - sqrt(disc)) / (2 a)
  • Displays the roots
  • disp('Roots are ', num2str(x1),' and
    ',num2str(x2))

16
Operators in Matlab
  • Relational Operators
  • lt Less than
  • lt Less than or equal
  • gt Greater than
  • gt Greater than or equal
  • Equal
  • Not Equal
  • Logical Operators
  • not
  • and
  • or

Priority Increases
17
if else end
if expression-1 commands-1 elseif
expression-2 commands-2 . . . elseif
expression-(n-1) commands-(n-1) else commands-
n end
A1 2 -3 6 if det(A)gt0
Ainvinv(A) disp(Ainv) end
18
switch
switch (selector) case label-1 commands-1
case label-2 commands-2 . .
. case label-n commands-n
otherwise commands-m end
switch (det(a)) case 1
ba' disp(b) case 2
baa disp(b) end
19
for loops
for xarray commands end
for num6 37 23 -1 disp(num2str(k), ' th
element is ', num2str(num)) kk1 end
20
while loops
while expression commands end
while xgt0 yyx xx-1 disp(y)
end
21
User defined functions
  • function output-parametersfunction-name(input-
    parameters)
  • function x1,x2 quadratic(a,b,c)
  • Finds the roots of the quadratic equation
  • ax2bxc0
  • discbb-4ac
  • x1(-bsqrt(disc))/(2a)
  • x2(-b-sqrt(disc))/(2a)
  • gtgt a,b quadratic (1, -2 ,7)

22
Example (islem.m)

function top,cik,bol,carp islem(a,b) top
ab cik a-b bol a/b carp ab end
23
Example (vectorislem.m)
function top,cik,dot vectorislem(a,b) top
ab cik a-b dot ab' end

24
Example 1
  • The series ex is given by
  • Write a MATLAB script to find the sum of the
    series while the value of the current term is
    greater than to the variable tol. Your program
    should input x and tol and should output the sum
    with proper messages. The result should be
    checked by using the MATLAB function exp().

25
Possible Matlab Code
  • x input('Enter a value for x of exp(x)')
  • tol input('Enter the tolerance')
  • k1
  • sum1
  • term1
  • fact1
  • sqx
  • format long
  • while termgttol
  • termsq/fact a single term is calculated
    xn / n!
  • sumsumterm calculated term is added to
    general sum
  • sqsqx next xn1 is calculated
  • factfact(k1) next (x1)! is calculated
  • kk1
  • end
  • disp(sum)
  • disp(term)

26
Example 2
  • Write a MATLAB function to generate and return
    the matrix in the form
  • Your function should accept the parameters d, t,
    s and the size of the square matrix.

27
Possible Matlab Code
function matrix matrix_ex(d,t,s,N) sM
zeros(N-1,1) , eye(N-1) zeros(1,N) s
superior 1's tM zeros(1,N) eye(N-1) ,
zeros(N-1,1) t lower 1's dM eye(N)
d identity matrix sM tM dM
28
Thank you!
Write a Comment
User Comments (0)
About PowerShow.com