From fc8c9456d131643fb9d0c6006d0680b37dab03f6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nils=20Forss=C3=A9n?= Date: Mon, 26 Sep 2022 15:20:26 +0200 Subject: [PATCH] pushing --- builder.m | 9 +++++++++ lufact.asv | 48 ------------------------------------------------ newtonRaphson.m | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 48 deletions(-) create mode 100644 builder.m delete mode 100644 lufact.asv create mode 100644 newtonRaphson.m diff --git a/builder.m b/builder.m new file mode 100644 index 0000000..e8dc626 --- /dev/null +++ b/builder.m @@ -0,0 +1,9 @@ + +A = zeros(length(t), 5); + +for i=1:length(t) + A(i, :) = [1 cos(2*pi*t(i,1)/0.3840) sin(2*pi*t(i,1)/0.3840) cos(4*pi*t(i,1)/0.3840) sin(2*pi*t(i,1)/0.3840)]; + + + +end \ No newline at end of file diff --git a/lufact.asv b/lufact.asv deleted file mode 100644 index 17f9c12..0000000 --- a/lufact.asv +++ /dev/null @@ -1,48 +0,0 @@ -function [L,U,P] = lufact(A) -%% -% Computes the LU factorization of the matrix A. The factorization is done -% in-place and then the L and U matrices are extracted at the end for -% output. The factorization is PA = LU -% -% INPUT: -% A - n by n square, non-singular matrix -% -% OUTPUT: -% L - lower triangular matrix with ones along the main diagonal -% U - upper triangular matrix -% P - permutation matrix for pivoting -%% -% get the size of the system - n = length(A); -%% -% initialize the pivoting matrix - P = eye(n); -%% -% Vectorized in-place LU factorization (with row pivoting) that keeps -% track of the total permutations by scrambleing the matrix P - for j = 1:n-1 - - [v i] = max(abs(A(:, j))); - A([k i],:) = A([i k],:); - P([k i],:) = P([i k],:); -%% -% Find the index of the largest pivot element in the current column - - -%% -% Swap the rows within the in-place array as well as the permutation matrix P - - - -%% -% Perform the in-place elimination and save the new column of L - i = j+1:n; % indices for the "active" matrix portion - A(i,j) = A(i,j)/A(j,j); - A(i,i) = A(i,i) - A(i,j)*A(j,i); - %A, return - end -%% -% Extract L and U from the in-place form - U = triu(A); - L = eye(n) + tril(A,-1); -end diff --git a/newtonRaphson.m b/newtonRaphson.m new file mode 100644 index 0000000..fd5da88 --- /dev/null +++ b/newtonRaphson.m @@ -0,0 +1,37 @@ +function [xVals,iter] = newtonRaphson(f,x0,tol) +%% +% Implementation of the Newton-Raphson method to approximate +% the root of a nonlinear function f(x) +% +% INPUT: +% f - function f(x) OBS! passed as an anonyomous function +% fprime - first derivative of the function f(x) OBS! passed as an anonyomous function +% x0 - initial guess of the root location +% tol - error tolerance to stop the iteration +% +% OUTPUT: +% xVals - sequence of approximate values for the root of the function f(x) +% iter - number of iterations it took to achieve user set tolerance +%% +% initialize a vector to store the sequence of guesses + xVals = zeros(16,1); +%% +% save the initial guess + xVals(1) = x0; +%% +% perform Newton-Raphson for a fixed number of iterations + for k = 1:15 +%% +% update to the next value + fk = f(xVals(k)); + xVals(k+1) = xVals(k) - ((fk)^2) / (f(xVals(k) + fk) - fk); +%% +% check the stopping condition + stopCond = abs(f(xVals(k+1))); + if stopCond < tol + xVals = xVals(1:k+1); + iter = k+1; + break; + end + end +end \ No newline at end of file -- 2.30.2