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

37
IC3003 BASIC SCIENTIFIC COMPUTING Lecture 1 Monday 08:30-11:30 U204a

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

IC3003BASIC SCIENTIFIC

COMPUTINGLecture 1

Monday 08:30-11:30U204a

2

Assessment Weighting30% Test – (30 Multiple Choice in 45 mins)40% Assignment (8 Exercises)30% Log sheet (Workshop Report) including

performanceLecture Notes & Reference Web site

http://edc.ic.polyu.edu.hkhttp://www.ic.polyu.edu.hkhttp://www.mathworks.com

Using POLYU webmail to submit assignments

COURSE OUTLINECOURSE OUTLINE

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 application4. Use M-file programming to construct user defined

math functions5. Construct programs with flow control functions6. Construct graphic user interface

OUTCOME – BASE OUTCOME – BASE LEARNINGLEARNING

LECTURE 1LECTURE 1INTRODUCTIONINTRODUCTION

5

INTRODUCTIONINTRODUCTION 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

INTRODUCTIONINTRODUCTION 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

INTRODUCTIONINTRODUCTION MATLAB software interface

Command WindowCommand Window

Current Current Directory / Directory / WorkspaceWorkspace

Command Command HistoryHistory

8

INTRODUCTIONINTRODUCTION Current Directory / Workspace

Files in Current Files in Current DirectoryDirectory

9

INTRODUCTIONINTRODUCTION Current Directory / Workspace

Store the Store the variablesvariablesThe data is not saved not saved after you exit MATLAB. All data can be saved by :1.Selecting “save Workspace As” “save Workspace As” from the File menu2.Using savesave function – This is used for saving all data to MAT-File

10

INTRODUCTIONINTRODUCTION Command History

Display all Display all previous previous commandcommandDouble click the commands in command history window will also re-activate that command

11

INTRODUCTIONINTRODUCTION Command Window

Show the resultShow the result

12

INTRODUCTIONINTRODUCTION Command Window

MATLAB inserts a blank line to separate command lines. You can eliminate it by using format compactformat compact

Type the Type the commandcommandShow the resultShow the result

Pi or CommandRemove blank line Format compact3.14159265358979 Format long (15 digits)3.1416 Format short (5 digits)3.14 Format bank (2 decimal place)335/113 Format rat (Ratio)

13

INTRODUCTIONINTRODUCTION M-File Programming

14

INTRODUCTIONINTRODUCTION M-File Programming

The new The new window pump window pump upup

15

INTRODUCTIONINTRODUCTION 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

INTRODUCTIONINTRODUCTION Simple Mathematics Functions

+, -, *, /, \,^ pi= sin(pi) cos(pi) tan(pi) sqrt(100) factorial(5) factor(100) primes(100) randperm(10)

17

INTRODUCTIONINTRODUCTION 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

INTRODUCTIONINTRODUCTION 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

INTRODUCTIONINTRODUCTION 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

VARIABLEVARIABLE MATLAB variables are created when they appear

on the left of an equal sign>>variable = expression

Variables includeScalarsScalarsVectorsVectorsMatricesMatricesStringsStrings

Characteristics of MATLAB variables:Variable names must begin with an alphanumeric

letterFollowing 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”“x” and “X”“X” are different variables

20

SCALARS SCALARS A scalarscalar is a variable with one row and one one row and one

columncolumn>>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 Operation

Statement

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 SCALARS Example: Compute 5sin(2.53-pi)+1/75

>>5*sin(2.5^(3-pi))+1/75ans=

3.8617Example: Compute 1\3(cos3pi)

>>1\3*cos(3^pi)ans=

2.9753

22

VECTORS VECTORS A vectorvector is a matrix with either one row or one one row or one

columncolumn Creating vector of the following statements

OnesTo create a row vector of length 5 with single row

>>x=ones(1,5)x=

1 1 1 1 1Zeros

To create a column vector of length 5 with single row>>y=zeros(5,1)x=

00000

The first one is The first one is no. of rowno. of row

The second one The second one is no. of columnis no. of column

23

VECTORS VECTORS Linspace

To create a linearly spaced element

>>x=linspace(1,5,5)x=

1 2 3 4 5LogspaceTo 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

VECTORS 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 VECTORS Increasing the size of vectorWe 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)= -9x=

1 2 3 4 5 0 -9

X(6) is assigned to zero 26

VECTORS VECTORS Colon notationThe format of this command is shown as below:>>x=xbegin:dx:xend Or>> x=xbegin:xendWhere 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.1x=

1.1 2.1 3.1 4.1 5.1

27

VECTORS 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=

12345

28

MATRICES MATRICES A matrixmatrix is a variable with more than one row more than one row

and one columnand one columnCreating 2 by 2 matrix>>A=[1 2; 3 4]A=

1 23 4

Creating 2 by 3 matrix>>A=[1 2 3; 4 5 6]A=

1 2 34 5 6

29

MATRICES MATRICES Addressing matrix elements

>>A=[1 2 3; 4 5 6; 7 8 9]A=

1 2 34 5 67 8 9

>>A(2,3)ans=

6>>A(3,2)=-5A=

1 2 34 5 67 -5 9

The first one is The first one is no. of rowno. of row

The second one The second one is no. of columnis no. of column

30

STRINGS STRINGS A stringstring is a word

>>a='test'a=test Converts to ASCII code for simple calculation>>a+aans=

232 202 230 232 Apply the string>>[a a]ans=testtest

31

MATHEMATICAL MATHEMATICAL OPERATIONOPERATION

Addition>>A=[1 1 1; 1 2 3; 1 3 6];>>B=[8 1 6; 3 5 7; 4 9 2];>>X=A+BX=

9 2 74 7 105 12 8

Subtraction>>Y=X-AY=

8 1 63 5 74 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 ==> plusMatrix dimensions must

agree.

32

MATHEMATICAL MATHEMATICAL OPERATIONOPERATION

Multiplication>>u=[2 3 4];>>v=[-2 0 2]';>>x=u*vx=

4

>>x=v*ux=

-4 -6 -80 0 04 6 8

u*v v*u

33

MATHEMATICAL MATHEMATICAL OPERATIONOPERATION

Element by Element Operations(.* or ./ or .^)>>a=[1 2 3]; b=[2 5 8];>>a+bans=

3 7 11 Let’s try to multiply>>a*b??? Error using ==> mtimesInner matrix dimensions must agree.>>a*b'ans=

36

It needs to transpose the second vector

34

MATHEMATICAL MATHEMATICAL OPERATIONOPERATION

Element by Element Operations(.* or ./ or .^)>>a=[1 2 3]; b=[2 5 8];>>a.*bans=

2 10 24 It also needs to apply at .^>>a.^2ans=

1 4 9If forget the period will lead to :>>a^2??? Error using ==> mpowerMatrix must be square.

35

MATHEMATICAL MATHEMATICAL OPERATIONOPERATION

Solving Linear Equationax + by + cz = pdx + ey + fz = qgx + hy + iz = r

Set Matrices,a b c x p

A= d e f , B= y and C= qg h i z r

A B = CB=A-1C

36

MATHEMATICAL MATHEMATICAL OPERATIONOPERATION

For example,x + y + z = 0x - 2y + 2z = 4x + 2y - z = 2

Set Matrices,1 1 1 x 0

A= 1 -2 2 , B= y and C= 41 2 -1 z 2

>>A=[1 1 1;1 -2 2; 1 2 -1];>>C=[0 4 2]';>>B=inv(A)*CB=

4.000-2.000-2.000

Therefore the linear system has one solution:

X=4, y=-2 and z=-237