# Functions This directory contains the functions written throught the course. * days.m * falsePositive.m * heun.m * luFactor.m * simpson.m * spacialMatrix.m --- ## [days.m](days.m) Function to count the total days elapsed in a year according to a given date. The syntax of the function is `days(, , )` where `` is a integer (1-12). `` is an integer from 1-31 and `` accounts for leap years. ### Input * `months` - month number (1-12). Example: `8` represents August. * `days` - day number of the month. * `leap` - indicates if the year is a leapyear or a regular year. `0` for regular and `1` for leap year. ### Output * `nd` - number of days elapsed in the year. ### Example: `days(8,4,0)` represents August 8th in a regular year (non-leap year). --- ## [falsePositive.m](falsePositive.m) Function finds the root of an anonymous function using the false position method. Synopsis: `[root, fx, ea, iter] = falsePosition(func, xl, xu, es, maxit, varargin)`. ### Input * `func` - the function being evaluated. * `xl` - lower bound guess. * `xu` - upper bound guess. * `es` - desired relative error (default 0.0001%) * `maxit` - maximum number of iterations (default 200) * `varargin` - any additional parameters used by the function ### Output * `root` - estimated root location. * `fx` - function evaluated at root location. * `ea` - approximated relative error (%). * `iter` - number of iterations performed. ### Notes: Known issue: the output of `iter` needs fixing. The output is incorrect. --- ## [heun.m](heun.m) Uses the heun method to integrate an ODE. Synopsis: `[t,y] = Heun(dydt,tspan,y0,h)`. ### Input * `dydt` -the differential equation of interest (must be anonymous function). * `tspan` - the initial and final values of the independent variable as a vector with length=2 [ti,tf]. * `y0` - the initial value of the dependent variable. * `h` - step size. * `es` - stopping criterion (%), optional (default = 0.001). * `maxit` - maximum iterations of corrector, optional (default = 50). ### Output * `t` - vector of independent variable values * `y` - vector of solution for dependent variable ### Notes: This function needs some working on to compute a correct solution when using multiple steps with an irregular step size at the end. --- ## [luFactor.m](luFactor.m) Performs LU decomposition with pivoting. Synopsis: `[L, U, P] = luFactor(A)`. ### Input * `A` - a coefficient matrix. ### Output * `L` - lower triangular matrix, with 1's along the diagonals. * `U` - upper triangular matrix. * `P` - the permutation matrix. ### Notes: Be cautious when using this function on bigger matrices. The `L` variable is known to be incorrect. --- ## [simpson.m](simpson.m) Evaluates the integral of two vectors by Simpsons 1/3 rule. Synopsis: `[I] = Simpson(x, y)` ### Input * `x` - the vector of equally spaced independent variable. * `y` - the vector of function values with respect to x. ### Output * `I` - numerical calculated integral. ### Notes: The current state of this function is **deprecated**. The algorithm fails compute the correct trapeziodal rule given 2 data points as well as 3 data points. Thus, failing to solve real problem. Pull requests are welcomed. --- ## [specialMatrix.m](specialMatrix.m) # specialMatrix.m Function returns a special matrix A with the following criteria: - The value of each element in the first row is the number of the column. - The value of each element in the first column is the number of the row. - The rest of the elements each has a value equal to the sum of the element above it and element to the left. - The function returns a sensible error if the user does not input exactly two arguments. Synopsis: `[root, fx, ea, iter] = falsePosition(func, xl, xu, es, maxit, varargin)`. ## Input * `func` - the function being evaluated. * `xl` - lower bound guess. * `xu` - upper bound guess. * `es` - desired relative error (default 0.0001%) * `maxit` - maximum number of iterations (default 200) * `varargin` - any additional parameters used by the function ## Output * `A` - special matrix with the appropriate rules ## Notes: This function has not much of a practical application, rather a very good exercise for beginners to get started with the basics of matrix manipulation and user-defined functions.