IC3003 BASIC SCIENTIFIC COMPUTING Lecture 1 Monday 08:30-11:30 U204a COURSE OUTLINE Assessment 30% Test – (30 Multiple Choice in 45 mins) 40% Assignment (8 Exercises) 30% Log sheet (Workshop Report) including performance Lecture Weighting 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 2 OUTCOME – BASE LEARNING After completed the course, the students should be able to 1. Understand the MATLAB can solve mathematical & Scientific problem 2. Analyze the data by using 2D and 3D visualization plots to enhance graphic presentation 3. Import and export data with other application 4. Use M-file programming to construct user defined math functions 5. Construct programs with flow control functions 3 6. 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…… 5 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 6 INTRODUCTION MATLAB software interface Current Directory / Workspace Command History Command Window 7 INTRODUCTION Current Directory / Workspace Files in Current Directory 8 INTRODUCTION Current Directory / Workspace Store the variables The data is not saved after you exit MATLAB. All data can be saved by : 1. Selecting “save Workspace As” from the File menu 2. Using save function – This is used for saving all data to MAT-File 9 INTRODUCTION Command History Display all previous command Double click the commands in command history window will also re-activate that command 10 INTRODUCTION Command Window Show the result 11 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 Pi or Remove blank line 3.14159265358979 3.1416 3.14 335/113 Command Format compact Format long (15 digits) Format short (5 digits) Format bank (2 decimal place) Format rat (Ratio) 12 INTRODUCTION M-File Programming 13 INTRODUCTION M-File Programming The new window pump up 14 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 15 command window INTRODUCTION Simple Mathematics Functions +, -, *, /, \,^ pi= sin(pi) cos(pi) tan(pi) sqrt(100) factorial(5) factor(100) primes(100) randperm(10) 16 INTRODUCTION Round floating point numbers to integer round fix >>round(10.5) ans= 11 >>round(10.4) ans= 10 >>fix(-10.5) ans= -10 >>fix(10.4) ans= 10 17 INTRODUCTION Round floating point numbers to integer ceil floor >>ceil(10.5) ans= 11 >>ceil(-10.4) ans= -10 >>floor(10.5) ans= 10 >>floor(-10.4) ans= -11 18 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); 19 VARIABLE MATLAB variables are created when they appear on the left of an equal sign >>variable = expression Variables include Scalars Vectors Matrices Strings 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 20 MATLAB variable names are case sensitive so “x” and “X” are different variables Characteristics of MATLAB 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 Scalar Operation Statement >>x=6; >>y=3; >>a=x+y; >>s=x-y; >>m=x*y; >>d=x/y; >>i=y\x; >>p=x^y; Addition a=x+y Subtraction s=x-y Multiplication m=x*y Division d=x/y Inverse i=x\y Power p=x^y 21 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 22 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) The second one is x= no. of column 0 0 The first one is 0 no. of row 0 0 23 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 24 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 25 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 26 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= 27 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 28 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 29 MATRICES Addressing matrix elements >>A=[1 2 3; 4 5 6; 7 8 9] A= 1 2 3 4 5 6 7 8 9 The second one is >>A(2,3) no. of column ans= 6 >>A(3,2)=-5 A= 1 2 3 The first one is 4 5 6 no. of row 7 -5 9 30 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 31 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= Addition & Subtraction require 9 2 7 both matrices to have same 4 7 10 dimension. If dimensions are 5 12 8 incompatible, an error will display Subtraction >>Y=X-A Y= 8 1 3 5 4 9 6 7 2 >>C=[1:3; 4:6]; >>X=A+C 32 ??? 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 0 0 4 6 u*v v*u -8 0 8 33 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' It needs to transpose the second vector ans= 36 34 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. 35 MATHEMATICAL OPERATION Solving Linear Equation ax + by + cz = p dx + ey + fz = q gx + hy + iz = r Set Matrices, a b c A= d e f , g h i A B = C B=A-1C B= x p y and C= q z r 36 MATHEMATICAL OPERATION For example, x+y+z=0 x - 2y + 2z = 4 x + 2y - z = 2 Set Matrices, 1 1 1 A= 1 -2 2 , B= 1 2 -1 x 0 y and C= 4 z 2 >>A=[1 1 1;1 -2 2; 1 2 -1]; >>C=[0 4 2]'; >>B=inv(A)*C Therefore the linear system has one solution: B= 4.000 X=4, y=-2 and z=-2 37 -2.000 -2.000