L01 Matlab1
L01 Matlab1
Textbook
Applied Numerical Methods With MATLAB for Engineers and Scientists
STEVEN C. CHAPRA
References
Numerical Methods For Engineers with Personal Computer Applications, (Third Edition) by Chapra, S.C. and R.P. Canale, McGraw-Hill, 1998 Numerical Methods with MATLAB : Implementations and Applications, Gerald W. Recktenwald, Prentice-Hall, 2000 An Introduction to Numerical Methods : A MATLAB Approach, Abdelwahab Kharab and Ronald B. Guenther, Chapman & Hall/CRC, 2002 Numerical Methods using MATLAB, John H. Mathews and Kurtis D. Fink, Prentice-Hall, 2004 The Matlab 7 Handbook , Mathwork Inc.
KEEP THESE BOOKS! They are excellent career references (at least for a while)
Topics Covered
Introduction to Matlab Approximations and Errors Roots of Equations Linear Systems Curve Fitting Interpolation Numerical Integration Ordinary Differential Equations Optimization
Conduct of Course
Homework/Projects/Quizzes Midterm Exam Final Exam 30 % 30 % 40 %
Grading Policy
Final Score 100 - 90 89 - 85 84 - 80 79 - 75 74 - 70 69 - 65 64 - 60 59 - 0 Grade A B+ B C+ C D+ D F
WARNINGS !!!
1) Participation expected, check by quizzes 2) Study in groups but submit work on your own 3) No Copying of Matlab code 4) Submit Homework at the beginning of class 5) Late homework with penalty 30% 6) No make up quizzes or exams
MATLAB
The Language of Technical Computing
M ATLAB
MATLAB
The latest version Matlab 7.3 R2006b
www.mathworks.com
MATLAB 1
- Getting started - Basic Arithmetic - Built-in Functions - Built-in Variables - Vector & Matrix
Getting Started
MATLAB Desktop
Getting Started
Command Window
Getting Started
Editor/Debugger
Basic Arithmetic
Calculator functions work as you'd expect:
>>(1+4)*3 ans = 15
+ and - are addition, / is division, * is multiplication, ^ is an exponent.
>> >>
5*5*5 5^(-2.5)
>> 3*(23+14.7-4/6)/3.5
Last-line editing Up Arrow
>> 2 + 6 - 4 ans = 4 The value in ans can be recalled: >> ans/2 ans = 2 Assign value to a variable: >> a = 5 >> b = 6 >> c = b/a
Upper & Lower Case >> a=2; >> A=3; >> 2*a >> 2*A Several commands in the same line: >>x=2;y=6+x,x=y+7
For too long command: >> Num_Apples = 10; >> Num_Orange = 25; >> Num_Pears = 12; >> Num_Fruit=Num_Apples+Num_Orange... + Num_Pears
Built-in Functions
>> sin(pi/4) ans = 0.7071 >> pi ans = 3.1416 sin(x), cos(x), tan(x), sqrt(x), log(x), log10(x), asin(x), acos(x), atan(x)
The output of each command can be suppress by using semicolon ; >> x = 5; >> y = sqrt(59); >> z = log(y) + x^0.25 z = 3.5341
The commas allow more than one command on a line: >> a = 5; b = sin(a), c = sinh(a) b = -0.9589 c = 74.2099 MATLAB Variables is created whenever it appears on the left-hand of = >> t = 5; >> t = t + 2 t = 7
Any variable appearing on the right-hand side of = must already be defined. >> x = 2*z ??? Undefined function or variable z >> who >> whos >> clear Use long variable names is better to remember and understandable for others >> radius = 5.2;
Format
>> pi >> format long >> pi >> format short >> format bank >> format short e >> format long e >> format compact >> format loose
Built-in Variables
Use by MATLAB, Should not be assigned to other values Variable ans eps i, j pi realmax realmin Inf NaN >> x = 0; >> 5/x >> x/x Meaning value of an expression when not assigned to variable floating-point precision unit imaginary numbers, i = j = = 3.14159265 . . . largest positive floating-point number smallest positive floating-point number , a number larger than realmax, result of 1/0 not a number (0/0) >> help log On-line Help: >> lookfor cosine
1
Renaissance engraving Melencolia I by the German artist and amateur mathematician Albrecht Drer.
Entering Matrices
You can enter matrices into MATLAB in several different ways: Enter an explicit list of elements. Load matrices from external data files. Generate matrices using built-in functions. Create matrices with your own functions in M-files. Start by entering matrix as a list of its elements. You only have to follow a few basic conventions: Separate the elements of a row with blanks or commas. Use a semicolon, ; , to indicate the end of each row. Surround the entire list of elements with square brackets, [ ] . To enter matrix, simply type in the Command Window >> A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
Arrays Operations
>> >> >> >> >> >> >> >> >> A = [ 3 5 7 9 11 ] A(3) length(A) clear(A) B = [ 2 4 6 8 10 ] A + B A - B A * B A .* B >> >> >> >> >> >> >> >> A / B A ./ B A .^ 2 odd = 1:2:11 even = 2:2:12 natural = 1:6 angle = 0:pi/10:pi; sin(angle)
Subscripts
The element in row i and column j of A is denoted by A(i , j) A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] MATLAB displays the matrix you just entered. A = 16 5 9 4 3 10 6 15 2 11 7 14 13 8 12 1
For example, A(4,2) is the number in the fourth row and second column. >> A(4,2) ans = 15
Submatrix
>> A(1,:) >> A(:,2) >> A(3:4,1:2)
Juxtaposition
>> B = [9 8 2 5 ; 4 5 6 7 ; 2 1 3 4] >> [A B] >> size(ans) >> [A ; B]
Linspace
linspace function creates row vectors with equally spaced elements. >> u = linspace(0.0,0.25,5) >> v = linspace(0,9,4) >> x = linspace(0,pi/6,6*pi); >> s = sin(x); >> c = cos(x); >> t = tan(x); >> [x s c t]
Workspace Browser
The MATLAB workspace consists of the set of variables (named arrays) built up during a MATLAB session and stored in memory. You add variables to the workspace by using functions, running M-files, and loading saved workspaces. To view the workspace and information about each variable, use the Workspace browser, or use the functions who and whos.
Solution:
>>d = [560, 440, 490, 530, 370] >>t = [10.3, 8.2, 9.1, 10.1, 7.5] >>speed = d./t speed = 54.3689
53.6585
53.8462
52.4752
49.3333