MATH 3341: Introduction to Scientific Computing Lab Libao Jin - - PowerPoint PPT Presentation

math 3341 introduction to scientific computing lab
SMART_READER_LITE
LIVE PREVIEW

MATH 3341: Introduction to Scientific Computing Lab Libao Jin - - PowerPoint PPT Presentation

Lab 02: Variables, Arrays, and Scripts MATH 3341: Introduction to Scientific Computing Lab Libao Jin University of Wyoming February 05, 2020 1 / 36 Script Files Variables Lab 02: Variables, Arrays, and Scripts Arrays Additional Functions


slide-1
SLIDE 1

Lab 02: Variables, Arrays, and Scripts

MATH 3341: Introduction to Scientific Computing Lab

Libao Jin

University of Wyoming

February 05, 2020

1 / 36

slide-2
SLIDE 2

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Lab 02: Variables, Arrays, and Scripts

2 / 36

slide-3
SLIDE 3

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Script Files

3 / 36

slide-4
SLIDE 4

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

A script file is simply a file that contains a chain of commands that you edit in a separate window, then execute with a single mouse click or command. This is where we can define variables, perform calculations and leave commands to remind us what the file calculates.

4 / 36

slide-5
SLIDE 5

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

File Naming Conventions

“The rules are exactly the same as for variable names: start with a letter, followed by letters or numbers or underscore, maximum 63 characters (excluding the .m extension), and must not be the same as any MATLAB reserved word.” “None of the conventions matter to MATLAB itself: they only matter to the people writing the code, and the people maintaining the code (usually a much harder task), and to the people paying for the code (you’d be amazed how much gets written into contract specifications.)” Reference: https://www.mathworks.com/matlabcentral/answers/ 30223-what-are-the-rules-for-naming-script-files

5 / 36

slide-6
SLIDE 6

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Put Comments to Your Script File

% MATH 3341, Spring 2020 % Lab 02: Variables, Arrays, and Scripts % Author: first_name last_name % Date: 02/05/2020

6 / 36

slide-7
SLIDE 7

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Variables

7 / 36

slide-8
SLIDE 8

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Variables help us represent quantities or expressions in order to make their use and re-use more convenient.

8 / 36

slide-9
SLIDE 9

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Naming Variables

Must start with a letter. Followed by letters (a-z, A-Z) or numbers (0-9) or underscores ( ). Maximum 63 characters (excluding the .m extension). Must not be the same as any MATLAB reserved word. Space is not permitted. Case sensitive, i.e., a ˜= A.

9 / 36

slide-10
SLIDE 10

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Naming Variables

Be as descriptive as possible with your variable names. Avoid built-in function/variable names (reserved keywords) such as pi, sin, exp, etc. Check if a name is already in use: which variableName or exist variableName.

10 / 36

slide-11
SLIDE 11

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Naming Conventions

snake case: writing compound words or phrases in which the elements are separated with one underscore character ( ) and no spaces, e.g. “foo bar”. camelCase: writing compound words or phrases such that each word or abbreviation in the middle of the phrase begins with a capital letter, with no intervening spaces or punctuation, e.g. “fooBar” Other conventions: Hungarian notation, positional notation, etc. Reference: https://en.wikipedia.org/wiki/Naming_ convention_(programming)

11 / 36

slide-12
SLIDE 12

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Default Variable Definitions

Command Description pi variable defining π i or j imaginary number i = √−1

12 / 36

slide-13
SLIDE 13

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Arrays

13 / 36

slide-14
SLIDE 14

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Array, Vectors, and Matrices

An array is a data form that can hold several values, all of one type. A vector is a 1-D array: we can define row vectors, column vectors. A matrix is a 2-D array. Also, we can define N-D array. The general notation for a vector or matrix is a list of values enclosed in square brackets [] separated by commas (space) or semi-colons (or the combination).

14 / 36

slide-15
SLIDE 15

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Vectors

Row vector, e.g., x =

  • 1

2 3 4

  • x = [1,2,3,4]

x = [1 2 3 4]

Column vector, e.g., x =

    

1 2 3 4

     or x =

  • 1

2 3 4

⊤ x = [1;2;3;4] x = [1 2 3 4]' where ' is the infix notation for transpose

  • peration in MATLAB.

15 / 36

slide-16
SLIDE 16

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Matrices

Define a matrix, e.g., A =

  • 1

2 3 4

  • A = [1,2;3,4]

16 / 36

slide-17
SLIDE 17

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Generate a Subarray using Slicing

a = [1,2,3;4,5,6;7,8,9] b = a(1,1) % b = [1] c = a(:,1) % c = [1;4;7] same as c = a(1:3,1) d = a(2:end,2:end) % d = [5,6;8,9] same as d = a(2:3,2:3)

17 / 36

slide-18
SLIDE 18

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Generate a 3-D Array using Slicing

A = [1,2;3,4] B = [5,6;7,8] C(:,:,1) = A or C(:,:,1) = [1,2;3,4] C(:,:,2) = B or C(:,:,2) = [5,6;7,8]

18 / 36

slide-19
SLIDE 19

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Concatenate Arrays

a = [1,2,3] b = [4,5,6] c = [a,b] % c = [1,2,3,4,5,6] d = [a;b] % d = [1,2,3;4,5,6] e = [d;d] % e = [1,2,3;4,5,6;1,2,3;4,5,6] f = [d,d] % f = [1,2,3,1,2,3;4,5,6,4,5,6]

19 / 36

slide-20
SLIDE 20

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

String: Array of Characters

s = 'abc' t = ['a' 'b' 'c'] s == t % return logical 1 [s t] % return 'abcabc' [s;t] % return ['abc';'abc']

