What is programming?
– A set of instructions (recipe) on how to solve a problem
– A programming language is a language developed to express programs (with its own rules and syntax)
– How would you tell someone to make a peanut butter sandwich?


How can we talk to
Computers work a computer in 0s on binary number and 1s – we need system – 0s and programming
1s.
languages !
You need to be specific in the instructions you give to the computer !
– variables
– Symbolic name associated with a value
– Value of the variable is the data stored in that variable
– Variable names must start with a letter
– Case-sensitive ! ‘Test’ is not same as ‘test’


– Variables can store scalars, vectors, matrices, strings etc. – Use the equal sign to assign values to a variable

variables practice
>> m = 2; % scalar variable
>> x = [1 2 3]; % row vector
>> y = [4; 5; 6]; % column vector
>> z = [8 9 6]; % row vector
Try x’- what happens?
Single quotation mark ’ transposes arrays, i.e. the rows and columns are interchanged.
>> a = [2 3; 5 1]; % a matrix with rows and columns
>> b = [‘hello world’]; % string variable
Matrix operations
>> a = [2 3; 7 1] % First matrix
>> b = [9 7; 9 6] % Second matrix
>> a*b
>> a.*b
>> a./b
>> c = [1 2 3 4 5]
Try a*c What do you get?
– [ ] square brackets are used for creating vectors, arrays and matrices and ( ) round brackets are used for accessing elements.
[ ] and ( )
– [ ] square brackets are used for creating vectors, arrays and matrices and ( ) round brackets are used for accessing elements.
Try the following in MATLAB
>> r = [1 2 3 4; 7 8 9 4; 9 10 11 12] Q: How can you find the size of r?
>> r(2,3)
>> r(3,:)
>> r(2,4) = 13
>> r(1:2,2:3)
Logic statements
== equals
~= does not equal
> greater than
>= greater or equal than
< less than
<= less or equal than && and || or If you have no variables in the workspace, what would you get if you type the following in the command window? a == 2 a > b
Useful commands
find() – for finding specific elements a = [4 -2 0 0 0 1];
Q: Find positions of non-zero elements of a. Q: Find positions of positive elements of a.
sum – for finding sum of elements r = [1 2 3 4; 7 8 9 4; 9 10 11 12] Q: Find the sum of all values in all columns. Q: Find the sum of all values in all rows.
repmat – repeat copies of array a = [4 -2 0 0 0 1];
Q: Create a 5 x 5 matrix where the vector a gets replicated 5 times.
Other useful command:
eye (for making identity matrices); inv (for taking an inverse)

Putting knowledge into practice
Sustainability Assessment of an economy
Follow the steps below to calculate the total carbon footprint of an economy in MATLAB.
– Load the following files T, v, y and Q in the workspace.
– Calculate the total input and total output of the economy.
– Test if your input-output system is balanced.
– Calculate the direct intensity q
– Calculate the A matrix and the Leontief’s inverse L.
– Calculate the total intensity m
– Calculate the total footprint by sector.
– Calculate the total footprint of the economy.

More on MATLAB: Statements
– For setting conditions or repeating operations
– ‘if’, ‘else’ statements the most widely used
– Work for numbers and strings if logic expression
commands
end
if logic expression commands else commands
end
if logic expression commands
elseif logic expression commands >> a = 1;
>> if a == 1 MyName=‘Arunima’; if
MyName==‘Arunima’ disp(‘correct
name’); else disp(‘wrong name’) end
b = 2*a end a = 4; b = 3; if (a<b) j = -1; elseif (a>b)
j = 2;
end
end
More on MATLAB: Loops
– for statements loop a specific number of times, and keep track of each iteration with an incrementing index variable. See:
https://au.mathworks.com/help/matlab/matlab_prog/loop-controlstatements.html – Examples:
for x=1:1:9 x
end
x = ones(1,10); for n = 2:6 x(n) = 2 * x(n – 1); end
Practice:
– Given the vector x below, create a short set of commands using a ‘for’ loop that will add up the values of the elements (Same as sum function)
x = [1 8 3 9 0 1];
More on MATLAB: Nested Loops
– Nested for loops – loop inside a loop.
for m = 1:j for n = 1:k
end
end
– Example: Compute the sum of all numbers in a matrix. A = [8, 5; 4, 9; 1, 3]; sum = 0;
for (i = 1:3) % loop over rows of first matrix for (j = 1:2) % loop over columns of second matrix sum = sum + A(i,j); end end
More on MATLAB: Nested Loops Exercise
Create a M-by-N array of random numbers (use rand). Move through the array, element by element, and set any value that is less than 0.2 to 0 and any value that is greater than (or equal to) 0.2 to 1.
More on MATLAB: While Loop
– while repeats the execution of a group of statements in a loop while the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false. See: https://au.mathworks.com/help/matlab/ref/while.html
while expression commands
end
How many times will this code print Programming is fun?
n = 10; while n > 0 disp(‘Programming is fun’) n = n – 1; end
Dealing with multiple commands in MATLAB: M-files
– M-files allow multiple commands to be saved in one file. These commands can be executed to perform a specific task.
– Script files: Contain a sequence of commands to be run in order.
– Function files: Have to specify input data
– Useful for sharing your code with colleagues and collaborators
MATLAB scripts
– Let’s make a script file for the following:
n = 10; while n > 0 disp(‘Programming is fun’) n = n – 1;
end
– Remember to put comments.
– Come up with a MATLAB script and test if it runs.