This is a console application, a project which implements various numerical methods to solve mathematical problems.
- Solution of Linear Equations
- Solution of Non-linear Equations
- Solution of Differential Equations
- Matrix Inversion
- Arrange the given system of linear equations in diagonally dominant form.
- set the tolerable error;
- Convert the first equation in terms of first variable , second equation in the term of second variable and so on.
for example,
x1=1/a11(b1-a12*x2-a13*x3).x2=1/a12(b2-a11*x1-a13*x3).x3=1/a13(b3-a11*x1-a12*x2). - Set the initial guesses for x10,x20,x30 and so on;
- Substitute the value of
x10,x20,x30,.......in equation obtained from step 4 to calculate new values ofx1,x2,x3 and so on; - Repeat step 5 until
(|x10-x1|)>eand(|x20-x2|)>eand(|x30-x3|)>eand so on ; - Print the value of x1,x2,x3 and so on;
- Arrange the given system of linear equations in diagonally dominant form.
- set the tolerable error;
- Convert the first equation in terms of first variable , second equation in the term of second variable and so on.
for example,
x1=1/a11(b1-a12*x2-a13*x3).x2=1/a12(b2-a11*x1-a13*x3).x3=1/a13(b3-a11*x1-a12*x2). - Set the initial guesses for
x10,x20,x30 and so on; - Substitute the value of
x20,x30,.......in the first equation obtained from step 4 to calculate new value x1.Then usex1,x20,x30,......in second equation to calculate new value of x2 and usex1,x2,x30,.....in third equation to calculate new value of x3 and so on; - Repeat step 5 until
(|x10-x1|)>eand(|x10-x1|)>eand(|x10-x1|)>e; - Print the value of x1,x2,x3 and so on;
Purpose: The Gauss Elimination method is a systematic procedure to solve a system of linear equations. It converts the matrix into an upper triangular form, from which you can use back-substitution to find the solution.
- Convert the coefficient matrix to an upper triangular form by performing row operations.
- The goal is to have all zeros below the main diagonal of the matrix.
- To achieve this, use the following operations:
- Subtract a multiple of one row from another to make the elements below the pivot (diagonal) equal to zero.
- Once the matrix is in upper triangular form, start from the last row and work your way up to solve for each variable.
- Use previously computed variable values to solve the current equation.
x + y + z = 6
2x + 3y + 7z = 18
3x + 5y + 2z = 14
____ ____
| |
| 1 1 1 |6 |
| 2 3 7 |18 |
| 3 5 2 |14 |
|___ ___|
- Perform row operations to make it upper triangular.
- Use back-substitution to find the values of (x), (y), and (z).
- For each column
iin A:- Find pivot element in row i
- For each row j below the pivot row:
Factor = A[j][i] / A[i][i]- For each column k in row j:
A[j][k] = A[j][k] - Factor * A[i][k]
b[j] = b[j] - Factor * b[i]
- For row
ifrom last to first:x[i] = (b[i] - sum(A[i][j] * x[j] for j from i+1 to end)) / A[i][i]
Purpose: Gauss-Jordan Elimination is an extension of Gauss Elimination. It simplifies the system further to the Reduced Row Echelon Form (RREF), from which the solution can be read directly without back-substitution.
Forward Elimination:- Similar to Gauss Elimination, perform row operations to make the elements below the main diagonal zero.
Backward Elimination:- Normalize the pivot elements to 1 by dividing the entire row by the pivot value.
- Use the pivot rows to make all entries above and below each pivot equal to zero.
- The solutions x, y, and z can be directly read from the matrix.
- The resulting matrix will be in Reduced Row Echelon Form, which looks like:
____ ____
| |
| 1 0 0 |x |
| 0 1 0 |y |
| 0 0 1 |z |
|___ ___|
From the above augmented matrix:
- Use additional row operations to make elements above the diagonal zero.
- Normalize the diagonal elements to 1.
- Directly extract the solutions from the final matrix.
- For each column i in A:
- Normalize row i by dividing by the pivot A[i][i]
- For each row j (j != i):
- Factor = A[j][i]
- For each column k in row j:
- A[j][k] = A[j][k] - Factor * A[i][k]
- b[j] = b[j] - Factor * b[i]
LU Factorization is a fundamental technique in linear algebra used to solve systems of linear equations. It expresses a given square matrix A as a product of two matrices, one lower L and one upper U i.e. A = LU. This simplifies many matrix operations, making it an essential tool in various engineering applications.
- Given a set of linear equations of the form
a11x1 + a12x2 + a13x3 + ... + a1nxn = b
a21x1 + a22x2 + a23x3 + ... + a2nxn = b
a31x1 + a32x2 + a33x3 + ... + a3nxn = b
.....................................
an1x1 + an2x2 + an3x3 + ... + annxn = b
convert them into matrix formAX = BwhereAis the coefficient matrix,Xis variable matrix andBis the matrix of constants on the right side of the equation. - Create two matrices naming
LandUof the same size asA. Calculate the values of the elements of both matrices such thatLU = A - Now equation can be written as
LUX = BorLY = BwhereUX = Y - Solve
YfromLY = Band then usingYsolveXfromUX = Y
Bi-section method is used to find root of an equation in a given interval i.e. value of x for which f(x) = 0.
The method is based on The Intermediate Value Theorem which states that if f(x) is a continuous function and there are two real numbers a and b such that f(a)*f(b) < 0, then it is guaranteed that it has at least one root between them.
f(x)is a continuous function in interval[a, b]f(a) * f(b) < 0
- Find middle point
c = (a + b)/2. Iff(c) == 0, thencis the root of the solution.Elsef(c) != 0Ifvaluef(a)*f(c) < 0then root lies betweenaandc. So we recur foraandcElse Iff(b)*f(c) < 0then root lies betweenbandc. So we recurbandc.Elsegiven function doesn’t follow one of assumptions.
- Repeat the steps until difference between
aandbis less then a value (very small value).
Like Bi-section method, False Position method is also an approximation method to find the roots of a given equation by repeatedly dividing the interval.
False position method is the same as Bi-section method with the only difference is that instead of finding the middle point we find the point that touches the x-axis.
- Find middle point
c = (a*func(b)-b*func(a))/(func(b)-func(a)). Iff(c) == 0, thencis the root of the solution.Elsef(c) != 0Ifvaluef(a)*f(c) < 0then root lies betweenaandc. So we recur foraandcElse Iff(b)*f(c) < 0then root lies betweenbandc. So we recurbandc.Elsegiven function doesn’t follow one of assumptions.
- Repeat the steps until difference between
aandbis less then a value (very small value).
- Define the function as f(x).
- Assume initial guesses x1,x2.
- Calculate f(x1) and f(x2) and if f(x1)=f(x2) , then print "Mathematical Error" and terminate the program.
- Determine
x3=x2-(x2-x1)*f(x2)/f(x2)-f(x1). - Check if
f(x3)=0,- if true , then print the solution x3.
- else replace
x1=x2 and x3=x2.
- Repeat step 3 ,4,5 until
f(x3)=0.
- Define the function as f(x).
- Assume initial guesses x0.
- Calculate the derivatives of f(x) and define it as f '(x).
- Calculate f(x0) and f '(x0) .
- Check
if f'(x0)=0, then then print"Mathematical Error"and terminate the program. - Determine
x1=x0-f(x0)/f '(x0). - Check if
f(x1)=0,- if true then print the solution x1.
- else replace x0=x1.
- Repeat step 3 ,4,5 until f(x3)=0.
Purpose: The Runge-Kutta method is a numerical technique used to solve ordinary differential equations (ODEs) of the form:
- dy/dx=f(x,y)
This method estimates the value of y at a given x using an iterative approach.
The 4th-Order Runge-Kutta Method is the most commonly used variant due to its balance of accuracy and computational efficiency. Here’s how it works:
1. `Initialize`:
-> Start with the initial condition: x0, y0.
-> Define the step size hand the endpoint x_end.
2. Iterative Steps:
-> Compute four slopes (k-values) for each step:
-> k1 = h*f(x,y)
-> k2 = h*f( x + 0.5h, y + 0.5k1)
-> k3 = h*f(x + 0.5h,y + 0.5k2)
-> k4 = h*f(x + h,y + k3)
3. Update the next value:
-> y_next = y + 1/6(k1 + 2k2 + 2k3 + k4)
-> Increment x by h.
-> Repeat until the target x_end is reached.
Given:
dy/dx = x + y, x0 = 0, y0 = 1,x_end = 2, h = 0.1
The program calculates intermediate values using the 4th-order Runge-Kutta method. It prints out the x and y values at each step.
- Initialize x = x0, y = y0
- While x < x_end:
-
k1 = h * f(x, y)
-
k2 = h * f(x + 0.5 * h, y + 0.5 * k1)
-
k3 = h * f(x + 0.5 * h, y + 0.5 * k2)
-
k4 = h * f(x + h, y + k3)
-
y = y + (1/6) * (k1 + 2k2 + 2k3 + k4)
-
x = x + h
-
Matrix inversion is used to find the inverse of a matrix, which can then be applied to solve a system of linear equations. The inverse of a matrix A, denoted by A-1 in such that:
A * A-1= I
where I is the identity matrix.
- For a system of equations:
- AX=B
- A is the coefficient matrix.
- X is the column vector of unknowns.
- B is the column vector of constants.
- If A is invertible, the solution is given by:
- -X= A-1*B
- If the determinant of A is zero, the matrix is singular (non-invertible).
If you have a coefficient matrix A:
____ ___
| |
| 1 1 1 |
| 2 3 7 |
| 3 5 2 |
|___ ___|
The program computes A-1 and displays it.
- If det(A) == 0:
- Return "Matrix is singular and cannot be inverted"
- Else:
- Use np.linalg.inv(A) to compute the inverse
- Return A^{-1}









