MATLAB - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

MATLAB

Description:

Grand total is 22 elements using 776 bytes. Define Symbolic Variables. The 'solve' Command ... 4D Lucky prize $1392000. 20775. Sub-Total (4D Bonus Draws) ... – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 49
Provided by: mat113
Category:
Tags: matlab

less

Transcript and Presenter's Notes

Title: MATLAB


1
MATLAB
Karthik Natarajan Email matkbn_at_nus.edu.sg Depart
ment of Mathematics
2
Outline
  • Introduction to MATLAB
  • Arithmetic, M-files, Graphics, Symbolic
    computing
  • Applications
  • PageRank (Google), Sweep Game (Singapore Pools)

3
  • Part 1
  • Introduction to MATLAB
  • Arithmetic
  • M-files
  • Graphics
  • Symbolic computing

4
What is MATLAB?
  • MATLAB is an interactive software tool that is
    easy to use and yet extremely powerful for
    solving science and engineering problems
  • MATLAB MATrix LABoratory
  • Started in the 1970s. It has now evolved into a
    huge system comprising of built-in functions and
    toolboxes
  • User community size ? 500,000 (industry,
    government academia)

5
Strengths of MATLAB
  • Relatively easy to learn. Easy to interpret and
    fix errors
  • MATLAB can be used as a calculator or a
    programming language
  • Excellent graphics to analyze and visualize your
    data
  • Many built-in functions and toolboxes
    (mathematical, statistical and engineering) that
    are accurate and fast
  • Interfaces to external languages, such as C, C

6
Weakness of MATLAB
  • MATLAB is not a general purpose programming
    language
  • More expensive than conventional Fortran or C
    compiler
  • MATLAB is an interpreted language (making it
    mostly slower than a compiled language such as C,
    C)
  • MATLAB is designed for scientific computation and
    not suitable for other things (such as parsing
    text)

7
MATLAB Interface
MATLAB provides many toolboxes for specialized
capabilities
8
Simple Arithmetic
9
Simple Arithmetic
10
Useful Commands
11
Useful Commands
12
Simple Arithmetic (Vectors)
13
Simple Arithmetic (Vectors)
14
Simple Arithmetic (Vectors)
15
Simple Arithmetic (Matrices)
16
Simple Arithmetic (Matrices)
17
Data Structures
18
M Files
  • Interactive programming is useful only in the
    initial stages.
  • M-files are text files that contain a sequence of
    MATLAB commands to achieve the required goals
  • You can enter the commands into a file and save
    it. Then just type in the file name inside the
    MATLAB command window. All the commands will be
    executed and the results will be available in the
    MATLAB workspace

19
Ensure that you run the M file from the correct
directory
Directory in which M file is stored
20
Functions M-Files
  • A function file is also an M-file where all
    variables in the function file are local.
  • MATLAB functions can return vectors, matrices

gtgt CtoF(5) ans 41
gtgt CtoF(-40) ans -40
21
Commands If
22
Commands For
23
Plots
  • MATLAB is very good at visualizing mathematical
  • functions. Use plot command for the basic plot

gtgt x-80.18 gtgt ysin(x) gtgt plot(x,y) gtgt
xlabel('X') gtgt ylabel('sin(X)')
24
Plots
  • Plotting multiple figures on the same axis

