diff options
| -rw-r--r-- | Functions/README.md | 107 | ||||
| -rw-r--r-- | Functions/days.m | 22 | ||||
| -rw-r--r-- | Functions/falsePosition.m | 45 | ||||
| -rw-r--r-- | Functions/heun.m | 44 | ||||
| -rw-r--r-- | Functions/luFactor.m | 87 | ||||
| -rw-r--r-- | Functions/simpson.m | 39 | ||||
| -rw-r--r-- | Functions/specialMatrix.m | 51 |
7 files changed, 395 insertions, 0 deletions
diff --git a/Functions/README.md b/Functions/README.md new file mode 100644 index 0000000..a198244 --- /dev/null +++ b/Functions/README.md @@ -0,0 +1,107 @@ +# 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(<months>, <days>, <leap>)` where `<months>` is a integer (1-12). `<days>` is an integer from 1-31 and `<leap>` 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) diff --git a/Functions/days.m b/Functions/days.m new file mode 100644 index 0000000..87163b7 --- /dev/null +++ b/Functions/days.m @@ -0,0 +1,22 @@ +function nd = days(mo, da, leap) +%days - Counts days elapsed in the year. +% +% days(<months>, <days>, <leap>) determines the days elapsed in a year +% based on the current date. This will include the current day. +% Months (1-12), +% Example: August 28, no leap year +% days(8,28,0) + +daysPeM=[0 31 59 90 120 151 181 212 243 273 304 334]; % cummulative days prior each month. + +nd = daysPeM(mo) + da; + +if leap == 1 & mo >= 3 + nd = nd + 1; +end +end + + +% MECH 105: Homework 4 - Part 1 +% Author: Christian Kolset +% Date: 5.21.21
\ No newline at end of file diff --git a/Functions/falsePosition.m b/Functions/falsePosition.m new file mode 100644 index 0000000..d2cb776 --- /dev/null +++ b/Functions/falsePosition.m @@ -0,0 +1,45 @@ +function [root, fx, ea, iter] = falsePosition(func, xl, xu, es, maxit, varargin) +%falsePosition finds the root of a function using false position method +% Syntax: +% [root, fx, ea, iter] = falsePosition(func, xl, xu, es, maxit, varargin) +% +% Input: +% func = function being evaluated +% xl = lower guess +% xu = upper 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 + +if nargin < 3, error('Too few arguments: at least 3 needed'), end +if nargin < 4 | isempty(es), es = 0.0001; end +if nargin < 5 | isempty(maxit), maxit = 200; end + +iter=0; +ea=100; +xr= xl; + +while(1) + xrold= xr; + iter = iter + 1; + xr=xu-func(xu)*(xl-xu)/(func(xl)-func(xu)); + if xr~=0 + ea = abs((xr-xrold)/xr)*100; + end + if func(xl)*func(xr)<0 + xl=xr; + else + xl=xr; + end + if iter == maxit | ea <= es, break, end +end +root = xr; +fx=func(xr,varargin{:}); +ea=ea; +iter=iter; +end
\ No newline at end of file diff --git a/Functions/heun.m b/Functions/heun.m new file mode 100644 index 0000000..5812dfe --- /dev/null +++ b/Functions/heun.m @@ -0,0 +1,44 @@ +function [t,y] = Heun(dydt,tspan,y0,h,es,maxit) +% [t,y] = Heun(dydt,tspan,y0,h): uses the heun method to integrate an ODE +% inputs: +% dydt = the differential equation of interest (must be anonymous +% function) +% tspan = [ti,tf] the initial and final values of the independent +% variable +% 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) +% outputs: +% t = vector of independent variable values +% y = vector of solution for dependent variable + +%Error checking +if nargin<4, error('At least 4 aruments required.');end +if nargin<5|isempty(es), es = 0.001;end +if nargin<6|isempty(maxit), maxit = 50;end +if length(tspan)~=2, error('Argument tspan must have a length of 2');end + +%Trims tspan according to step size (h) +t=tspan(1):h:tspan(2); +%if rem(tspan(2),h)~=0 +if t(length(t))~=tspan(2) + t=[t,tspan(end)]; +end + +y=ones(1,length(t))*y0; +h=diff(t); + +for i=1:length(t)-1 + S=dydt(t(i),y(i)); + ynew=y(i)+S*h; + for j=1:maxit + yold=ynew; + ynew=y(i)+h(i)/2*(S+dydt(t(i+1),yold)); + ea = abs((ynew-yold)/ynew)*100; + if ea <= es, break, end + end + y(i+1)=ynew; +end +plot(t,y) +end
\ No newline at end of file diff --git a/Functions/luFactor.m b/Functions/luFactor.m new file mode 100644 index 0000000..4c853dd --- /dev/null +++ b/Functions/luFactor.m @@ -0,0 +1,87 @@ +function [L, U, P] = luFactor(A) +% luFactor(A) +% LU decomposition with pivoting +% inputs: +% A = coefficient matrix +% outputs: +% L = lower triangular matrix +% U = upper triangular matrix +% P = the permutation matrix + + +[m,n] = size(A); +if m~=n, error('Enter a square matrix.'); end +nb = n + 1; +L=zeros(m,m); +U=zeros(m,m); +P=eye(m); + +%Partial pivoting +for j = 1:n - 1 + for p = j+1:m + if (abs(A(j,j)) < abs(A(p,j))) + A([j p],:) = A([p j],:); + P([j p],:) = P([p j],:); + end + end +end +% [big,i] = max(abs(A(j:n,j))); +% ipr = i + j - 1; +% if ipr ~= j +% A([j,ipr],:) = A([ipr,j],:); +% end +%end + +for i=1:m + % Computing L + for k=1:i-1 + L(i,k)=A(i,k); + for j=1:k-1 + L(i,k)= L(i,k)-L(i,j)*U(j,k); + end + L(i,k) = L(i,k)/U(k,k); + end + + % Computing U + for k=i:m + U(i,k) = A(i,k); + for j=1:i-1 + U(i,k)= U(i,k)-L(i,j)*U(j,k); + end + end +end + +for i=1:m + L(i,i)=1; +end + +%% Checks +if P*A ~= L*U, error('LU factorization failed to be computed correctly.'); end + +% Checks output size +if size(L) ~= size(A) | size(U) ~= size(A) | size(P) ~= size(A) + error('Incorrect output size') +end + +% Checks if matrices are triangular +%for i=1:m +% for j=1:n +% error('Incorrect output: LU decomposition are not triangular') +%end + +% Checks the permutation matrix P is valid +%P_str = string(P) +%for k = m +% num1=count(P(:,1:n),1) +% if num1 ~=1 +% error('Incorrect P matrix output. Too many 1's on the same line') +% end +% num0=count(P(:,1:n),0) +% if num0 ~= n-1 +% error('Incorrect P matric output. Wrong number of 0's') +% end +%end + +%} + +end
\ No newline at end of file diff --git a/Functions/simpson.m b/Functions/simpson.m new file mode 100644 index 0000000..acd4a76 --- /dev/null +++ b/Functions/simpson.m @@ -0,0 +1,39 @@ +function [I] = Simpson(x, y) +% Numerical evaluation of integral by Simpson's 1/3 Rule +% Inputs +% x = the vector of equally spaced independent variable +% y = the vector of function values with respect to x +% Outputs: +% I = the numerical integral calculated + +%Error cheching +if length(x)~=length(y), error('The vectors need to be of equal lengths'),end +if length(x)==1, error('Vectors must be at least 2 data points long'),end +if range(x(2:end)-x(1:end-1))~=0, error('The x vector needs to be equally spaced'), end +%Error Errorchecking END + +lower_bound=min(x); +interval=length(x)-1; +if rem(interval,2)==0 + trap_rule=0; + upper_bound=max(x); +else + warning('Odd number of intervals. Applying Trapezoidal rule on last interval') + trap_rule=1; + upper_bound=x(end-1); +end + +if length(x)==2 + I=(upper_bound-lower_bound)*(y(1)+y(2))/2; +else + h=(upper_bound-lower_bound)/interval; + SumOdd=sum(y(1:2:end))-y(end-1); + SumEven=sum(y(2:2:end))-y(end-2); + I=(upper_bound-lower_bound)*(y(1)+4*SumOdd+2*SumEven+y(end))/(3*interval); +end + +if trap_rule==1 & length(x)~=2 + I_trap=(max(x)-upper_bound)*(y(upper_bound)+y(max(x)))/2; + I=I+I_trap; +end +end
\ No newline at end of file diff --git a/Functions/specialMatrix.m b/Functions/specialMatrix.m new file mode 100644 index 0000000..d6c31f8 --- /dev/null +++ b/Functions/specialMatrix.m @@ -0,0 +1,51 @@ +function [A] = specialMatrix(n,m) +% This function should return a matrix A as described in the problem statement +% Inputs n is the number of rows, and m the number of columns +% It is recomended to first create the matrxix A of the correct size, filling it with zeros to start with is not a bad choice + +%-------------------------------------------- + +if nargin ~= 2 + error('Error: Please enter two arguments.') +end +if (n|m) <= 0 + error('Error: Index error. Arguments must be positive integers or logical values.') +end + +A = zeros(n,m); %Creates a "blank" n x m matrix full of zeros + +% Now the real challenge is to fill in the correct values of A + +A(1,:) = 1:m; %Lables the first row with column numbers +A(:,1) = 1:n; %Lables the first column with row numbers + +for j = 2:n + for k = 2:m + A(j,k)=A(j-1,k)+A(j,k-1); % fills each element of the matrix with the sum of the left and above element. + end +end +end + +%--------------------------------------------- + +%{ +if nargin(specialMatrix) ~= 2 + error('Error: Please enter two arguments.') +elseif nargin(specialMatrix) <= 0 + error('Error: Index errer. Argument must be positive integers or logical values.') +end + +if n<2 + A = [1:m]; %If matrix is 1 x m then make a matrix with 1 row. +else + A = [1:m;1:m]; %Create row 1 +end + +for j = 3:n %Loop to make + A(j,:) = sum(A); % +end % +A(:,1)=(1:n); + +end +% Things beyond here are outside of your functions +%}
\ No newline at end of file |
