Numerical Methods MATLAB Notes
INDEXING
Use the following table
data = 4x7
3.0000 18.0000 19.0000 20.0000 21.0000 23.0000 38.0000
0.5300 1.7800 6.6678 0.8600 2.5400 6.1100 4.7243
1.1456 1.2446 5.5300 4.1478 5.1236 8.4888 0.1456
6.5454 8.8889 5.4789 6.4569 6.4563 6.5478 1.2254
alpha =
0.2077 0.4709
0.3012 0.2305
delta =
0.8443 0.2259
0.1948 0.1707
● Extracting values from a array
○ x = chart’s variable name (row, column)
○ x = data(4, 3)
■ x = 5.4789
● Use the keyword ‘end’ to reference last element
○ y = chart’s variable name (end column, column)
○ y = data(end,2)
■ y = 8.8889
● Arithmetic with ‘end’
○ z = chart’s variable name(second to last, end, column)
○ z = data(end-1, 3)
■ z = 5.4789
● Colon operator and indexing: will specify ALL the elements
○ x = chart’s name (colon, column)
○ x = data(:, 1)
3.0000
0.5300
1.1456
6.5454
● Colon operator & indexing, specifying a range of values: specifies ALL elements
○ y = chart’s name (columns, colon) ✩ form is interchangeable
○ y = A(:, 3:4)
19.0000 20.0000
6.6678 0.8600
5.5300 4.1478
5.4789 6.4569
● Reference a specific element with indexing
○ z = chart’s name, either a row or column (element)
○ z = data(3)
1.1456
● Reference multiple elements with indexing range
○ y = chart’s name (element range, use a colon)
○ y = data(3:5) ☆ number count
18.0000
1.1456
6.5454
● Colon operator and indexing
○ z = chart’s name (row’s full range using a colon, end value of column)
○ z = data(:,end)
38.0000
4.7243
0.1456
1.2254
● Colon operator and indexing
○ z = chart’s name (row range, colon, end value of column)
○ z = data(10:end)
20.0000 21.0000 23.0000 38.0000
6.6678 0.8600 2.5400 6.1100 4.7243
5.5300 4.1478 5.1236 8.4888 0.1456
5.4789 6.4569 6.4563 6.5478 1.2254
● Change an element in a variable
○ chart’s name(row) = new value
○ data(2) = 2.1759
3.0000
2.1759
1.1456
6.5454
● Use indexing to change the values in a matrix
■ Existing chart
3.0000 18.0000 NaN
0.5300 1.7800 6.6678
○ chart’s name (row value, end value of column) = new value
○ data(1,end) = 21.000
3.0000 18.0000 21.000
0.5300 1.7800 6.6678
● Change array values to equal other elements using indexing
○ chart’s name 1 (row) = chart’s name 2 (row)
○ alpha (1) = delta (2)
0.1948 0.4709
0.3012 0.2305
ARRAY CALCULATIONS
Use the following tables
data = 4x7
3.0000 18.0000 19.0000 20.0000 21.0000 23.0000 38.0000
0.5300 1.7800 6.6678 0.8600 2.5400 6.1100 4.7243
1.1456 1.2446 5.5300 4.1478 5.1236 8.4888 0.1456
6.5454 8.8889 5.4789 6.4569 6.4563 6.5478 1.2254
alpha = 2x2
0.2077 0.4709
0.3012 0.2305
delta = 2x2
0.8443 0.2259
0.1948 0.1707
● Add a scalar value to all elements of an array
○ x = chart’s name + value you want to add
○ x = alpha + 1
1.2077 1.4709
1.3012 1.2305
● Add two arrays of the same size
y = alpha + delta
1.052 0.6968
0.4960 0.4012
● Multiply or divide ALL elements by a scalar
○ z = alpha / 2
0.1039 0.1130
0.1506 0.0854
● Find the maximum value with the ‘max’ function
○ x = max function(chart’s name)
○ x = max(alpha)x =
0.4709
● Perform math operations on the entire vector or array
○ y = desired math input (table’s name)
○ y = sqrt(alpha)y =
0.4557 0.6862
0.5488 0.4801
○ y = round(alpha)y =
0 0
0 0
● Matrix multiplication of two equally sized vectors - MUST use the ‘ * ’ function
○ Use the DOT operator for ‘element to element’ operations
■ Multiplying (.*) , dividing (./) , subtraction (.-) , exponential (.^)
○ z = chart name 1 .* chart name 2
○ z = alpha .* delta
0.1754 0.1064
0.0587 0.0393
○ z = [1 2; 3 4; 5 6; 7 8] .* [1; 2; 3; 4]
1 (1x1) 2 (2x1)
6 (3x2) 8 (4x2)
15 (5x3) 18 (6x3)
28 (7x4) 32 (8x4)
● alpha = [0.2077 0.4709 ;0.3012 0.2305]
○
OBTAINING HELP
● Use ‘ randi ‘ : Uniformly distributed pseudorandom integers
○ x = randi function (random integers in range from [#1 - #2] , row, column)
○ x = randi([1-20], 5,7)
● Open the documentation using the ‘ doc ‘ function
○ Use when you know the name of the function
○ Use when you want more information about it
○ doc randi
● When a function name is unknown, use documentation using phrases
○ doc normally distributed numbers
● Search for ‘Line specification’ for more information on plotting symbols
CALLING FUNCTIONS
Use the following tables
data = 4x7
3.0000 18.0000 19.0000 20.0000 21.0000 23.0000 38.0000
0.5300 1.7800 6.6678 0.8600 2.5400 6.1100 4.7243
1.1456 1.2446 5.5300 4.1478 5.1236 8.4888 0.1456
6.5454 8.8889 5.4789 6.4569 6.4563 6.5478 1.2254
alpha = 2x2
0.2077 0.4709
0.3012 0.2305
alpha2 = 2x1
0.4709
0.2305
delta = 2x2
0.8443 0.2259
0.1948 0.1707
● Size function to an array to produce a output variable w array size in a two
element array
○ The first element is the number of rows
○ The second element is the number of columns
○ x = size function (chart’s name)
○ x = size(data)
■ x=4 7
● Size function to a matrix to produce either one or two output variable
○ Use brackets for more than one output
○ [variable name1 row, variable name2 column] = size funct. (chart’s name)
○ [dr, dc] = size(data)
■ dr = 4
■ dc = 7
● Max function to find the maximum value of a vector
○ [variable name1 row, variable name2 column] = size funct. (chart’s name)
○ [vMax, ivMax] = max(alpha)
○ vMax = 0.4709
○ ivMax = 3 ☆ row in which the ‘max’ value is at
● ‘Tilde’ to ignore specific outputs
y = alpha(:, 2)
[tilde, ivMax] = max(alpha2)
yMax = y(ivMax)
○
LOGICAL ARRAYS
Use the following tables
data = 4x7
3.0000 18.0000 19.0000 20.0000 21.0000 23.0000 38.0000
0.5300 1.7800 6.6678 0.8600 2.5400 6.1100 4.7243
1.1456 1.2446 5.5300 4.1478 5.1236 8.4888 0.1456
6.5454 8.8889 5.4789 6.4569 6.4563 6.5478 1.2254
alpha = 2x2
0.2077 0.4709
0.3012 0.2305
delta = 2x2
0.8443 0.2259
0.1948 0.1707
● Relational operators: 1 (true) 0 (false)
○ Less than (>) , greater than (<) , approximately equal to.(~=) , value is
equal to (==) ☆ remember, (=) assigns a value to a variable
○ x = mathematical variable < Relational operation > numerical variable
○ x = pi < 4
■ x = 1 (true)
● Relational operators: compare an array to a single scaler
○ y = chart’s name < Relational operation > numerical variable
○ y = alpha < 0.300
0.2077
0.2305
● Use logical array as array index to extract elements
○ z = chart’s name (chart’s name <Relational operation> numerical variable)
○ z = delta (delta < 0.300)
■ z=
0.1948
0.2259
0.1707
● Logical indexing w 2 different vectors
○ x = chart’s name1 (chart’s name2 <Relational operator> numeric variable)
○ x = delta (alpha < 0.300)
■ x=
0.2259
0.1948
● Reassign values in an array:
○ chart’s name (chart’s name <Relational operator> numeric variable) =
numeric variable
○ delta (delta == 0.2259) = 0.3000
■ the value must be THE specific number so it’ll update
0.8443 0.3000
0.1948 0.1707
○ delta (delta < 0.3000 = 0
■ the value does NOT have to be a specific number so it’ll update
0.8443 0
0 0
PLOTTING DATA
Use the following tables
Sample
3.0000 18.0000 19.0000 20.0000 21.0000 23.0000 38.0000
density
0.5300 1.7800 6.6678 0.8600 2.5400 6.1100 4.7243
v1
1.1456 1.2446 5.5300 4.1478 5.1236 8.4888 0.1456
v2
6.5454 8.8889 5.4789 6.4569 6.4563 6.5478 1.2254
mass1
0.6072 2.2154 36.8729 3.5671 13.0139 51.8666 0.6850
Mass2
3.4691 15.8222 36.5322 5.5529 16.3990 40.0071 5.7892
● Plot function: 2 vectors of the same length can be plotted against each other
○ plot (chart’s name1 (x-axis) , chart’s name2 (y-axis))
○ plot (sample, mass1)
■
● Plot function to specify the color, line style, and marker style using symbols in 2x quotes
○ plot(x(axis), y(axis), “r--o”)
○ plot(sample, mass2,'r*')
■ plot(sample, mass2,'r (color red) * (star(plot marker)) ')
● Plotting ONE line on TOP of another
○ plot(sample, mass1, ‘ks’)
hold on
■ plot(sample (x-axis), mass1 (y-axis), ‘k (black) s(square(plot marker))’)
hold on (plots will continue to go on the same axes)
● Hold off function: returns the default plot behavior, each plot gets its own axes
○ plot(sample, mass1, ‘ks’)
hold on
plot(sample, mass2, ‘r*’)
hold off
● Plotting a single vector by itself, MATLAB uses they vector values as the y-axis data &
sets the x-axis dato to range from 1 to n (the # of elements in the vector)
○ plot (y)
○ plot(v1)
● Plot function accepts optional additional inputs, property name & an associated value
○ plot (y, “LineWidth”, 5) ☆ linewidth of 5 → heavy line
plot (v1, “LineWidth”, 3)
● Can provide additional inputs to the plot function after the line specification
○ plot(x(axis), y(axis), “r--o” , “LineWidth” , number)
○ plot(sample, v1, ‘r--o’ , ‘LineWidth’ , 4
○ plot(sample, v1, ‘ro-’ , ‘LineWidth’ , 4
○ plot(sample, v1, ‘ro’ , ‘LineWidth’ , 4
● Histogram function
○ histogram(density, "FaceColor", "y")
● Labels can de added to plots with functions like ‘title’ with double quotes
title(“Plot Title”)
title(“Sample Mass)
● Ylabel function: add a label
○ ylabel(‘Mass(g)’)
● Legend function: add a legend to the plot
○ legend(“Exp A” , “Exp B”)
● plot(sample, mass1, ‘ks’)
hold on
plot(sample, mass2, ‘r*’)
hold off
title(“Sample Mass)
ylabel(‘Mass(g)’)
legend(“Exp A” , “Exp B”)
● Use a variable’s value in plot annotations by joining text with a variable
○ bar(data(3,:))
○ title("sample" + sample(3) + "data")
PROGRAMMING
Use the following tables
data = 4x7
3.0000 18.0000 19.0000 20.0000 21.0000 23.0000 38.0000
0.5300 1.7800 6.6678 0.8600 2.5400 6.1100 4.7243
1.1456 1.2446 5.5300 4.1478 5.1236 8.4888 0.1456
6.5454 8.8889 5.4789 6.4569 6.4563 6.5478 1.2254
alpha = 2x2
0.2077 0.4709
0.3012 0.2305
delta = 2x2
0.8443 0.2259
0.1948 0.1707
● IF block: only execute if TRUE
○ x = rand
if x > 0.5 (condition)
y=3 (body)
end
○ Use ‘==’ (is equal to) to check for equality variable
if x == 0.5
y == 3
end
○ Book example:
x = -4
if x >= 0
xSqrt = sqrt(x)
end
xSqrt = x^2
○ Book example: modify so the plotting code on lines 5-8 are executed only
when ‘doPlot’ is 1
if doPlot == 1
plot(density)
title(“Sample Densities”)
xticklabels(element)
ylabel(“Density (g/cm^3)”)
end
● ELSE block: conditions that are NOT met
○ x = rand
if x > 0.5 (condition)
y=3 (body)
else
y=4
end
○ Book example: modify the script so when the ‘if’ conditions is NOT
satisfied, the following line of code is executed
if doPlot == 1
plot(density)
title(“Sample Densities”)
xticklabels(element)
ylabel(“Density (g/cm^3)”)
else
disp(“The density of” + element + “is” + density)
end
● ELSEIF block: used after ‘if’ to ADD more conditions
○ if condition1
code
elseif condition2
code
end
○ Book example: include function equal to ‘1’
if doPlot == 1
plot(density)
title(“Sample Densities”)
xticklabels(element)
ylabel(“Density (g/cm^3)”)
elseif doDisplay == 1
end
○ Book example: include function equal to ‘0’
if doPlot == 0
plot (density)
title (“Sample Densities”)
xticklabels (element)
ylabel (“Density (g/cm^3)”)
elseif doDisplay == 0
end
☆ ‘unrecognized function or variable ‘doDisplay’
● FOR LOOPS: Loop will be executed (variable) times
○ for (loop counter) = start value : end value
disp (variable) (body)
end
○ Book example: execute the code 7 times
for idx = 1:7
hold on
plot (idx, density (idx), “*”)
hold off
pause (0.2)
end
○ Book example: use ‘length’ to execute the code over an unknown length
for idx = 1:length(density)
hold on
plot (idx, density (idx), “*”)
hold off
pause (1)
end
IMPORTING DATA
Use the following tables
data = 4x7
3.0000 18.0000 19.0000 20.0000 21.0000 23.0000 38.0000
0.5300 1.7800 6.6678 0.8600 2.5400 6.1100 4.7243
1.1456 1.2446 5.5300 4.1478 5.1236 8.4888 0.1456
6.5454 8.8889 5.4789 6.4569 6.4563 6.5478 1.2254
alpha = 2x2
0.2077 0.4709
0.3012 0.2305
delta = 2x2
0.8443 0.2259
0.1948 0.1707
● Assigning the result of a calculation to a table
○