# channelflow.org

### Site Tools

gibson:teaching:fall-2014:math445:lecture2-diary

# Differences

This shows you the differences between two versions of the page.

 — gibson:teaching:fall-2014:math445:lecture2-diary [2014/09/04 07:21] (current)gibson created 2014/09/04 07:21 gibson created 2014/09/04 07:21 gibson created Line 1: Line 1: + <​code> ​ + % ================================================================== + % A few more matlab basics + % format: change appearance of output + % '​format long' makes matlab print in full (16 digit) precision + + pi + ans = + 3.1416 + + format long + pi + ans = + ​3.141592653589793 + + % Special numbers: inf, NaN, i, j + + % inf is infinity, for example 1 divided by 0 + 1/0 + ans = + Inf + + % NaN is 'not a number',​ for example 0 divided by 0, which is undefined + 0/0 + ans = + NaN + + % i and j are the unit imaginary numbers, the square root of -1 + i + ans = + 0.000000000000000 + 1.000000000000000i + + j + ans = + 0.000000000000000 + 1.000000000000000i + + i^2 + ans = + -1 + + % most common functions have extensions to the set of complex numbers + cos(2+3*i) + ans = + ​-4.189625690968807 - 9.109227893755337i + + + % caution: if you use a function name for a variable name, you won't be able + % to access the function until you clear the variable + + rand = rand() + rand = + ​0.090750827467831 + + + rand() + ans = + ​0.090750827467831 + + rand() + ans = + ​0.090750827467831 + + rand() + ans = + ​0.090750827467831 + + % hmmm, why do I keep getting the same random number? + % because you're access a variable named '​rand'​ and not the function '​rand()",​ silly! + + whos + Name          Size            Bytes  Class     ​Attributes + + ans           ​1x1 ​               16  double ​   complex ​ + rand          1x1                 ​8 ​ double ​             ​ + x         ​10000x1 ​            ​80000 ​ double ​             ​ + + % you need to run 'clear rand' to release the variable + + clear rand + + whos + Name          Size            Bytes  Class     ​Attributes + + ans           ​1x6 ​               12  char                ​ + x         ​10000x1 ​            ​80000 ​ double ​             ​ + + % now you can access the function again + rand() + ans = + ​0.546980919566268 + + rand() + ans = + ​0.895124242734885 + + % characters: one last data type, along with double, int32, uint32, ..., and logical + + % you can assign a set fo characters to a variable as follows + + f = '​foo'​ + f = + foo + + whos + Name          Size            Bytes  Class     ​Attributes + + ans           ​1x1 ​                ​8 ​ double ​             ​ + f             ​1x3 ​                ​6 ​ char                ​ + x         ​10000x1 ​            ​80000 ​ double ​             ​ + + + % ==================================================================== + % Vectors and matrices + + % Construct a row vector by explicitly listing its elements, separated by commas + x = [4, 5, 9] + x = + ​4 ​    ​5 ​    9 + + % Construct a column vector by explicitly listing its elements, separated by semicolons + x = [4; 5; 9] + x = + 4 + 5 + 9 + + % To access an element (component) of the vector, use parentheses + % x(i) accesses ith component of x + + x(1) + ans = + 4 + x(2) + ans = + 5 + x(3) + ans = + 9 + x(4) + {Index exceeds matrix dimensions.} % error message + + + % Transpose: the transpose operator ' (apostrophe) turns a row vector into a col vec + + x + x = + ​4 ​    ​5 ​    9 + + y = x'  ​ + y = + 4 + 5 + 9 + + y' + ans = + ​4 ​    ​5 ​    9 + + + % Matlab colon syntax + + % m:n means m through n by intervals of 1 + + 1:4 + ans = + ​1 ​    ​2 ​    ​3 ​    4 + + 1:10 + ans = + ​1 ​    ​2 ​    ​3 ​    ​4 ​    ​5 ​    ​6 ​    ​7 ​    ​8 ​    ​9 ​   10 + + + % x:inc:y means x through y by steps of inc + + 0:​0.2:​1 ​  % 0 through 1 by steps of 0.2 + + ans = + ​0 ​   0.2000 ​   0.4000 ​   0.6000 ​   0.8000 ​   1.0000 + + % use this functionality to produce plot of sin(x) for 0 <= x < pi + x = 0:​0.1:​pi; ​ + size(x) + ans = + ​1 ​   32 + plot(x,​sin(x),'​r-'​) + plot(x,​sin(x),'​r.-'​) + + + % linspace: another way to get a vector of uniformly spaced points + + x = linspace(0,​pi,​100); ​  % 100 uniformly distributed points btwn 0 and pi + size(x) + ans = + ​1 ​  100 + + plot(x,​sin(x),'​r.-'​) + + x(1) + ans = + 0 + x(2) + ans = + 0.0317 + x(3) + ans = + 0.0635 + x(4) + ans = + 0.0952 + + + % subindexing:​ how to extract a subset of the components of a vector + + % observe that x is a vector of dimension 100 and look at the values of + % its first four components + + size(x) + ans = + ​1 ​  100 + x(1) + ans = + 0 + x(2) + ans = + 0.0317 + x(3) + ans = + 0.0635 + x(4) + ans = + 0.0952 + + % recall that 1:4 means the vector [1, 2, 3, 4] + 1:4 + ans = + ​1 ​    ​2 ​    ​3 ​    4 + + % extract components 1,2,3,4 of x using syntax x(1:4) + x(1:4) + ans = + ​0 ​   0.0317 ​   0.0635 ​   0.0952 + + % extract components 1,2,3,4 of x using syntax x([1 2 3 4]), will give same thing + x([1 2 3 4]) + ans = + ​0 ​   0.0317 ​   0.0635 ​   0.0952 + + % Some more demonstrations of subindexing + x = 11:15 + x = + 11    12    13    14    15 + + x(1:3) + ans = + 11    12    13 + + x(3:5) + ans = + 13    14    15 + + x([5 4 3 2 1]) + ans = + 15    14    13    12    11 + + x(5:-1:1) + ans = + 15    14    13    12    11 + + x(randi(5,​1,​5)) + ans = + 15    14    14    13    12 + + + % Vector arithmetic: vectors add elementwise + x = [ 4 5 9] + x = + ​4 ​    ​5 ​    9 + + y = [1 2 0] + y = + ​1 ​    ​2 ​    0 + + x + y + ans = + ​5 ​    ​7 ​    9 + + % scalar multiplication + x + x = + ​4 ​    ​5 ​    9 + + 2*x + ans = + ​8 ​   10    18 + + % norm: measures the length of a vector + norm(x) + ans = + ​11.0454 + x + x = + ​4 ​    ​5 ​    9 + sqrt(4^2 + 5^2 + 9^2) + ans = + ​11.0454 + + + % Matrices + + % create a matrix literally ​ + A = [4, 5, 9 ; 3, 2, 1 ; 0 , 6,4] + A = + ​4 ​    ​5 ​    9 + ​3 ​    ​2 ​    1 + ​0 ​    ​6 ​    4 + + % Accessing components + A = [4, 5, 9 ; 3, 2, 1 ; 0 , 6,4] + A = + ​4 ​    ​5 ​    9 + ​3 ​    ​2 ​    1 + ​0 ​    ​6 ​    4 + + % A(i,j) gets elem in ith row and jth col + A(1,1) + ans = + 4 + + A(1,2) + ans = + 5 + + A(3,1) + ans = + 0 + + + % you can also assign a new number into a matrix element + A(3,1) = 99 + A = + ​4 ​    ​5 ​    9 + ​3 ​    ​2 ​    1 + 99     ​6 ​    4 + + % indexing with colons + % A(:,j) returns jth column + + A(:,1) + ans = + 4 + 3 + 99 + + A(:,2) + ans = + 5 + 2 + 6 + + A(:,3) + ans = + 9 + 1 + 4 + A + A = + ​4 ​    ​5 ​    9 + ​3 ​    ​2 ​    1 + 99     ​6 ​    4 + + % A(i,:) returns ith row + + A(1,:) + ans = + ​4 ​    ​5 ​    9 + + A(2,:) + ans = + ​3 ​    ​2 ​    1 + + A(3,:) + ans = + 99     ​6 ​    4 + + % A(i,m:n) returns ith row elements m through n + + A = + ​4 ​    ​5 ​    9 + ​3 ​    ​2 ​    1 + 99     ​6 ​    4 + + A(1,:) + ans = + ​4 ​    ​5 ​    9 + + A(1,2:3) + ans = + ​5 ​    9 + 