Last Updated: 21 Apr 08, 7.35pm (final) ======================== Lectures by Dr Colin Tan ======================== Lecture 1A ============== -. GIYF = Google Is Your Friend. -. You will revisit slide 25-29 again in Lecture 2. Lecture 1B ============== -. Slide 6, DOTA = Defence of the Ancients, Warcraft (video game) term. -. Slide 8-21, To get to know more about how computer works, go to: http://computer.howstuffworks.com/pc.htm or do google searches. -. Slide 29-31, Easy way to verify your base number conversion if you forgot which one is which: MSB->LSB or LSB->MSB, just remember some easy binary numbers, e.g. 8 (decimal) = 1000 (binary) for simple verification purpose. -. Slide 36, 123.321 cannot be represented in binary EXACTLY. -. You can use Windows calculator to help you learn these base number stuffs. Lecture 2 ============== -. Spidey example has been shown previously in Lecture 1A. -. Slide 31, note: you cannot split string into two lines! This slide has two bugs! -. You do not need to type the code again, just load this into Matlab http://www.comp.nus.edu.sg/~stevenha/myteaching/spidey.m The result for bdg_height = 300 m and sp_weight = 80 kg http://www.comp.nus.edu.sg/~stevenha/myteaching/spidey.png The result will be different if bdg_height and sp_weight are different. Lecture 3A ============== -. To get you started with Matlab, it is best to visit computer labs in Faculty of Engineering, COM1/114, or SOC1/815 and try it by yourself. -. Visit: http://www.mathworks.com/support/product/demos_index_by_product.html?product=ML -. Slide 5, 3/4 = 0.75, 3\4 = 1.33 (same as 4/3) -. Slide 6, BODMAS = Brackets first, Orders (power or square roots), Division and Multiplication (left to right), finally Addition and Substraction (also left to right) -. Slide 15, Note that single quote is (') and not (`)! -. Slide 21, Chicken = $2.5 (quite straightforward) -. Slide 36, Bob gross monthly salary = $2892.50 Lecture 3B ============== -. Array/Vector/Matrix is first class citizen in Matlab, everything is actually an array. -. For those who do not know, remember these: ':' -> colon ';' -> semi colon '-' -> hyphen ''' or '`' -> apostrophe These may be familiar: ' ' -> space, '.' -> period, ',' -> comma -. Slide 9, unlike many other programming languages, Matlab arrays start from index 1 and not 0! Those with programming background with other languages must take note of this! -. Slide 10, we mention 'end', how about 'first' or 'start'? Just write '1' :) -. Slide 11, 2:2:8 means: start from 2, increment 2 by 2, until 8. Since the result is 2, 4, 6, 8, it is the same as taking every 2nd element! -. Slide 20, try and remember this! bookmark this slide! -. Slide 26, Scalar expansion: for an operation between an array M and a scalar s, the scalar is EXPANDED to an array of the same size as M. AUTOMATIC. Other programming languages are not as kind as this! -. Slide 29, make sure you understand the meaning of '.' in '.*', './', '.^', etc. -. Slide 34, 10 apples, 15 oranges. -. Slide 37, another important slide, try all these operations at least once in Matlab. -. Slide 41 eye(3) .* rand(3) -> random numbers [0.0 .. 1.0] along the main diagonal. ones(3,4) .* rand(4,3)' -> random number [0.0 .. 1.0] in 3x4 matrix. ones(3,4) .* [1 2 3; 4 5 6; 7 8 9] -> error, matrix dimensions do not agree. Lecture 3C ============== -. Slide 2, yes, there are many Matlab functions for array. We will just focus on the important ones. Some commands will help you do certain operations in a much simpler way... -. Slide 7, if originally: A = [1 2 3] [4 5 6] [7 8 9] B = [10 11 12] C = [13] [14] [15] then A(:,2) = B is [1 10 3] [4 11 6] [7 12 9] and A(2,:) = C is [ 1 2 3] [13 14 15] [ 7 8 9] -. Slide 8, typo: B = 1 3 <-- not 7! 4 6 7 9 -. Slide 10, sorting is actually a little bit more complex in other programming languages. Luckily, Matlab has built in function for this =) -. Slide 11-14, same thing, searching is also a bit more complex if using programming languages other than Matlab. In slide 12, the indices of items > 5 are 3, 6, 8, and 9 because the 3*3 array A is 'linearized' to [1 4 7 2 5 8 3 6 9] index 1 2 3 4 5 6 7 8 9 ^ ^ ^ ^ Try using this linearization operation A_linear = A(:)' -. Slide 17-21, 3D arrays are a bit hard to visualize, be careful! 4D and beyond are too complex. Note: A=zeros(2,3,2) means 2*3 matrix with 2 pages [r c p] = size(A) will return us r=2 c=3 p=2 To make it a little bit clearer, use this example A=zeros(4,3,2) means 4*3 matrix with 2 pages [r c p] = size(A) will return us r=4 c=3 p=2 Lecture 4 ============== -. Slide 2, woo hoo, some 'serious programming', welcome on board =) -. Slide 5, remember this!, Note: ~= is Matlab operator, != or <> are our standard Math for inequality test. -. Slide 8-10, understand this, you will frequently use this to test equality of two floating point numbers. -. Slide 15, TRUE, not TUE. -. Slide 16-18, understand this, somehow lecturers like to test this kind of question! -. Slide 19 is the complete BODMAS thing (lecture 3A). Do things in slide 19 first, before doing slide 20, therefore: 2+3 > 0 && 4*2-1 < 10 will be evaluated like this (brackets are used to indicate order of operation): (((2+3) > 0) && (((4*2)-1) < 10)) -. Slide 23-25, we have encountered this in lab 0/1 question 2.c =). Lecture 5a ============== -. Slide 5-7, you see some diagrams here. These are called 'flow charts'. Google that term for more information! -. Slide 8-12, to simplify your life, just use the for loop version in slide 12! -. Slide 13 Challenge 1, one sample answer: A = rand(1,200); % a random 1 row vector with 200 columns of random number sum = 0; for i=A % iterate through all the elements in A if i > 0.5 % if this item is greater than 0.5 sum = sum + i; end end disp(sum); Challenge 2, one sample answer: for x=2*pi:-0.1:-2*pi % decreasing with -0.1 decrement sin(x) end -. Slide 18, I think there is another typo here. Change the first line of the code into: A = rand(3,3) % Generate a random A array -. Slide 17-19. Master this nested thingy, it will get more complex later! -. Slide 20. Challenge 1, one sample answer: A = rand(10,10); % random number from [0..1] maxValue = -1; % initialize this to something smaller than 0! for i=1:10 for j=1:10 if A(i,j) > maxValue maxValue = A(i,j); end end end disp(maxValue); Challenge 2, one sample answer: A = rand(10,10); max(max(A)); % similar case as in 'creating flatland' example % or alternatively, you can write: max(A(:)) % convert A into single column vector then find the max :) Lecture 5b ============== -. Slide 9 vs 10. I prefer to use the style of slide 10! -. Slide 21. I do not want to do it... The if-elseif-else-end version of the grade point is 'inelegant'! -. Slide 22-27. If you use this to enhance your lab0/1 part C in order to avoid the potential division by zero error, you will get weird result... It seems that x = 1 / 0 (division by zero error) is not treated as an error in Matlab, but just 'warning'... the result is x = Inf (infinity). Lecture 6 ============== -. Slide 5. Remember the syntax for declaring a function! I repeat again here: function returnValue = functionName(argument1, argument2, ..., argumentN) % Help message line 1 % Help message line 2 % ... % Help message last line (terminate this function help with blank line) % do some computation with the input arguments here % usually the intermediate computation results are suppressed using ';' % normally we do not ask for user's input or print output INSIDE this function. % that job is left for the one CALLING this function. % return the results by assigning value to 'returnValue' returnValue = someExpression; % finally, save this file as 'functionName.m'! Do not change it into other file name! -. Slide 6. If you actually overwrite the original function 'sin', you may need to restart Matlab again. -. Slide 18-19. Remember, you can return more than ONE value! Other programming languages typically do not allow this! -. Slide 23-40. A rather long demo on breaking down large programs into separate logical functions. You should always try to do this for your large programming projects! Read this for extra information: http://en.wikipedia.org/wiki/Great-circle_distance ======================== Lectures by Dr Saif Khan ======================== Roots ============== -. To understand more about term 'polynomial', 'transcendental equation', 'non linear', or 'simultaneous equations', google them using this syntax: 'define ' -. There are three ways to find roots using Matlab 1. roots(theCoefficientsOfPolynomial) only works for simple polynomial 2. fzero(@theFunction, initialGuess) more general, can be applied to transcendental equations, but your initialGuess must be clever enough, otherwise it will output nothing. 3. fsolve(@thefunction, initialGuess) similar with fzero, but better. however, this is only available in Matlab programs with "Optimization Toolbox"... the Matlab installed in the computers inside COM1/114 does not have this toolbox! go to SoC1/815 or find Matlab in FoE computer labs! fsolve can also be used to solve simultaneous equations. This part is re-highlighted in the early part of the next lecture! -. Slide 8-9. I think there are typos. It should be 'Method 2: fzero'! Linear Algebra ============== -. Slide 2-3 are actually part of the few slides of the last lecture. An example on how to use 'fsolve' to tackle simultaneous equations. -. Slide 11-20 are background to Matrix algebra, what we are going to use is Slide 21-26. -. Sets of Linear Equations can be written as Ax = b Following slides 23-26, we can find the values of x with two ways: x = inv(A) * B or x = A \ B --> preferred style in Matlab, just use this one. -. Slide 27-35. Sorry, I am not good in Chem Eng. Understand these slides by yourself. Symbolic Math ============== -. My initial impression after trying all the examples on Matlab: WOW... Okay, I thought Matlab can only compute numbers... but now it seems that Matlab can compute things symbolically too! Let's review this feature! -. If you look at Matlab workspace, anything that we declare as symbols e.g. syms a b c will be stored in Matlab workspace as 'sym' (symbol) object, not ordinary numbers -. The best way to understand this lecture, especially if you MISS the lecture, is by downloading the lecture note and TRY EXAMPLES on EACH SLIDE by yourself! You will be amazed how Matlab emulates what you have known from your high school/ junior college A level Math... -. Slide 5. Typo. f = sin(5x) should be f = sin(5*x) -. In case you forgot your A level Math, open your old Math text book again... Graphic Tools ============== -. Nice reference note. Please try all these features on your own. You should then be able to draw nicer plots for your lab reports/term assignment :) -. Additional technique not mentioned in the lecture note: legend --> to tell the plot reader that first plot is what, the second is what, etc plotyy --> two draw two y axis in both side (left and right) semilogy --> semi log y, if you y data is exponential, this may come handy. Ordinary Differential Equations ============== -. For the first three lectures about ODE, we are basically dealing with a variant called Initial Value Problem (IVP). Given an ordinary differential equation and the initial value, simulate it... -. Standard syntax: function d_variableToBeDerived_d_t = anyFunctionNameWillDo(time, variableToBeDerived) % Write Matlab codes for the differential equation here d_variableToBeDerived_d_t = d_variableToBeDerived_d_t'; % must be a column vector Then call one of the ODE solver (there are so many variants, I still do not know which one to use...?) timeSpan = [startTime endTime]; initialValue = someNumbers; [time, variableToBeDerived] = ode45(@anyFunctionNameWillDo, timeSpan, initialValue); -. Not only for one ODE, you can do this for coupled ODEs! You just need to adjust something, as mentioned in the lecture note -. We can also use it for HIGHER order ODE... -. Anyway, we can also create our own ODE IVP solver, as shown in eulerODE.m (V01-V03) -. For stiff systems, we should use ode15s. -. Later on, you will learn what is the methods behind these ODE solvers. Curve Fitting ============== -. Two important commands: polyfit and polyval Optimization ============== -. Two important commands: fminsearch and fmincon -. fminsearch is fmincon but without constraint...