Comprehensive Guide To Numerical Methods For ECE Students (Code Claude)
Comprehensive Guide To Numerical Methods For ECE Students (Code Claude)
Engineering
Table of Contents
1. Power Method for Dominant Eigenvalue
2. Gauss-Seidel Method
3. LU Decomposition
4. Thomas Algorithm
What is it?
The Power Method finds the largest eigenvalue (dominant eigenvalue) and its corresponding eigenvector
of a matrix. Think of it as repeatedly multiplying a vector by the matrix until it converges to the dominant
eigenvector.
How it works:
1. Start with an initial guess vector
2. Multiply by the matrix
3. Normalize the result
C Code:
c
#include <stdio.h>
#include <math.h>
#define N 3
#define MAX_ITER 1000
#define TOLERANCE 1e-6
// Check convergence
if(fabs(lambda_new - lambda_old) < TOLERANCE) {
printf("Converged after %d iterations\n", iter + 1);
break;
}
lambda_old = lambda_new;
}
// Store results
eigenvalue = lambda_new;
for(int i = 0; i < N; i++) {
eigenvector[i] = x[i];
}
int main() {
double A[N][N] = {{4, -2, 1},
{1, 3, -1},
{2, 1, 5}};
double eigenvalue;
double eigenvector[N];
return 0;
}
2. Gauss-Seidel Method
What is it?
The Gauss-Seidel method solves systems of linear equations (Ax = b) iteratively. It's like solving each
equation for one variable and using updated values immediately.
How it works:
1. Rearrange each equation to solve for one variable
C Code:
c
#include <stdio.h>
#include <math.h>
#define N 3
#define MAX_ITER 1000
#define TOLERANCE 1e-6
// Check convergence
double max_error = 0;
for(int i = 0; i < N; i++) {
double error = fabs(x[i] - x_old[i]);
if(error > max_error) {
max_error = error;
}
}
printf("Solution:\n");
for(int i = 0; i < N; i++) {
printf("x[%d] = %.6f\n", i, x[i]);
}
printf("\n");
}
int main() {
// System: 10x + y + z = 12
// x + 10y + z = 12
// x + y + 10z = 12
double A[N][N] = {{10, 1, 1},
{1, 10, 1},
{1, 1, 10}};
double b[N] = {12, 12, 12};
double x[N];
return 0;
}
3. LU Decomposition
What is it?
LU Decomposition breaks a matrix A into two parts: L (Lower triangular) and U (Upper triangular), where A
= L × U. This makes solving Ax = b easier by solving two simpler triangular systems.
How it works:
1. Decompose A into L and U matrices
2. Solve Ly = b (forward substitution)
C Code:
c
#include <stdio.h>
#define N 3
// Decomposition
for(int i = 0; i < N; i++) {
// Upper triangular matrix U
for(int k = i; k < N; k++) {
double sum = 0;
for(int j = 0; j < i; j++) {
sum += L[i][j] * U[j][k];
}
U[i][k] = A[i][k] - sum;
}
int main() {
double A[N][N] = {{2, -1, -2},
{-4, 6, 3},
{-4, -2, 8}};
double b[N] = {-1, 0, 3};
double L[N][N], U[N][N];
double y[N], x[N];
lu_decomposition(A, L, U);
printf("L matrix:\n");
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
printf("%.3f ", L[i][j]);
}
printf("\n");
}
printf("\nU matrix:\n");
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
printf("%.3f ", U[i][j]);
}
printf("\n");
}
forward_substitution(L, b, y);
backward_substitution(U, y, x);
printf("\nSolution:\n");
for(int i = 0; i < N; i++) {
printf("x[%d] = %.6f\n", i, x[i]);
}
printf("\n");
return 0;
}
4. Thomas Algorithm
What is it?
The Thomas Algorithm efficiently solves tridiagonal systems of equations. It's a specialized version of
Gaussian elimination for matrices that have non-zero elements only on three diagonals.
When to use:
When your matrix looks like:
C Code:
c
#include <stdio.h>
#define N 4
void thomas_algorithm(double a[N], double b[N], double c[N], double d[N], double x[N]) {
double c_prime[N], d_prime[N];
// Forward sweep
c_prime[0] = c[0] / b[0];
d_prime[0] = d[0] / b[0];
// Back substitution
x[N-1] = d_prime[N-1];
for(int i = N-2; i >= 0; i--) {
x[i] = d_prime[i] - c_prime[i] * x[i+1];
}
}
int main() {
// Tridiagonal system example
// Note: a[0] and c[N-1] are not used
double a[N] = {0, -1, -1, -1}; // Lower diagonal
double b[N] = {2, 2, 2, 2}; // Main diagonal
double c[N] = {1, 1, 1, 0}; // Upper diagonal
double d[N] = {1, 0, 0, 1}; // Right hand side
double x[N];
thomas_algorithm(a, b, c, d, x);
printf("Solution:\n");
for(int i = 0; i < N; i++) {
printf("x[%d] = %.6f\n", i, x[i]);
}
printf("\n");
return 0;
}
5. Jacobi Method for Eigenvalues
What is it?
The Jacobi method finds all eigenvalues of a symmetric matrix by repeatedly applying rotations to make
the matrix diagonal. The diagonal elements become the eigenvalues.
How it works:
1. Find the largest off-diagonal element
2. Apply a rotation to make it zero
3. Repeat until the matrix is diagonal
C Code:
c
#include <stdio.h>
#include <math.h>
#define N 3
#define MAX_ITER 1000
#define TOLERANCE 1e-10
// Check convergence
if(max_val < TOLERANCE) {
printf("Converged after %d iterations\n", iter);
break;
}
// Apply rotation to A
double temp_pp = A[p][p];
double temp_qq = A[q][q];
double temp_pq = A[p][q];
printf("Eigenvalues:\n");
for(int i = 0; i < N; i++) {
printf("λ[%d] = %.6f\n", i, eigenvalues[i]);
}
printf("\n");
}
int main() {
double A[N][N] = {{4, -2, 1},
{-2, 4, -2},
{1, -2, 4}};
double eigenvalues[N];
return 0;
}
6. Givens Method for Eigenvalues
What is it?
The Givens method uses Givens rotations to transform a symmetric matrix into tridiagonal form, then
applies the QR algorithm to find eigenvalues. It's more stable than Jacobi for large matrices.
How it works:
1. Transform matrix to tridiagonal form using Givens rotations
C Code:
c
#include <stdio.h>
#include <math.h>
#define N 3
#define MAX_ITER 1000
#define TOLERANCE 1e-10
printf("Approximate Eigenvalues:\n");
for(int i = 0; i < N; i++) {
printf("λ[%d] = %.6f\n", i, eigenvalues[i]);
}
printf("\n");
}
int main() {
double A[N][N] = {{4, 1, 2},
{1, 4, 1},
{2, 1, 4}};
double eigenvalues[N];
return 0;
}
2. Gauss-Seidel: For solving linear systems when the matrix is diagonally dominant
3. LU Decomposition: For solving multiple systems with the same coefficient matrix
4. Thomas Algorithm: Specifically for tridiagonal systems (very efficient)
Remember: These are fundamental building blocks used in more complex engineering software!