matlab physics - iuspas.fnal.gov/materials/14unm/matlab_1.pdf · matlab physics - i matlab and...

40
06/15/14 UPAS - MATLAB Physics 1 MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the scripts. There is a textbook available The book is supplied by UPAS There is a CD with the .m file scripts for the demonstrations used in the course Lecture/class notes – email list for communication of the class Distribute all scripts by data stick – 200 examples Homework and “project”

Upload: phungnhi

Post on 05-Feb-2018

253 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UPAS - MATLAB Physics 1

MATLAB Physics - I

● MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the scripts.

● There is a textbook available■ The book is supplied by UPAS■ There is a CD with the .m file scripts for the

demonstrations used in the course● Lecture/class notes – email list for communication of

the class ● Distribute all scripts by data stick – 200 examples● Homework and “project”

Page 2: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UPAS - MATLAB Physics 2

Programming in MATLAB

• Templates and demos available - local

Page 3: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UPAS - MATLAB Physics 3

Matlab Windows

● Workspace, command history, command window and editor – desktop layout

■ Open .m files, editor color coded■ Debug options■ In command window – set path to scripts

(.m files)● Setup toolbars, status busy/ready. Help● Use product help and function browser● Use demos – e.g. getting started, quick

start

Page 4: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UPAS - MATLAB Physics 4

Desktop Layout

Page 5: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UPAS - MATLAB Physics 5

MATLAB Startup

● Getting Started■ Functions■ Examples■ Demos

● Quick start■ Language■ Math■ Graphics■ Programming

Page 6: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UPAS - MATLAB Physics 6

Help Page – Top of Tree

Page 7: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UPAS - MATLAB Physics 7

Demos and Getting Started

Page 8: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UPAS - MATLAB Physics 8

Drill Down in HELP - Examples

Page 9: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UPAS - MATLAB Physics 9

Programming - GUI

• Demo exists – many tools supplied

Page 10: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 10

MATLAB Displays

● MATLAB has a suite of displays● 2d

■ Errorbar, plot, loglog, semilogx, semilogy, polar, bar, pie, hist, scatter

■ title, xlabel, ylabel, legend, grid, hold, axis● 3d

■ Contour, surface, mesh, quiver

Page 11: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 11

Help - Graphics

Page 12: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 12

Help – 3 d

Page 13: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 13

Plot details, Axis definition

Page 14: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 14

plot(x,y) –

Page 15: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 15

3-d Plots

Page 16: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 16

Mesh and Meshc

Page 17: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 17

Surfc and quiver

Page 18: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 18

MATLAB Symbolic Tools

● MATLAB has many, many tools. You will have to browse through the options.

■ Diff, int (calculus)■ Matrix inversion■ Eigenvalues■ Taylor■ Fourier – our scripts

● Other examples and script in the textbook

Page 19: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 19

SM_Diff and SM_Int

● Use MATLAB diff, intyou need neverdo another integral

Page 20: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 20

SM_Diff Script “loop” -blue

● ~ standard “loop”● Use “while”

“break”“for”“if”“end”

Page 21: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 21

Eigenvalues

You need neversolve anothereigenvalueproblem

Page 22: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 22

Eigen Script

● Used diff and int for the 2 previous examples● Now use MATLAB matrix tools, det, inv and eig

Page 23: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 23

Taylor Series

● See convergence of the series graphically – select the number of terms

ftay = taylor(sym(fin),x,aoff,'Order',nterm); yy = simple(ftay); pretty(yy)

Page 24: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 24

Fourier - 1

● Examples worked out already – square, triangle and sawtooth. See convergence with number of terms

Page 25: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 25

Fourier - 2

● Use int, sym and eval to do a general Fourier series. Any analytically solvable series!

Page 26: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 26

Fourier - 3

● Example for f(t) = t*cos(t), 5 terms

Page 27: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 27

Numeric Tools - I

● Most problems must be solved numerically● Numeric tools for a vector x

■ Length■ Min■ Max■ Mean■ Std■ sort

Page 28: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 28

Numeric Tools- II

● MATLAB utility “quad” does numeric integration. Try “int” first – default to “quad” if it fails.

Page 29: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 29

MATLAB Equation Solvers

● MATLAB has several ways to solve equations

■ Symbolic solutions should be tried first■ Define symbolic variables using “syms”■ “solve” is for algebraic equations■ “dsolve” is for ordinary differential equations■ “ode45” is for numerical solutions to ODE■ “pde” is for partial differential equations in 1

space (x) and 1 time (t) dimension

Page 30: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 30

Help for solve – has examples

● Use help and search for solve

● Symbolic solution

● You need never solve another ODE

Page 31: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 31

solve -2

● Use command window to define a simple quadratic

● Use “simple” and “pretty for visualization

Page 32: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 32

dsolve

● Solves ODE symbolically with IC

Page 33: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 33

Dsolve - example

● Use symbolically for ODE. Initial conditions

Page 34: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 34

SM_ODE3

● SM_ODE2 allows a choice of a few well known equations. SM_ODE3 is free form.

Page 35: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 35

ode45

● Numerical solution of ODE

Page 36: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 36

ode45 - 2

Need to define a function which specifies the ODE

Page 37: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 37

ode45 - 3

● The physics is all in the function specified. The boundary conditions are specified in the ode45 call as is the time evolution of the system. We do examples later.

Page 38: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 38

pde

● PDE solver in MATLAB

Page 39: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 39

pde-2

● Example from 1-d Quantum Mechanics

Page 40: MATLAB Physics - Iuspas.fnal.gov/materials/14UNM/Matlab_1.pdf · MATLAB Physics - I MATLAB and Symbolic Math should be installed on an accessible computer – you will execute the

06/15/14 UIC – MATLAB Physics 40

Special Functions

● You need never again look up a special function! Search MATLAB help (mfun)

● Run script DG/SM ” Matlab_Functions”