├── GE.m ├── LUdecomp.m ├── README.md ├── bisectionMethod.m ├── compTrap_Simp13.m ├── fixedPoint.m ├── gaussElimination.m ├── gaussJacobi.m ├── gaussSeidal.m ├── lagrangeInterpolation.m ├── leastSquaresMethod.m ├── newtonDividedDifference.m ├── newtonMethod.m ├── powerMethod.m ├── secantMethod.m └── trapSimpson13and38.m /GE.m: -------------------------------------------------------------------------------- 1 | % Gauss Elimination | used as a function for Least Squares Method 2 | 3 | function x = GE(A) 4 | 5 | % clc; clear all; 6 | 7 | [n,m] = size(A); 8 | for i = 1: n - 1 9 | for k = i + 1: n 10 | multi = A(k,i) / A(i,i); 11 | A(k, :) = -multi * A(i, :) + A(k, :); 12 | end 13 | end 14 | 15 | x = zeros(1,n); 16 | for i = n: -1: 1 17 | x(i) = ((A(i,m) - sum(A(i,1:n) .* x)) / (A(i,i))); 18 | end 19 | 20 | x 21 | end 22 | -------------------------------------------------------------------------------- /LUdecomp.m: -------------------------------------------------------------------------------- 1 | % LU Decemposition 2 | 3 | clc; clear all; 4 | 5 | A = [2 3 4; 4 11 14; 2 8 17]; 6 | B = [19; 55; 50]; 7 | L = eye(3); 8 | [n,m] = size(A); 9 | for i = 1: n - 1; 10 | for k = i + 1: n 11 | multi = A(k,i) / A(i,i); 12 | L(k, i) = multi; 13 | A(k, :) = -multi * A(i, :) + A(k, :); 14 | end 15 | end 16 | 17 | U = A; 18 | L = [L B]; 19 | y = zeros(1, n); 20 | [n, m] = size(L); 21 | for i = 1: n 22 | y(i) = ((L(i, m) - sum(L(i, 1:n) .* y)) / (L(i,i))); 23 | end 24 | 25 | U = [U y.']; 26 | x = zeros(1, n); 27 | [n, m] = size(U); 28 | for i = n: -1: 1 29 | x(i) = ((U(i, m) - sum(U(i, 1:n) .* x)) / (U(i,i))); 30 | end 31 | x' 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UMA011-NumericalAnalysis-LabCodes 2 | numerical analysis (uma011) is a course taught to engineering students of many disciplines. in this repository, you can find all the codes that are executed in the labs. 3 | 4 | list of codes that you can find in this repository: 5 | index of activities performed in the numerical analysis labs 6 | -------------------------------------------------------------------------------- /bisectionMethod.m: -------------------------------------------------------------------------------- 1 | % Bisection Method 2 | 3 | clc; clear all; 4 | 5 | f = @(x) tan(pi * x) - x - 6; 6 | tol = 0.00001; 7 | a = 0.4; 8 | b = 0.48; 9 | count = 1; 10 | 11 | while ((b - a) / 2 > tol) 12 | p = (a + b) / 2; 13 | fa = sign(f(a)); 14 | fb = sign(f(b)); 15 | fp = sign(f(p)); 16 | if ((f(a) * f(p)) < 0) 17 | b = p; 18 | else 19 | a = p; 20 | end 21 | count = count + 1; 22 | end 23 | a 24 | b 25 | count 26 | -------------------------------------------------------------------------------- /compTrap_Simp13.m: -------------------------------------------------------------------------------- 1 | clc; clear all; 2 | 3 | f = @(x) sin(x); 4 | a = 0; 5 | b = pi / 4; 6 | n = 6; 7 | 8 | % Composite Trapezoidal 9 | xvec = linspace(a, b, n+1); 10 | h = (b - a) / n; 11 | A = sum(f(xvec(2 : end - 1))) 12 | compTrap = (h / 2) * (f(a) + 2 * A + f(b)) 13 | 14 | % Composite Simpson 1/3 15 | if(rem(n, 2) == 0) 16 | xvec = linspace(a, b, n + 1); 17 | h = (b - a) / n; 18 | A = sum(f(xvec(2: 2: end - 1))); 19 | B = sum(f(xvec(3: 2: end - 1))); 20 | compSimp13 = (h / 3) * (f(a) + 4 * A + 2 * B + f(b)) 21 | end 22 | -------------------------------------------------------------------------------- /fixedPoint.m: -------------------------------------------------------------------------------- 1 | % Fixed Point Iteration Method 2 | 3 | clc; clear all; 4 | 5 | %f = @(x) (x^3 + x^2 - 3) / 3; 6 | %f = @(x) (3 + 3 * x - x^2)^(1/3); 7 | f = @(x) -1 + (3 * x + 3) / x^2; 8 | 9 | tol = 0.01; 10 | old = 1; % Assuming 1st point.. 11 | error = 10; 12 | while (error > tol) 13 | new = f(old); 14 | error = abs(old - new); 15 | old = new; 16 | end 17 | 18 | new 19 | -------------------------------------------------------------------------------- /gaussElimination.m: -------------------------------------------------------------------------------- 1 | % Gauss Elimination 2 | 3 | clc; clear all; 4 | 5 | A = [2 3 4 19; 4 11 14 55; 2 8 17 50]; 6 | [n,m] = size(A); 7 | for i = 1: n - 1 8 | for k = i + 1: n 9 | multi = A(k,i) / A(i,i); 10 | A(k, :) = -multi * A(i, :) + A(k, :); 11 | end 12 | end 13 | 14 | A 15 | 16 | x = zeros(1,n); 17 | for i = n: -1: 1 18 | x(i) = ((A(i,m) - sum(A(i,1:n) .* x)) / (A(i,i))); 19 | end 20 | 21 | x 22 | -------------------------------------------------------------------------------- /gaussJacobi.m: -------------------------------------------------------------------------------- 1 | % Gauss Jacobi 2 | 3 | clc; clear all; 4 | 5 | % A = [8 3 4; 4 16 14; 2 8 17]; 6 | % B = [19 55 50]'; 7 | A = [5 1 2; -3 9 4; 1 2 -7]; 8 | B = [10 -14 -33]'; 9 | 10 | oldX = [0 0 0]'; 11 | D = diag(diag(A)); 12 | L = tril(A) - D; 13 | U = triu(A) - D; 14 | 15 | error = 100; 16 | tol = 0.000000001; 17 | while(error > tol) 18 | newX = inv(D) * B - inv(D) * (L + U) * oldX; 19 | error = norm(newX - oldX, inf); 20 | oldX = newX; 21 | end 22 | 23 | newX 24 | error 25 | -------------------------------------------------------------------------------- /gaussSeidal.m: -------------------------------------------------------------------------------- 1 | % Gauss Seidal 2 | 3 | clc; clear all; 4 | 5 | A = [1 0.5 0.5; 0.5 1 0.5; 0.5 0.5 1]; 6 | B = [1 1 1]'; 7 | 8 | oldX = [0 0 0]'; 9 | D = diag(diag(A)); 10 | L = tril(A) - D; 11 | U = triu(A) - D; 12 | 13 | error = 100; 14 | tol = 0.0000000001; 15 | while(error > tol) 16 | % newX = inv(D) * B - inv(D) * (L + U) * oldX; <- Gauss Jacobi 17 | newX = (D + L) \ B - ((D + L) \ U) * oldX; % <- Gauss Seidal 18 | error = norm(newX - oldX, inf); 19 | oldX = newX; 20 | end 21 | 22 | newX 23 | error 24 | -------------------------------------------------------------------------------- /lagrangeInterpolation.m: -------------------------------------------------------------------------------- 1 | % Lagrange Interpolation 2 | 3 | clc; clear all; 4 | 5 | xvec = [0 1 2 3]; 6 | yvec = [1 3 3 3]; 7 | [~, n] = size(xvec); 8 | p = zeros(n, n); 9 | num = zeros(n, 4); 10 | den = zeros(4, 1); 11 | 12 | for i = 1: n 13 | roots = xvec; 14 | roots(i) = []; 15 | num(i, :) = poly(roots); 16 | den(i) = polyval(num(i, :), xvec(i)); 17 | p(i, :) = num(i, :) / den(i) * yvec(i); 18 | end 19 | 20 | finalCoeff = sum(p, 1); 21 | lagrangePoly = poly2sym(finalCoeff) 22 | polyval(finalCoeff, 0.3) 23 | -------------------------------------------------------------------------------- /leastSquaresMethod.m: -------------------------------------------------------------------------------- 1 | % Least Squares Method 2 | 3 | clc; clear all; 4 | 5 | xvec = [1 2 3 4 5 6 7 8 9 10]; [~, n] = size(xvec); 6 | yvec = [1.3 3.5 4.2 5.0 7.0 8.8 10.1 12.5 13.0 15.6]; 7 | 8 | degree = 1; 9 | X = zeros(degree + 1, n); 10 | for i = 1: degree + 1 11 | X(i, :) = xvec .^(i - 1); 12 | end 13 | 14 | A = X * X'; 15 | b = X * yvec'; 16 | A = [A b]; 17 | x = GE(A); 18 | scatter(xvec, yvec) 19 | hold on; 20 | x = flip(x); 21 | poly2sym(x); 22 | Approx = polyval(x, xvec); 23 | Error = sum((Approx - yvec) .^ 2, 2) 24 | plot(xvec, Approx) 25 | hold off; 26 | -------------------------------------------------------------------------------- /newtonDividedDifference.m: -------------------------------------------------------------------------------- 1 | % Newton's Divided Difference 2 | 3 | clc; clear all; 4 | 5 | xvec = [0 1 3 4 7]; 6 | yvec = [1 3 49 129 813]; 7 | [~, n] = size(xvec); 8 | yNaught = yvec(1); 9 | top = zeros(1, n - 1); 10 | for i = 1: n - 1 11 | yvec = diff(yvec); 12 | a = xvec(i + 1: end) - xvec(1: end - i); 13 | yvec = yvec ./ a; 14 | top(i) = yvec(1); 15 | end 16 | 17 | ddp = zeros(1, n); 18 | for i = 1: n - 1 19 | roots = xvec; 20 | roots(i + 1: end) = []; 21 | ddp = ddp + [zeros(1, n - i - 1) poly(roots) * top(i)]; 22 | end 23 | constant = [zeros(1, n - 1) yNaught]; 24 | finalddp = ddp + constant; 25 | polynomial = poly2sym(finalddp) 26 | polyval(finalddp, 0.3) 27 | -------------------------------------------------------------------------------- /newtonMethod.m: -------------------------------------------------------------------------------- 1 | % Newton's Method 2 | 3 | clc; clear all; 4 | 5 | %f = @(x) x^3 + 2 * x^2 - 3 * x - 1; 6 | %fdashx = @(x) 3 * x^2 + 4 * x - 3; 7 | f = @(x) sin(x); 8 | fdashx = @(x) cos(x); 9 | 10 | old = 5; 11 | error = 10; 12 | tol = 10^(-11); 13 | count = 1; 14 | 15 | while (error >= tol) 16 | new = old - (f(old) / fdashx(old)); 17 | error = abs(new - old); 18 | old = new; 19 | count = count + 1; 20 | end 21 | 22 | old 23 | new 24 | error 25 | -------------------------------------------------------------------------------- /powerMethod.m: -------------------------------------------------------------------------------- 1 | % Power Method 2 | 3 | clc; clear all; 4 | 5 | A = [-2 -2 3; -10 -1 6; 10 -2 -9]; 6 | oldX = [1 0 0]'; 7 | errVec = 10; 8 | errEig = 10; 9 | eigOld = 10; 10 | tol = 5 * 10^(-6); 11 | 12 | while(errEig > tol || errVec > tol) 13 | Y = A * oldX; 14 | [~, index] = max(abs(Y(:))); 15 | eigNew = Y(index); 16 | errEig = abs(eigNew - eigOld); 17 | eigOld = eigNew; 18 | newX = Y./ Y(index); 19 | errVec = norm(newX - oldX, inf); 20 | oldX = newX; 21 | end 22 | 23 | newX' 24 | eigNew 25 | -------------------------------------------------------------------------------- /secantMethod.m: -------------------------------------------------------------------------------- 1 | % Secant Method 2 | 3 | clc; clear all; 4 | 5 | f = @(x) x^3 + 2 * x^2 - 3 * x - 1; 6 | current = 2; 7 | old = 1; 8 | 9 | error = 10; 10 | tol = 10^(-12); 11 | count = 1; 12 | 13 | while (error >= tol) 14 | new = current - ((f(current) * (current - old)) / (f(current) - f(old))); 15 | error = abs(new - current); 16 | old = current; 17 | current = new; 18 | count = count + 1; 19 | end 20 | 21 | current 22 | old 23 | new 24 | count 25 | -------------------------------------------------------------------------------- /trapSimpson13and38.m: -------------------------------------------------------------------------------- 1 | clc; clear all; 2 | 3 | f = @(x) sin(x); 4 | a = 0; 5 | b = pi / 4; 6 | n = 1; 7 | 8 | % Trapezoidal Rule 9 | iTrap = ((b - a) / (2)) * (f(a) + f(b)) 10 | 11 | % Simpsons 1/3 Rule 12 | n = 2; 13 | iSimp13 = ((b - a) / (2 * 3)) * (f(a) + 4 * f((a + b) / 2) + f(b)) 14 | 15 | % Simpsons 3/8 Rule 16 | n = 3; 17 | h = (b - a) / 3; 18 | iSimp38 = (3 * (h / 8)) * (f(a) + 3 * f(a + h) + 3 * f(a + (2 * h)) + f(b)) 19 | --------------------------------------------------------------------------------