gtgt x-80.18 gtgt ysin(x) gtgt z cos(x) gtgt
plot(x,y,'-',x,z,'o-') gtgt legend('sin(X)','cos(X)
')
25
Subplots
  • subplot partitions a graphics window to show
    many
  • plots

gtgt x-80.18 gtgt ysin(x) gtgt z cos(x) gtgt
subplot(211) gtgt plot(x,y) gtgt ylabel('sin(X)') gtgt
subplot(212) gtgt plot(x,z,'r') gtgt xlabel('X') gtgt
ylabel('cos(X)')
26
Three dimensional plot
  • plot3 is a three dimensional analogue of plot

gtgt t -4pipi/164pi gtgt x sin(t) gtgt y
cos(t) gtgt z t gtgt plot3(x,y,z)
27
Surface Plot
  • Remember the trd function from before

gtgt surf(x,y,z) gtgt xlabel('X') gtgt ylabel('Y') gtgt
zlabel('Z(X2)(Y2)')
28
Conversion of Graphics
  • Can Copy and Paste the graphics plot to
    documents such as Microsoft Word, PowerPoint etc
  • Choose Edit gt Copy Figure
  • MATLAB lets you store the graphs in a variety of
    other formats such as postscript (ps),
    encapsulated postscript (eps), jpeg, pdf etc

29
Symbolic Computing
  • Originally, MATLAB was intended to be a tool for
    numerical analysis
  • Symbolic computing was performed using software
    such as MAPLE and MATHEMATICA
  • MATLAB incorporated the kernel of MAPLE to
    supplement its numerical and graphic facilities
  • Scope Calculus, Linear Algebra, Solution of
    Equations, ?

30
Symbolic Computing An Example
Consider the two symbolic expressions
f1 exp(?-t1)/t
f2 exp(?-t2)/t
We are interested in determining ? and t as
functions of f1, f2, t1 and t2.
31
Define Symbolic Variables
The syms Command
gtgt syms f1 f2 theta tau t1 t2
gtgt whos Name Size Bytes
Class f1 1x1 128 sym
object f2 1x1 128 sym
object t1 1x1 128 sym
object t2 1x1 128 sym
object tau 1x1 130 sym
object theta 1x1 134 sym
object Grand total is 22 elements using 776 bytes
32
Symbolic Solution
The solve Command
gtgt solsolve(log(f1)-((theta-t1)/tau),log(f2)-((th
eta-t2)/tau),theta,tau)
Result is stored in the variable sol
gtgt sol sol tau 1x1 sym theta
1x1 sym
33
Symbolic Solution
gtgt sol.tau ans -(-t2t1)/(log(f1)-log(f2))
gtgt sol.theta ans -(log(f2)t1-t2log(f1))/(l
og(f1)-log(f2))
34
Symbolic Solution (in pretty form)
The pretty command
gtgt pretty(sol.tau) -t2 t1
- -----------------
log(f1) - log(f2) gtgt
pretty(sol.theta)
log(f2) t1 - t2 log(f1)
- -----------------------
log(f1) - log(f2)
35
From Symbolic Solution To Numbers
Compute the values for t1 10, t2 20, f1
0.6469 f2 0.1469
The subs command
gtgt subs(sol.tau,t1,t2,f1,f2,10,20,0.6469,0.1469
) ans 6.7456 gtgt subs(sol.theta,t1,t2,f1,f
2,10,20,0.6469,0.1469) ans 7.0618
36
Symbolic Computing Nonlinear Eqns
  • Consider the solution to nonlinear equations
  • f1 f2 0.5
  • f2 log(f2) f1 log(f1) 0

gtgt syms f1 f2 gtgt solsolve(f1-f2-0.5,(f2log(f2))-
(f1log(f1)),f1,f2)
gtgt sol.f1 ans .646907686670207746658300826651
83 gtgt sol.f2 ans .146907686670207746658300826
65183
37
Symbolic Computing Derivatives
The diff command
gtgt syms x gtgt diff(x5) ans 5x4 gtgt
diff(x5,3) ans 60x2
38
Symbolic Computing Derivatives
gtgt syms x y gtgt f y / (y - x) gtgt
dfx2diff(f,2) gtgt dfy3diff(f,y,3) gtgt dfx2 dfx2
2y/(y-x)3 gtgt dfy3 dfy3 6/(y-x)3-6y/(y-
x)4
39
Symbolic Computing Integrals
The int command Can perform both Indefinite
and Definite Integration
gtgt syms x gtgt intxint(x-(x2)) gtgt
pretty(intx) 1/2 x2 - 1/3 x3 gtgt
intxint(x-(x2),1,2) intx -5/6
40
Exercise 1 Matrix Manipulation
  • Enter matrix
  • A 2 6 3 9, B 1 2 3 4, C -5 5 5
    3
  • Create a big matrix G that has A, B and C along
  • diagonals Do not explicitly write out the entire
  • matrix G. Go to HELP and search for matrix
  • indexing
  • Replace G(5,5) with 4
  • What do you get for G(15)?
  • What do you get for G(15,1)?

41
Exercise 2 Root Finding
  • Use MATLAB to find the value of x that satisfies
  • sin(x) ex 2
  • You need to use two different approaches
  • Code your own Newton method based on
  • Use the inbuilt fzero function

xnew xold f(xold)/f(xold)
42
Exercise 3 Taylors approximation
  • Plot the first three orders of Taylors
    expansions
  • for the function log(x) around x 1 on a graph.
  • Your output should look like this

f(x) f(xo) (x-xo)f(xo) (x-xo)2f(xo)/2
(x-xo)3f(xo)/6
43
  • Part 2
  • Applications
  • Page Rank (Google)
  • Sweep Game (Singapore Pools)

44
Application 1 PageRank
  • PageRank is a key element of the Google search
  • engine - developed by Larry Page and Sergey
    Brin
  • Googles Description of PageRank

PageRank relies on the uniquely democratic nature
of the web by using its vast link structure as an
indicator of an individual page's value. In
essence, Google interprets a link from page A to
page B as a vote, by page A, for page B. But,
Google looks at more than the sheer volume of
votes, or links a page receives it also analyzes
the page that casts the vote. Votes cast by pages
that are themselves "important" weigh more
heavily and help to make other pages "important."
45
Application 1 PageRank
  • You will develop your own version of the PageRank
  • algorithm in MATLAB.
  • It should be able to
  • Handle large databases
  • Be efficient and provide good rankings

46
Application 2 Sweep Game
  • The Sweep game allows the holder of a ticket to
    participate in
  • One 7D sweep draw and
  • Three 4D bonus draws
  • Each tickets costs 3 with up to 3 million
    tickets that can be sold.

47
(No Transcript)
48
Application 2 Sweep Game
  • You will develop a simulation of the Sweep
  • Game in MATLAB that can be used to study the
    effect
  • of the number of tickets sold on the profits from
    the
  • game for Singapore Pools
Write a Comment
User Comments (0)
About PowerShow.com