Presentation is loading. Please wait.

Presentation is loading. Please wait.

IC3003 B ASIC S CIENTIFIC C OMPUTING Lecture 1 Monday 08:30-11:30 U204a.

Similar presentations


Presentation on theme: "IC3003 B ASIC S CIENTIFIC C OMPUTING Lecture 1 Monday 08:30-11:30 U204a."— Presentation transcript:

1 IC3003 B ASIC S CIENTIFIC C OMPUTING Lecture 1 Monday 08:30-11:30 U204a

2 2 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 C OURSE O UTLINE

3 3 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 6. Construct graphic user interface O UTCOME – BASE L EARNING

4 L ECTURE 1 I NTRODUCTION

5 5 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……

6 6 I NTRODUCTION 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

7 7 I NTRODUCTION MATLAB software interface Command Window Current Directory / Workspace Command History

8 8 I NTRODUCTION Current Directory / Workspace Files in Current Directory

9 9 I NTRODUCTION Current Directory / Workspace Store the variables not saved The data is not saved after you exit MATLAB. All data can be saved by : “save Workspace As” 1.Selecting “save Workspace As” from the File menu save 2.Using save function – This is used for saving all data to MAT-File

10 10 I NTRODUCTION Command History Display all previous command Double click the commands in command history window will also re-activate that command

11 11 I NTRODUCTION Command Window Show the result

12 12 I NTRODUCTION Command Window format compact MATLAB inserts a blank line to separate command lines. You can eliminate it by using format compact Type the command Show the result Pi or  Command Remove blank lineFormat compact 3.14159265358979Format long (15 digits) 3.1416Format short (5 digits) 3.14Format bank (2 decimal place) 335/113Format rat (Ratio)

13 13 I NTRODUCTION M-File Programming

14 14 I NTRODUCTION M-File Programming The new window pump up

15 15 I NTRODUCTION 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

16 16 I NTRODUCTION Simple Mathematics Functions +, -, *, /, \,^ pi=  sin(pi) cos(pi) tan(pi) sqrt(100) factorial(5) factor(100) primes(100) randperm(10)

17 17 I NTRODUCTION 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

18 18 I NTRODUCTION 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

19 I NTRODUCTION 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

20 V ARIABLE MATLAB variables are created when they appear on the left of an equal sign >>variable = expression Variables include Scalars Scalars Vectors Vectors Matrices Matrices Strings 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 “x” “X” MATLAB variable names are case sensitive so “x” and “X” are different variables 20

21 S CALARS scalar one row and one column 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; Scalar OperationStatement Additiona=x+y Subtractions=x-y Multiplicationm=x*y Divisiond=x/y Inversei=x\y Powerp=x^y 21

22 S CALARS Example: Compute 5sin(2.5 3-pi )+1/75 >>5*sin(2.5^(3-pi))+1/75 ans= 3.8617 Example: Compute 1\3(cos3 pi ) >>1\3*cos(3^pi) ans= 2.9753 22

23 V ECTORS vector one row or one column 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= 11111 Zeros To create a column vector of length 5 with single row >>y=zeros(5,1) x= 0 The first one is no. of row The second one is no. of column 23

24 V ECTORS Linspace To create a linearly spaced element >>x=linspace(1,5,5) x= 12345 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 24

25 V ECTORS Addressing vector elements Location of vector can be address by using >>x=linspace(11,15,3) x= 111315 >>x(2) ans= 13 >>x(end) ans= 15 25

26 V ECTORS 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= 12345 >>x(7)= -9 x= 12345 0-9 X(6) is assigned to zero 26

27 V ECTORS 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.15.1 27

28 V ECTORS Colon notation Location of vector can be address by using >>x=1:10; >>x(1:2:end) ans= 1 3579 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 28

29 M ATRICES matrix more than one row and one column A matrix is a variable with more than one row and one column Creating 2 by 2 matrix >>A=[1 2; 3 4] A= 12 34 Creating 2 by 3 matrix >>A=[1 2 3; 4 5 6] A= 123 456 29

30 M ATRICES Addressing matrix elements >>A=[1 2 3; 4 5 6; 7 8 9] A= 123 456 789 >>A(2,3) ans= 6 >>A(3,2)=-5 A= 123 456 7-59 The first one is no. of row The second one is no. of column 30

31 S TRINGS string A string is a word >>a='test' a= test Converts to ASCII code for simple calculation >>a+a ans= 232202230232 Apply the string >>[a a] ans= testtest 31

32 M ATHEMATICAL O PERATION 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= 927 4710 5128 Subtraction >>Y=X-A Y= 816 357 492 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. 32

33 M ATHEMATICAL O PERATION Multiplication >>u=[2 3 4]; >>v=[-2 0 2]'; >>x=u*v x= 4 >>x=v*u x= -4-6-8 000 468 u*v  v*u 33

34 M ATHEMATICAL O PERATION Element by Element Operations(.* or./ or.^) >>a=[1 2 3]; b=[2 5 8]; >>a+b ans= 3 711 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 34

35 M ATHEMATICAL O PERATION Element by Element Operations(.* or./ or.^) >>a=[1 2 3]; b=[2 5 8]; >>a.*b ans= 2 1024 It also needs to apply at.^ >>a.^2 ans= 149 If forget the period will lead to : >>a^2 ??? Error using ==> mpower Matrix must be square. 35

36 M ATHEMATICAL O PERATION Solving Linear Equation ax + by + cz = p dx + ey + fz = q gx + hy + iz = r Set Matrices, abcxp A=def,B=y and C=q ghizr A  B = C B=A -1 C 36

37 M ATHEMATICAL O PERATION For example, x + y + z = 0 x - 2y + 2z = 4 x + 2y - z = 2 Set Matrices, 111x0 A=1 -22,B=y and C=4 12 -1z2 >>A=[1 1 1;1 -2 2; 1 2 -1]; >>C=[0 4 2]'; >>B=inv(A)*C B= 4.000 -2.000 Therefore the linear system has one solution: X=4, y=-2 and z=-2 37


Download ppt "IC3003 B ASIC S CIENTIFIC C OMPUTING Lecture 1 Monday 08:30-11:30 U204a."

Similar presentations


Ads by Google