Summary

This document provides notes on MATLAB basics, including matrix operations, image commands, and more. It's suitable for undergraduate-level math students.

Full Transcript

[Module Intro] Flipped learning - watch content before lecture Lectures = tutorial style sessions Schedule: A colorful text box with black text Description automatically generated with medium confidence ![A screenshot of a calendar Description automatically generated](media/image2.png) 50% Cou...

[Module Intro] Flipped learning - watch content before lecture Lectures = tutorial style sessions Schedule: A colorful text box with black text Description automatically generated with medium confidence ![A screenshot of a calendar Description automatically generated](media/image2.png) 50% Coursework handed out in week 7 -- matlab programming, due week 11 50% Online test [MATLAB Basics] Matlab is a system used to visualize math problems, especially working with matrices. Matlab has its own language which is a high level (fairly simple) language used for performing operations on matrices. Matlab can also display images and 3d graphics from the matrices given. There are toolboxes (like libraries) to do other, more complex operations that aren't standardised. There are no data types in matlab, i.e. can create an integer and later assign it to be an array/matrix. If a calculation is performed without setting a variable to save it to then it is automatically assigned to the temporary value *ans*. Matlab will also print out the new value every time a variable is changed, to stop this add a semicolon at the end of the calculation line. **Matrices** Matrices are the main data type used in matlab. There are also vectors which are 1x*y* or *z*x1 matrices (row or column vectors). To declare a matrix each row is ended with a semicolon or a new line and each value in each column is separated by a space or comma (or both) e.g. A white paper with black text Description automatically generated To create larger matrices you can also save them in any other file (typically an ascii or text editor) and load them into the workspace by calling load *data*.ext (where *ext* is any extension other than.mat). You can also generate matrices using some functions e.g. rand(n), magic(n), eye(n), ones(n) and zeros(n). (In all these you can also mass (n,m) into the function to create a none square matrix with n rows and m columns.) As well as simple matrices, matlab can also process audio, image and video files and there are basic commands for those too. (will be used in scientific computing module). A simple black and white image is stored as a 2d array (aka a normal matrix) where each value is the intensity of the greyscale colour. A colour image is stored as a 3d array (for RGB) which can also be handled in matlab. Some basic image commands: **Imread(**filename.ext**)**: reads an image file and assigns a variable the matrix that represents it **Size(**img**)**: gets the dimensions of an image **Whos(**var**)**: lists a given variable in long form **Imshow(**var**)**: creates a window to display the image For videos matlab stores them as a 2d array of each image in each frame. You can also use commands like size() and whos() on video variables. You use VideoReader and VideoWriter to create/read videos to/from the arrays. You can access specific values in an array using indexing. Matlab indexes from 1 and you can either use (n x m) or just (x), the first finds the value in the *n*th row and *m*th column. If there is only one value it gets the *x*th element - counts across the row e.g. ![A number on a white background Description automatically generated](media/image4.png) A black text on a white background Description automatically generated The math laws still apply to the matrices -- e.g. sizes will clash in some cases of addition, multiplication etc. In multiplication the inner sizes must match (e.g. if matrix A = *a* x *y* and matrix B = *y* x *b* then you could do AB but not BA) (In addition the matrices must be the same size). Matrix multiplication: A\*B ![A diagram of a diagram Description automatically generated](media/image6.png) Matrix division: *The relevant matrix must be inversible and the dimensions must match up* A\\C = inv(A) \* C A/C = A \* inv(C) The difference is pre vs. post multiplying: A close up of black text Description automatically generated There are 2 types of operations, array operations and matrix operations. Matrix operations are the ones defined above, array operations are performed element by element are defined using a '.' before the command e.g..\* is an array multiplication. For array operations the matrices must be the same size. In matlab you can separate matrices using colons to get submatrices: These are much faster and simpler than in other languages as they use vector operations instead of for loops You can create simple row vectors using \[a:b\] e.g. \[1:5\] = \[1,2,3,4,5\]. If you add a number in the middle \[a:x:b\] this will be the step e.g. \[1:2:10\] = \[1,3,5,7,9\]. In both of these the values don't need to be integers -- they can be floats instead. You can also use a negative step to go backwards. To get submatrices you can do something similar. E.g. if I have a matrix A then A(1:4, 2) will create a matrix of all the values in the second column in rows 1 to 4 (inclusive). You can also use a : on its own to get all the values e.g. A(1:4,:) is the first 4 rows of matrix A. you can also use the keyword *end* to get the last row/column and can do '*end*-1' to get the penultimate row etc. Finally, to get specific rows you can use \[\] e.g. A(\[1,3\],:) will create a submatrix of only rows 1 and 3 (not 2). Examples of using submatrices: ![A white background with black text Description automatically generated](media/image8.png) ![A black text on a white background Description automatically generated](media/image10.png) [Matlab Language] Matlab is an expression language aka the things you type are expressions that are interpreted or evaluated. It is also case sensitive!! You can end lines with... to continue the command onto the next line. You can also use the semicolon to put multiple commands on one line. The *who* command can be used to display a list of all the variables in the workspace along with their size and types. Use *who**s*** to display the variables in long form. The *clear* command can be followed by a variable name to delete it from the workspace, used on its own to clear all non-permanent variables, with *global* to remove all global variables, with *functions* to remove all compiled M and MEX functions and *all* to remove all variables, globals, function and MEX links. When matlab is closed all varables that aren't saved to a workspace are deleted. These can be retrieved using *load*. *Help* give basic help instructions, follow with a command to get more specific information. *Which* locates functions and files *Demo* runs demonstrations *Path* controls the matlab search path *Pack* is used to consolidate the workspace memory (if space runs out) *Disp* displays a matrix or text *Clc* clears the command window *Format* sets the output format *Quit* terminates matlab *Startup.m* is a special M-file that is executed when matlab is started *Matlabrc.m* is the matlab master startup file Ctrl + C usually stops any programs running A lot of linux commands also work in matlab e.g. *cd, pwd, dir, ls, delete, getenv, unix, diary* etc. We can also use programming constructs such as for, while and if statements like other programming languages. **For**: A white background with black text Description automatically generated For loops should be avoided if possible as it is a lot slower than using vectorised versions e.g. ![A close up of a sign Description automatically generated](media/image12.png) **While**: A close-up of words Description automatically generated e.g. ![A math equation with black text Description automatically generated](media/image14.png) **If**: A math equation with black text Description automatically generated with medium confidence Matlab logical operators: ![A white rectangular sign with black text Description automatically generated](media/image16.png) Instead of *true* or *false* the result of a comparison is 1 (for true) or 0 (for false). This means that comparisons can be applied to whole matrices and the result will be a matrix of 0s and 1s. The result is stored in a data type called a *logical* A number and numbers on a white background Description automatically generated There are some rules/traps when comparing matrices to matrices instead of to scalars. ![A white paper with black text Description automatically generated](media/image18.png) A white paper with black text Description automatically generated [Matlab Functions] There are many built in matlab functions, you can also create custom functions if needed. Scalar functions (operate element by element): ![A black text on a white background Description automatically generated](media/image20.png) Vector operations (operate row by row or column by column): A group of black text Description automatically generated Vector operations produce a row vector from a matrix by applying it column by column. There are also several ways to perform it row by row: transpose the matrix so that the rows are now columns (and transpose the result to turn it into a column vector if needed) or use the optional second parameter for the function -- func(A,1) means apply it column by column and func(A, 2) means row by row. You can also double up vector operations to apply them to the whole matrix. E.g. max(A) will make a row vector with the max value from each column so max(max(A)) will take that row vector and find the highest value from that which will be the highest value in the whole matrix. You can do this with most vector functions including mean, sum, min etc. Matrix functions: ![A close up of words Description automatically generated](media/image22.png) You can declare your own functions using A black text on a white background Description automatically generated Functions should be stored in an m-file somewhere in the file space (e.g. 'myfunction.m'). The comments on a function is the text that will be displayed when 'help *function'* is called. There are several functions in matlab that can help when creating functions: - Type = list function from command line - Edit = edit or create an m-file - Nargin = shows number of function arguments to be inputted (can also use nargout for return variables) - Disp = text strings can be displayed to user (e.g. warnings) - Error = displays text and aborts the m-file (function) - Global = variables are local by default - Dbtype = displays the program file to the command line Another type of m-file is a script, which can be used to execute common commands (e.g. workspace set up). In a script all variables are global (unlike in functions) There are 2 ways to create M-files, type them directly into a text editor or use the diary command. **MatLab Debugger** There are several common debugging tools in matlab that can be used including breakpoints, step-by-step execution and variable inspection/modification (when program is paused). [Matlab Graphics] There are many helpful functions for dealing with graphics in matlab. Matlab can produce 2d & 3d plots along with 3d mesh surface plots and 3d faceted surface plots. In this course we will mainly focus on 2d plots but can do many other things later on. We can use the plot function to create simple x-y plots. You can just plot y (plot(y)) but it is generally more useful to plot y against another vector (plot(x,y)) as it gives more information. If you just plot y then the x elements are just the index of the point (e.g. first element will be at 1, second element will be at 2 etc.). You can also customize the graph created using commands like title, xlabel, ylabel, gtext and text. These should be run only **after** the graph has been created (plot(x,y) has been run) or they won't work. There are also lots of commands for how the graph itself is displayed: ![](media/image24.png) As well as additional arguments that can be used in plot to define the line style (dotted, dashed solid, colours etc.) By default the image opened will overwrite anything in the current graphics window. We can create an additional window by using the figure() command, or specify the window to use using figure(n). You can use 'gcf' to get the current number of windows. You can plot multiple plots on a single graph. You can do this by adding multiple sets of vectors into the arguments (e.g. plot(x1, y1, x2, y2, x3, y3...)) or you can create a matrix Y where each column is one of the vectors (e.g. \[y1'; y2'; y3'\] and matlab will automatically create multiple plots. Finally you can also use the *hold* command. Hold freezes the current plot and any plots after it will be added to the plot not overwrite it (end this with 'hold off'). *Fplot* is matlab's version of an 'easy' plotting command. There is a default range and the argument is the function that should be used -e.g. fplot('tan(x)+3\*x\^2). You can add other parameters such as the range that should be used and any other parameters used in plot (like line style and colour). You can use subplot to plot multiple (separate) graphs onto a single graphics window. Use subplot(1,1,1) to reset it back to normal. There are some other specialised 2d plotting functions that can be used including: [Matlab GUIs] You can build a GUI in matlab in 2 ways, you can do it by hand (can only really do small simple guis) or you can use matlab's AppDesigner. There are several pre defined dialog boxes in matlab: ![](media/image26.png) This is an example of how to make an error pop up. The first string is the message in the box, the second the title and the third means that matlab can only have one error window open at once (if not included the box will pop up hundreds of time). These are the inputs you can give for a standard message dialog box: ![](media/image28.png) Dialog boxes can be both modal and non modal, modal means matlab will wait for a response before doing anything else. The question dialog is an example of a modal dialog. You can also create dialog boxes with multiple inputs (questions) like this: A close up of text Description automatically generated We use a cell array ('{....}') for the answers because its size is not fixed -- we cant use an array as the size can't be changes easily. We can use uisetfont and uisetcolor so that the user can modify the colour and font of the thing they are doing (usually the graph they have made). **AppDesigner** All the above are built in to the base of matlab but we can use matlab's built in appDesigner to make more complex GUIs. [Factorisation] Factorisation = a way of simplifying algebraic expressions. Ways of factorisation: - Use common factors (find and extract the HCF) - Use common factors by grouping (divide by more complex expressions like (a+3b) e.g. ![A math equation with black text Description automatically generated with medium confidence](media/image30.png) The ac method (see below) - Use knowledge of algebra A math equation with black text Description automatically generated **The ac method** For factorising ax^2^ + bx + c Find \|ac\| and write down all possible factors of the product. The find 2 factors which add up c (if c is positive) or which have a difference of c (if c is negative) and set these values as *m* and *n*. Write the expression as ax^2^ + *m*x + *n*x + c. Then group (ax^2^ + *m*x) + (*n*x + c) and factorise each group, will be left with common expression in bracket and can create second group with factors. E.g. ![A math equations on a white background Description automatically generated](media/image32.png) **Cubic equations** To factorise cubic equations we can use the factor theorem. The factor theorem is that assuming if f(a) = 0 then x-a is a factor of f. We can use this to solve cubics as if we find a factor (by testing random values of a e.g.0, 1, -1, 2, -2) we can divide f by (x-a) to get a quadratic equation we can solve. Example of algebraic long division: A math problem with numbers and symbols Description automatically generated We can also use the remainder theorem. The reminder theorem is that when f(x) is divided by (x-a) the **remainder** is f(a). e.g. if (x^3^ + 6x^2^ + 7x -4) / (x+3) then the remainder is f(-3) -- where f(x) = x^3^+6x^2^+7x-4. Another way to simplify cubic expression can be using the sum or difference of 2 cubes. This means we can rewrite ![](media/image34.png) And We can use all these factors on quartic (x^4^) equations too it is just longer and more complex. There is also an equivalent to the quadratic equation for the quartic equation, but it is too long winded to realistically use for solving them. (its like a billion complex equations). For long and complicated equations we can use matlab to factorise equations for us. ![A white paper with black text Description automatically generated](media/image36.png) [Polynomials] Formal definition of a polynomial: A math equations and formulas Description automatically generated with medium confidence A polynomial to the order n has n roots. Roots are the points that the polynomial crosses the x axis on the graph (or they can be imaginary if it doesn't cross the axis). A straight line follows y=mx + c. m is the gradient and c is the y intercept. We can find the gradient in 2 ways, using (y~2~ -- y~1~)/(x~2~ -- x~1~) or tan(o) where o is the angle that the line makes with the x axis. With a quadratic line we can use the discriminant (b^2^ -- 4ac) to determine the nature of the roots: - If = 0 then there are 2 equal roots (it forms a tangent with the x axis) - If \>0 (positive0 there are 2 distinct, real roots - If \ k), then we can define the expected value as E\[X\] = λ/n: The probability of k events happening is: ![A math equations on a white background Description automatically generated](media/image156.png) The Poisson distribution can be seen as an extreme case of the binomial distribution where the number of Bernoulli trials is infinite. **Joint distributions** A white paper with blue and red text Description automatically generated ![A white paper with black and blue text Description automatically generated](media/image158.png) If we only have the joint distributions, we can derive the individual distributions like A black symbol with text Description automatically generated The probability distributions px1...pxn are also called the marginal distributions. ![A math equations on a white background Description automatically generated](media/image160.png) A white background with black text and blue and red text Description automatically generated The **covariance** of 2 random variables is ![](media/image162.png) The **correlation coefficient** tells the link between 2 random variables. It can be defined as A math equation with a square and square symbol Description automatically generated with medium confidence It is always between -1 and 1. If the correlation coefficient is 0 then X and Y are *uncorrelated*. If X and Y are independent then the correlation coefficient is always 0. (However uncorrelated does not always mean independent) ![A black text on a white background Description automatically generated](media/image164.png) **Multinomial distribution** If we have a sequence of *n* experiments, where each one can yield one of the outcomes s1...sk and X~i~ is the number of times outcome s was obtained then the probability distribution of X1... Xk is the multinomial distribution. It can be defined as: A math equations on a white background Description automatically generated [Estimators] In real situations we often know which class the distribution belongs to but we don't know the actual parameters. We can estimate these parameters using observations. If we have a random variable X with an unknown distribution. A sample is a vector *a* = (a1...an) where the values of *a* are interpreted as difference outcomes of the corresponding experiment. We can assume that px is know up to the value of some parameter θ, to clarify that px depends on θ we can write px(a) = p(a; θ). The unknown parameter θ is called the estimand or true value. An **estimator** is a real value function **θ** which maps samples to estimations of θ: ![A close up of a math problem Description automatically generated](media/image166.png) **θ** itself is a random variable. Example: A math problem with numbers and equations Description automatically generated with medium confidence We don't know the true distribution of X but as it is 0.625 rather than 0.5 it may not be a normal distribution, it may be biased. More data is required to know with more certainty. There are several properties we can add to an estimator to make sure that it gives reasonable results. The **bias** or expected value of error of an estimator can be defined as ![](media/image168.png) Ideally an estimator will have no bias (=0) The variance is calculated the same: And a good estimator will have minimal variance. ![A math equations on a white background Description automatically generated](media/image170.png) Maximum likelihood estimation: A math equations on a white background Description automatically generated [Bayesian Inference] Bayesian inference is a way of using Baye's theorem to update a hypothesis about an estimator. We can use Baye's rule to get ![](media/image172.png) A white paper with red circles and black text Description automatically generated Example: ![A screenshot of a computer Description automatically generated](media/image174.png) A math problem with numbers and equations Description automatically generated with medium confidence

Use Quizgecko on...
Browser
Browser