20 / 36

slide-21
SLIDE 21

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Cell Arrays

s1 = {'abc', 'def'} vs. t1 = ['abc', 'def'] s2 = {'abc'; 'def'} vs. t2 = ['abs'; 'def'] s3 = {'ab', 'cd'; 'ef', 'gh'} s3{1,1} % 'ab' cell(n): create 1-D cell array of length n cell(m,n): create 1-D cell array of size m by n

21 / 36

slide-22
SLIDE 22

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Functions for Vectors & Matrices

Command Description linspace Linearly spaced vector logspace Logarithmically spaced vector colon or : Colon transpose or ’ Non-conjugate transpose of a vector eye Identity matrix

  • nes

Ones array zeros Zeros array rand Uniformly distributed pseudorandom numbers randn Normally distributed pseudorandom numbers magic Magic square diag Diagonal matrices and diagonals of a matrix reshape Reshape array size Size of array length Length of vector

22 / 36

slide-23
SLIDE 23

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Additional Functions and Commands

23 / 36

slide-24
SLIDE 24

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Command Description iskeyword Check if input is a keyword who List current variables whos List current variables, long form which Locate functions and files clear Clear variables and functions from memory clc Clear command window clf Clear current figure close Close figure exist Check existence of variable/script/function/folder/class disp Display array

24 / 36

slide-25
SLIDE 25

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Useful Functions for Homework 1

Command Description dot Vector dot product eig Eigenvalues and eigenvectors transpose or ’ Transpose fplot Plot 2-D function find Find indices of nonzero elements intmin Smallest integer value intmax Largest positive integer value realmin Smallest positive normalized floating point number realmax Largest finite floating point number

25 / 36

slide-26
SLIDE 26

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Useful MATLAB Shortcuts

Windows shortcuts

Press Ctrl + A to select all Press Ctrl + I to adjust indentation Press Ctrl + R to comment Press Ctrl + T to uncomment

macOS shortcuts

Press command + A to select all Press command + I to adjust indentation Press command + / to comment Press command + T to uncomment

26 / 36

slide-27
SLIDE 27

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

L

AT

EX Primer

27 / 36

slide-28
SLIDE 28

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

table Environment

\begin{table}[!hbtp] \caption{This is a table} \begin{tabular}{rcl} \toprule Column 1 & Column 2 & Column 3 \\ \midrule 1 & 1 & 1 \\ 12 & 12 & 12 \\ 123 & 123 & 123 \\ \bottomrule \end{tabular} \end{table}

28 / 36

slide-29
SLIDE 29

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

table Environment

Table 1:This is a table

Column 1 Column 2 Column 3 1 1 1 12 12 12 123 123 123

29 / 36

slide-30
SLIDE 30

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

figure Environment

\begin{figure}[!hbtp] \centering \includegraphics[height=0.3\textheight]{figure.pdf} \caption{Plot of $\sin{x}$} \label{fig:sin} \end{figure} generates

1 2 3 4 5 6 7

  • 1
  • 0.8
  • 0.6
  • 0.4
  • 0.2

0.2 0.4 0.6 0.8 1

Figure 1:Plot of sin x

30 / 36

slide-31
SLIDE 31

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

\left and \right vs. \big, \Big, \Bigg

\begin{align*} \|x\|_2 & = \big(\sum_{i = 1}ˆ{n} x_iˆ2 \big)ˆ{1/2}, \|x\|_2 = \Big(\sum_{i = 1}ˆ{n} x_iˆ2 \Big)ˆ{1/2}, \\ \|x\|_2 & = \Bigg(\sum_{i = 1}ˆ{n} x_iˆ2 \Bigg)ˆ{1/2}, \|x\|_2 = \left(\sum_{i = 1}ˆ{n} x_iˆ2 \right)ˆ{1/2}. \end{align*} generates x2 =

  • n
  • i=1

x2

i

1/2, x2 =

  • n
  • i=1

x2

i

1/2

, x2 =

  • n
  • i=1

x2

i

1/2

, x2 =

n

  • i=1

x2

i

1/2

.

31 / 36

slide-32
SLIDE 32

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Links

\href{https://www.google.com}{Google} Google Or simply \url{https://www.google.com} https://www.google.com

32 / 36

slide-33
SLIDE 33

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

case Environment

$$ f(x) = \begin{cases} 5 x + 4 & \text{if˜} x \leq 1, \\ 3 xˆ2 + 6 & \text{if˜} x > 1 \end{cases} $$ generates f(x) =

  • 5x + 4

if x ≤ 1, 3x2 + 6 if x > 1

33 / 36

slide-34
SLIDE 34

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Cross-Reference

\begin{equation} \label{eq:ls} A \mathbf{x} = \mathbf{b}. \end{equation} The expression \eqref{eq:ls} is a linear system. generates Ax = b. (1) The expression (1) is a linear system.

34 / 36

slide-35
SLIDE 35

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Cross-Reference

\begin{table}[!hbtp] \caption{$y = 2x$} \label{tab:xy} \begin{tabular}{cc} \toprule $x$ & $y$ \\ \midrule $6$ & $12$ \\ $7$ & $14$ \\ $8$ & $16$ \\ \bottomrule \end{tabular} \end{table} Table \ref{tab:xy} gives the result of $y = 2x$.

35 / 36

slide-36
SLIDE 36

Lab 02: Variables, Arrays, and Scripts Script Files Variables Arrays Additional Functions and Commands L

A

T EX Primer

Cross-Reference

Table 2:y = 2x

x y 6 12 7 14 8 16 Table 2 gives the result of y = 2x.

36 / 36