Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cfgnunes committed Mar 2, 2022
1 parent efe6bec commit 7166421
Show file tree
Hide file tree
Showing 26 changed files with 407 additions and 280 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Numerical methods implementation in MATLAB.

For the "Python version", see [this repository](https://github.com/cfgnunes/numerical-methods-python).
For the implementation in Python, see [this repository](https://github.com/cfgnunes/numerical-methods-python).

## Getting Started

Expand Down
21 changes: 14 additions & 7 deletions differentiation/derivative_backward_difference.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
function [dy] = derivative_backward_difference(x, y)
% Calculate the first derivative
% All values in 'x' must be equally spaced
% Inputs:
% x: Array containing x values
% y: Array containing y values
% Outputs:
% dy: Array containing the first derivative values
% Calculate the first derivative.
%
% All values in 'x' must be equally spaced.
%
% Args:
% x: array containing x values.
% y: array containing y values.
%
% Returns:
% dy: array containing the first derivative values.

x_size = size(x, 2);
y_size = size(y, 2);
Expand All @@ -22,13 +25,17 @@

n = x_size;
dy = zeros(1, n);

for i = 1:n

if i == n
h = x(i) - x(i - 1);
dy(i) = dy_difference(- h, y(i), y(i - 1));
else
h = x(i + 1) - x(i);
dy(i) = dy_difference(h, y(i), y(i + 1));
end

end

end
21 changes: 14 additions & 7 deletions differentiation/derivative_five_point.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
function [dy] = derivative_five_point(x, y)
% Calculate the first derivative
% All values in 'x' must be equally spaced
% Inputs:
% x: Array containing x values
% y: Array containing y values
% Outputs:
% dy: Array containing the first derivative values
% Calculate the first derivative.
%
% All values in 'x' must be equally spaced.
%
% Args:
% x: array containing x values.
% y: array containing y values.
%
% Returns:
% dy: array containing the first derivative values.

x_size = size(x, 2);
y_size = size(y, 2);
Expand All @@ -24,13 +27,17 @@
h = x(2) - x(1);
n = x_size;
dy = zeros(1, n);

for i = 1:n

if i == 1 || i == 2
dy(i) = dy_end(h, y(i), y(i + 1), y(i + 2), y(i + 3), y(i + 4));
elseif i == n || i == n - 1
dy(i) = dy_end(- h, y(i), y(i - 1), y(i - 2), y(i - 3), y(i - 4));
else
dy(i) = dy_mid(h, y(i - 2), y(i - 1), y(i + 1), y(i + 2));
end

end

end
21 changes: 14 additions & 7 deletions differentiation/derivative_three_point.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
function [dy] = derivative_three_point(x, y)
% Calculate the first derivative
% All values in 'x' must be equally spaced
% Inputs:
% x: Array containing x values
% y: Array containing y values
% Outputs:
% dy: Array containing the first derivative values
% Calculate the first derivative.
%
% All values in 'x' must be equally spaced.
%
% Args:
% x: array containing x values.
% y: array containing y values.
%
% Returns:
% dy: array containing the first derivative values.

x_size = size(x, 2);
y_size = size(y, 2);
Expand All @@ -24,13 +27,17 @@
h = x(2) - x(1);
n = x_size;
dy = zeros(1, n);

for i = 1:n

if i == 1
dy(i) = dy_end(h, y(i), y(i + 1), y(i + 2));
elseif i == n
dy(i) = dy_end(- h, y(i), y(i - 1), y(i - 2));
else
dy(i) = dy_mid(h, y(i - 1), y(i + 1));
end

end

end
16 changes: 10 additions & 6 deletions integration/composite2_simpson.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
function [xi] = composite2_simpson(x, y)
% Calculate the integral from 1/3 Simpson's Rule
% Inputs:
% x: Array containing x values
% y: Array containing y values
% Outputs:
% xi: Integral value
% Calculate the integral from 1/3 Simpson's Rule.
%
% Args:
% x: array containing x values.
% y: array containing y values.
%
% Returns:
% xi: integral value.

x_size = size(x, 2);
y_size = size(y, 2);
Expand All @@ -20,11 +22,13 @@
sum_even = 0;

for i = 2:(n - 1)

if mod(i, 2) == 0
sum_even = sum_even + y(i);
else
sum_odd = sum_odd + y(i);
end

end

xi = h / 3 * (y(1) + 2 * sum_even + 4 * sum_odd + y(n));
Expand Down
14 changes: 8 additions & 6 deletions integration/composite2_trapezoidal.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
function [xi] = composite2_trapezoidal(x, y)
% Calculate the integral from Trapezoidal Rule
% Inputs:
% x: Array containing x values
% y: Array containing y values
% Outputs:
% xi: Integral value
% Calculate the integral from Trapezoidal Rule.
%
% Args:
% x: array containing x values.
% y: array containing y values.
%
% Returns:
% xi: integral value.

x_size = size(x, 2);
y_size = size(y, 2);
Expand Down
20 changes: 12 additions & 8 deletions integration/composite_simpson.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
function [xi] = composite_simpson(f, b, a, n)
% Calculate the integral from 1/3 Simpson's Rule
% Inputs:
% f: Function f(x)
% a: Initial point
% b: End point
% n: Number of intervals
% Outputs:
% xi: Integral value
% Calculate the integral from 1/3 Simpson's Rule.
%
% Args:
% f: function f(x).
% a: initial point.
% b: end point.
% n: number of intervals.
%
% Returns:
% xi: integral value.

h = (b - a) / n;

Expand All @@ -15,11 +17,13 @@

for i = 1:(n - 1)
x = a + i * h;

if mod(i, 2) == 0
sum_even = sum_even + f(x);
else
sum_odd = sum_odd + f(x);
end

end

xi = h / 3 * (f(a) + 2 * sum_even + 4 * sum_odd + f(b));
Expand Down
18 changes: 10 additions & 8 deletions integration/composite_trapezoidal.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
function [xi] = composite_trapezoidal(f, b, a, n)
% Calculate the integral from Trapezoidal Rule
% Inputs:
% f: Function f(x)
% a: Initial point
% b: End point
% n: Number of intervals
% Outputs:
% xi: Integral value
% Calculate the integral from Trapezoidal Rule.
%
% Args:
% f: function f(x).
% a: initial point.
% b: end point.
% n: number of intervals.
%
% Returns:
% xi: integral value.

h = (b - a) / n;

Expand Down
22 changes: 15 additions & 7 deletions interpolation/lagrange.m
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
function [y_int] = lagrange(x, y, x_int)
% Interpolates a value using Lagrange polynomial
% Inputs:
% x: Array containing x values
% y: Array containing y values
% x_int: Value to interpolate
% Outputs:
% y_int: Interpolated value
% Interpolates a value using Lagrange polynomial.
%
% Args:
% x: array containing x values.
% y: array containing y values.
% x_int: value to interpolate.
%
% Returns:
% y_int: interpolated value.

n = size(x, 2);

y_int = 0;

for i = 1:n
p = y(i);

for j = 1:n

if i ~= j
p = p * ((x_int - x(j)) / (x(i) - x(j)));
end

end

y_int = y_int + p;
end

end
22 changes: 13 additions & 9 deletions interpolation/neville.m
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
function [y_int, q] = neville(x, y, x_int)
% Interpolates a value using Neville polynomial
% Inputs:
% x: Array containing x values
% y: Array containing y values
% x_int: Value to interpolate
% Outputs:
% y_int: Interpolated value
% q: Coefficients matrix
% Interpolates a value using Neville polynomial.
%
% Args:
% x: array containing x values.
% y: array containing y values.
% x_int: value to interpolate.
%
% Returns:
% y_int: interpolated value.
% q: coefficients matrix.

n = size(x, 2);
q = zeros(n, n - 1);
q = [y' q]; % Insert 'y' in the first column of the matrix 'q'
q = [y' q]; % Insert 'y' in the first column of the matrix 'q'

for i = 2:n

for j = 2:i
q(i, j) = (x_int - x(i - j + 1)) * q(i, j - 1) - (x_int - x(i)) * q(i - 1, j - 1);
q(i, j) = q(i, j) / (x(i) - x(i - j + 1));
end

end

y_int = q(n, n);
Expand Down
16 changes: 10 additions & 6 deletions linear_systems/backward_substitution.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
function [x] = backward_substitution(u, d)
% Solve the upper linear system ux=d
% Inputs:
% u: Upper triangular matrix
% d: Array containing d values
% Outputs:
% x: Solution of linear system
% Solve the upper linear system ux=d.
%
% Args:
% upper: upper triangular matrix.
% d: array containing d values.
%
% Returns:
% x: solution of linear system.

[n, m] = size(u);

Expand All @@ -15,11 +17,13 @@
x = zeros(1, n);

for i = n:-1:1

if (u(i, i) == 0)
error('Error: Matrix "u" is singular.')
end

x(i) = d(i) / u(i, i);
d(1:i - 1) = d(1:i - 1) - u(1:i - 1, i) * x(i);
end

end
16 changes: 10 additions & 6 deletions linear_systems/forward_substitution.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
function [x] = forward_substitution(l, c)
% Solve the lower linear system lx=c
% Inputs:
% l: Lower triangular matrix
% c: Array containing c values
% Outputs:
% x: Solution of linear system
% Solve the lower linear system lx=c.
%
% Args:
% lower: lower triangular matrix.
% c: array containing c values.
%
% Returns:
% x: solution of linear system.

[n, m] = size(l);

Expand All @@ -15,11 +17,13 @@
x = zeros(1, n);

for i = 1:n

if (l(i, i) == 0)
error('Error: Matrix "l" is singular.')
end

x(i) = c(i) / l(i, i);
c(i + 1:n) = c(i + 1:n) - l(i + 1:n, i) * x(i);
end

end
Loading

0 comments on commit 7166421

Please sign in to comment.