370 likes | 492 Views
IC3003 Basic Scientific Computing. Lecture 1 Monday 08:30-11:30 U204a. Course Outline. Assessment Weighting 30% Test – (30 Multiple Choice in 45 mins) 40% Assignment (8 Exercises) 30% Log sheet (Workshop Report) including performance Lecture Notes & Reference Web site
E N D
IC3003Basic Scientific Computing Lecture 1 Monday 08:30-11:30 U204a
Course Outline • Assessment Weighting • 30%Test – (30 Multiple Choice in 45 mins) • 40% Assignment (8 Exercises) • 30% Log sheet (Workshop Report) including performance • Lecture Notes & Reference Web site • http://edc.ic.polyu.edu.hk • http://www.ic.polyu.edu.hk • http://www.mathworks.com • Using POLYU webmail to submit assignments
Outcome – base Learning • After completed the course, the students should be able to • Understand the MATLAB can solve mathematical & Scientific problem • Analyze the data by using 2D and 3D visualization plots to enhance graphic presentation • Import and export data with other application • Use M-file programming to construct user defined math functions • Construct programs with flow control functions • Construct graphic user interface
Lecture 1 Introduction
Introduction • MATLAB stands for Matrix Laboratory • Basic Scientific Calculation (Linear Algebra, Matrix ) • Applied Pure Mathematics (Calculus, Partial Fraction) • 2D Plotting • 3D Visualization • Curve Fitting • File Input / Output • Graphic User Interface, etc……
Introduction • MATLAB software version - MATLAB R2008a • The software icon in desktop • If you use previous version, it cannot open the file • Another method: • Use Notepad to save all file in the M-File format (XXX.m) • It cannot run in the Notepad, you just check the solution in MATLAB
Introduction • MATLAB software interface Current Directory / Workspace Command Window Command History
Introduction • Current Directory / Workspace Files in Current Directory
Introduction • Current Directory / Workspace Store the variables The data is not saved after you exit MATLAB. All data can be saved by : Selecting “save Workspace As” from the File menu Using save function – This is used for saving all data to MAT-File
Introduction • Command History Display all previous command Double click the commands in command history window will also re-activate that command
Introduction • Command Window Show the result
Introduction • Command Window Type the command Show the result MATLAB inserts a blank line to separate command lines. You can eliminate it by using format compact
Introduction • M-File Programming
Introduction • M-File Programming The new window pump up
Introduction • M-File programming can be saved and submitted the solution in a clear way • M-File programming has script or function file (stand alone file) • Command windows is only to check the solution directly • Note: When you do the exercise or assignment, you should use M-File to run the solution and you can see the answer and error in the command window
Introduction • Simple Mathematics Functions • +, -, *, /, \,^ • pi= • sin(pi) • cos(pi) • tan(pi) • sqrt(100) • factorial(5) • factor(100) • primes(100) • randperm(10)
Introduction • Round floating point numbers to integer • round • >>round(10.5) • ans= • 11 • >>round(10.4) • ans= • 10 • fix • >>fix(-10.5) • ans= • -10 • >>fix(10.4) • ans= • 10
Introduction • Round floating point numbers to integer • ceil • >>ceil(10.5) • ans= • 11 • >>ceil(-10.4) • ans= • -10 • floor • >>floor(10.5) • ans= • 10 • >>floor(-10.4) • ans= • -11
Introduction • It is convenience since it allows multiple statements to be executed without printing the intermediate results. You can suppress the result by using “semicolon - ;” at the end of each function, • >>pi/3; • >>sin(ans)/cos(ans); • >> ans-tan(pi/3);
Variable • MATLAB variables are created when they appear on the left of an equal sign • >>variable = expression • Variables include • Scalars • Vectors • Matrices • Strings • Characteristics of MATLAB variables: • Variable names must begin with an alphanumeric letter • Following that, any number of letters, digits and underscores can be added, but only the first 19 characters are retained • MATLAB variable names are case sensitive so “x” and “X” are different variables
Scalars • A scalar is a variable with one row and one column • >>a=4; • >>b=5; • >>c=a+b; • The following statements demonstrate scalar operation • >>x=6; • >>y=3; • >>a=x+y; • >>s=x-y; • >>m=x*y; • >>d=x/y; • >>i=y\x; • >>p=x^y;
Scalars • Example: Compute 5sin(2.53-pi)+1/75 • >>5*sin(2.5^(3-pi))+1/75 • ans= • 3.8617 • Example: Compute 1\3(cos3pi) • >>1\3*cos(3^pi) • ans= • 2.9753
Vectors • A vector is a matrix with either one row or one column • Creating vector of the following statements • Ones • To create a row vector of length 5 with single row • >>x=ones(1,5) • x= • 1 1 1 1 1 • Zeros • To create a column vector of length 5 with single row • >>y=zeros(5,1) • x= • 0 • 0 • 0 • 0 • 0 The second one is no. of column The first one is no. of row
Vectors • Linspace • To create a linearly spaced element • >>x=linspace(1,5,5) • x= • 1 2 3 4 5 • Logspace • To create a logarithmically spaced element • >>y=logspace(1,5,5) • y= • 10 100 1000 10000 100000 • The third argument of both linspace and logspace is optional. The third argument is the number of elements to be used between the range specified with the first and second arguments
Vectors • Addressing vector elements • Location of vector can be address by using • >>x=linspace(11,15,3) • x= • 11 13 15 • >>x(2) • ans= • 13 • >>x(end) • ans= • 15
Vectors • Increasing the size of vector • We can also increase the size of vector by simply assigning a value to an element • >>x=linspace(1,5,5) • x= • 1 2 3 4 5 • >>x(7)= -9 • x= • 1 2 3 4 5 0 -9 • X(6) is assigned to zero
Vectors • Colon notation • The format of this command is shown as below: • >>x=xbegin:dx:xend • Or • >> x=xbegin:xend • Where xbegin and xend are the range of values covered by elements of the x vector and dx is the optional increment. The default value of dx is (unit increment). The numbers xbegin, dx and xend may not be integers. • >>x=1.1:5.1 • x= • 1.1 2.1 3.1 4.1 5.1
Vectors • Colon notation • Location of vector can be address by using • >>x=1:10; • >>x(1:2:end) • ans= • 1 3 5 7 9 • To create a column vector, append the transpose operator to the end of the vector-creating expression • >>y=(1:5)' • y= • 1 • 2 • 3 • 4 • 5
Matrices • A matrix is a variable with more than one row and one column • Creating 2 by 2 matrix • >>A=[1 2; 3 4] • A= • 1 2 • 3 4 • Creating 2 by 3 matrix • >>A=[1 2 3; 4 5 6] • A= • 1 2 3 • 4 5 6
Matrices • Addressing matrix elements • >>A=[1 2 3; 4 5 6; 7 8 9] • A= • 1 2 3 • 4 5 6 • 7 8 9 • >>A(2,3) • ans= • 6 • >>A(3,2)=-5 • A= • 1 2 3 • 4 5 6 • 7 -5 9 The second one is no. of column The first one is no. of row
Strings • A string is a word • >>a='test' • a= • test • Converts to ASCII code for simple calculation • >>a+a • ans= • 232 202 230 232 • Apply the string • >>[a a] • ans= • testtest
Mathematical Operation • Addition • >>A=[1 1 1; 1 2 3; 1 3 6]; • >>B=[8 1 6; 3 5 7; 4 9 2]; • >>X=A+B • X= • 9 2 7 • 4 7 10 • 5 12 8 • Subtraction • >>Y=X-A • Y= • 8 1 6 • 3 5 7 • 4 9 2 • Addition & Subtraction require both matrices to have same dimension. If dimensions are incompatible, an error will display • >>C=[1:3; 4:6]; • >>X=A+C • ??? Error using ==> plus • Matrix dimensions must agree.
Mathematical Operation • Multiplication • >>u=[2 3 4]; • >>v=[-2 0 2]'; • >>x=u*v • x= • 4 • >>x=v*u • x= • -4 -6 -8 • 0 0 0 • 4 6 8 • u*v v*u
Mathematical Operation • Element by Element Operations(.* or ./ or .^) • >>a=[1 2 3]; b=[2 5 8]; • >>a+b • ans= • 3 7 11 • Let’s try to multiply • >>a*b • ??? Error using ==> mtimes • Inner matrix dimensions must agree. • >>a*b' • ans= • 36 It needs to transpose the second vector
Mathematical Operation • Element by Element Operations(.* or ./ or .^) • >>a=[1 2 3]; b=[2 5 8]; • >>a.*b • ans= • 2 10 24 • It also needs to apply at .^ • >>a.^2 • ans= • 1 4 9 • If forget the period will lead to : • >>a^2 • ??? Error using ==> mpower • Matrix must be square.
Mathematical Operation • Solving Linear Equation • ax + by + cz = p • dx + ey + fz = q • gx + hy + iz = r • Set Matrices, • a b c x p • A= d e f , B= y and C= q • g h i z r • A B = C • B=A-1C
Mathematical Operation • For example, • x + y + z = 0 • x - 2y + 2z = 4 • x + 2y - z = 2 • Set Matrices, • 1 1 1 x 0 • A= 1 -2 2 , B= y and C= 4 • 1 2 -1 z 2 • >>A=[1 1 1;1 -2 2; 1 2 -1]; • >>C=[0 4 2]'; • >>B=inv(A)*C • B= • 4.000 • -2.000 • -2.000 • Therefore the linear system has one solution: • X=4, y=-2 and z=-2