Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword or section
Like this
29Activity

Table Of Contents

0 of .
Results for:
No results containing your search query
P. 1
Introduction to Matlab

Introduction to Matlab

Ratings: (0)|Views: 260 |Likes:
Published by thesuperleo

More info:

Published by: thesuperleo on Sep 28, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

10/20/2011

pdf

text

original

 
INTRODUCTION TO MATLAB
Ross L. SpencerDepartment of Physics and AstronomyBrigham Young Universityc
2000 Ross L. Spencer and Brigham Young UniversityThis is a tutorial to help you get started in Matlab. To find more details see the veryhelpful book
Mastering MATLAB 6 
by Duane Hanselman and Bruce Littlefield. Examplesof Matlab code in this pamphlet are in typewriter font
like this
. As you read through thesections below type and execute in Matlab all of the examples, either at the
commandline prompt or in a test program you make called
test.m
. Longer sections of code areflagged with the characters %begin and %end. All of the Matlab code between these twoflags can be found in the file
tutorial.m
which you can find on the Physics 330 web pageat www.physics.byu.edu.This booklet can also be used as a reference manual because it is short, it has lots of examples, and it has a table of contents and an index. It is almost true that the basics of Matlab are in sections 1-9 while physics applications are in sections 9-17. Please tell meabout mistakes and make suggestions to improve it (ross spencer@byu.edu).
 
Contents
1 Running Matlab 3
1.1 Starting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2 Its a Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.3 Making Script Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.4 Running Script Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.5 Pause command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.6 Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.7 Making Matlab Be Quiet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.8 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.9 Arranging the Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.10 Sample Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81.11 Breakpoints and Stepping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2 Variables 9
2.1 Numerical Accuracy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.2
π
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.3 Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.4 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.5 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3 Input, Calculating, and Output 11
3.1 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.2 Calculating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.3 Add and Subtract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.4 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.5 Complex Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.6 Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.7 Housekeeping Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143.8 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4 Arrays and x-y Plotting 16
4.1 Colon (:) Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164.2 xy Plots, Labels, and Titles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174.3 Overlaying Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184.4 xyz Plots: Curves in 3-D Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184.5 Logarithmic Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194.6 Generating Multiple Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194.7 Controlling the Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204.8 Greek Letters, Subscripts, and Superscripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204.9 Changing Line Widths, Fonts, Etc.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5 Surface, Contour, and Vector Field Plots 21
5.1 Meshgrid and Ndgrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215.2 Contour Plots and Surface Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225.3 Evaluating Fourier Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255.4 Vector Field Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
6 Vector Products, Dot and Cross 28
 
7 Linear Algebra 28
7.1 Solve a Linear System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287.2 Max and Min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297.3 Matrix Inverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297.4 Transpose and Hermitian Conjugate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297.5 Special Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307.6 Determinant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317.7 Norm of Vector (Magnitude) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317.8 Sum the Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317.9 Selecting Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317.10 Eigenvalues and Eigenvectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327.11 Fancy Stu. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
8 Polynomials 32
8.1 Roots of a Polynomial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328.2 Find the polynomial from the roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338.3 Multiply Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338.4 Divide Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338.5 First Derivative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338.6 Evaluate a Polynomial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338.7 Fitting Data to a Polynomial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
9 Loops and Logic 34
9.1 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359.1.1 Summing a series with a
for
loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359.1.2 Products with a
for
loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369.1.3 Recursion relations with
for
loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369.2 Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379.3 Secant Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389.4 Using Matlabs Fzero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
10 Derivatives and Integrals 42
10.1 Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4210.2 Denite Integrals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4410.3 Matlab Integrators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
11 Interpolation and Extrapolation 47
11.1 Linear Interpolation and Extrapolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4711.2 Quadratic Interpolation and Extrapolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4811.3 Interpolating With
polyfit
and
polyval
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4911.4 Matlab Commands Interp1 and Interp2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
12 FFT (Fast Fourier Transform) 53
12.1 Fourier Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5312.2 Matlabs FFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
13 Make Your Own Functions: Inline and M-les 56
13.1
Inline Functions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5713.2
M-file Functions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5713.3 Derivative Function
derivs.m
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5813.4 Definite Integral Function
defint.m
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5913.5 Indefinite Integral Function
indefint.m
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
14 Fitting Functions to Data 6215 Systems of Nonlinear Equations 65

Activity (29)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads
Rajesh Verma liked this
Ayushi Jain liked this
Kartik Totlani liked this
reynantebitas liked this
davandrei liked this
davandrei liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->