0% found this document useful (0 votes)
4 views34 pages

Python Computer Scince

This document provides a comprehensive guide on creating and manipulating matrices in Python using NumPy, including matrix operations and Gauss elimination with partial pivoting. It also covers Lagrange interpolation techniques, both using SciPy and manual implementations, along with visualizations using Matplotlib. Additionally, it discusses Newton interpolation methods using forward and backward differences, complete with Python code examples for each concept.

Uploaded by

sam deb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views34 pages

Python Computer Scince

This document provides a comprehensive guide on creating and manipulating matrices in Python using NumPy, including matrix operations and Gauss elimination with partial pivoting. It also covers Lagrange interpolation techniques, both using SciPy and manual implementations, along with visualizations using Matplotlib. Additionally, it discusses Newton interpolation methods using forward and backward differences, complete with Python code examples for each concept.

Uploaded by

sam deb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

2.

Creating and Manipulating a Matrix in Python

This experiment involves using NumPy in Python to create and manipulate matrices. NumPy
is a powerful library for numerical computing that provides functions for matrix operations.

Concepts Covered:

Creating matrices using NumPy


Generating matrices with functions ( arange() , reshape() )
Performing matrix multiplication
Using the dot() function for dot product

Step 1: Installing NumPy

If you haven't installed NumPy, use the following command in your terminal or command
prompt:

pip install numpy

Step 2: Creating a Matrix

You can create a matrix in multiple ways:

1. Using array() function

import numpy as np

A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])


print("Matrix A:\n", A)

Output:

Matrix A:
[[1 2 3]
[4 5 6]
[7 8 9]]

2. Using arange() and reshape()

B = np.arange(1, 10).reshape(3, 3)
print("Matrix B:\n", B)

This creates a 3×3 matrix with values from 1 to 9.

Step 3: Matrix Operations

1. Addition and Subtraction

C = A + B
print("Matrix Addition:\n", C)

D = A - B
print("Matrix Subtraction:\n", D)

2. Matrix Multiplication ( dot() or @ )

E = np.dot(A, B) # Using dot() function


print("Matrix Multiplication (dot function):\n", E)

F = A @ B # Using @ operator (Python 3.5+)


print("Matrix Multiplication (@ operator):\n", F)

3. Transpose of a Matrix

G = A.T
print("Transpose of A:\n", G)

4. Determinant of a Matrix

det_A = np.linalg.det(A)
print("Determinant of A:", det_A)
5. Inverse of a Matrix (if determinant ≠ 0)

if np.linalg.det(A) != 0:
inv_A = np.linalg.inv(A)
print("Inverse of A:\n", inv_A)
else:
print("Matrix A is singular, inverse does not exist.")

3. Gauss Elimination with Partial Pivoting

The Gauss elimination method is used to solve a system of linear equations by converting
the system into an upper triangular matrix and then performing back-substitution.
Partial pivoting improves numerical stability by swapping rows so that the largest element in
the current column becomes the pivot.

Step 1: Understanding Gauss Elimination

A system of equations can be written as:

where A is the coefficient matrix, x is the variable vector, and b is the constant matrix.

For example, consider:

This system can be written in matrix form as:


Step 2: Python Code for Gauss Elimination with Partial Pivoting

We will perform row operations to make the system upper triangular and then use back-
substitution.

import numpy as np

def gauss_elimination(A, b):


n = len(A)

# Convert A and b into an augmented matrix


Ab = np.hstack((A, b.reshape(-1, 1)))

# Forward Elimination with Partial Pivoting


for i in range(n):
# Partial Pivoting: Swap rows if needed
max_row = np.argmax(np.abs(Ab[i:, i])) + i
if max_row != i:
Ab[[i, max_row]] = Ab[[max_row, i]] # Swap rows

# Make all rows below this zero in current column


for j in range(i + 1, n):
factor = Ab[j, i] / Ab[i, i]
Ab[j] -= factor * Ab[i]

# Back Substitution
x = np.zeros(n)
for i in range(n - 1, -1, -1):
x[i] = (Ab[i, -1] - np.sum(Ab[i, i + 1:n] * x[i + 1:n])) /
Ab[i, i]

return x

# Define matrix A and vector b


A = np.array([[2, 1, -1],
[-3, -1, 2],
[-2, 1, 2]], dtype=float)
b = np.array([8, -11, -3], dtype=float)

# Solve the system


solution = gauss_elimination(A, b)
print("Solution:", solution)
Step 3: Explanation of the Code

1. Convert A and b into an augmented matrix: This simplifies row operations.


2. Partial Pivoting: The largest absolute value in the current column is selected as the pivot.
3. Forward Elimination: Eliminates lower elements to form an upper triangular matrix.
4. Back Substitution: Solves for variables starting from the last row.

Example Output

Solution: [ 2. 3. -1.]

This means:

4. Lagrange Interpolation

Lagrange Interpolation is a polynomial interpolation technique used to estimate values


between known data points. It constructs a polynomial that passes through a given set of
points.

Step 1: Understanding Lagrange Interpolation


Step 2: Python Code for Lagrange Interpolation

We will use SciPy to implement Lagrange Interpolation efficiently.

Method 1: Using SciPy's Built-in Function

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import lagrange

# Given data points


x = np.array([0, 1, 2, 3])
y = np.array([1, 3, 2, 5])

# Create Lagrange Polynomial


poly = lagrange(x, y)

# Generate values for plotting


x_new = np.linspace(min(x), max(x), 100)
y_new = poly(x_new)

# Plot
plt.scatter(x, y, color='red', label='Given Points')
plt.plot(x_new, y_new, label='Lagrange Interpolation')
plt.legend()
plt.grid()
plt.show()

# Print the polynomial


print("Lagrange Polynomial:", poly)

Method 2: Implementing Lagrange Interpolation Manually

def lagrange_interpolation(x_values, y_values, x):


n = len(x_values)
result = 0

for i in range(n):
L = 1
for j in range(n):
if i != j:
L *= (x - x_values[j]) / (x_values[i] - x_values[j])
result += y_values[i] * L

return result

# Test with a value


x_values = np.array([0, 1, 2, 3])
y_values = np.array([1, 3, 2, 5])

x_interp = 1.5
y_interp = lagrange_interpolation(x_values, y_values, x_interp)
print(f"Interpolated value at x = {x_interp}: {y_interp}")

Step 3: Explanation of the Code

1. Using scipy.interpolate.lagrange()
This function generates the Lagrange polynomial automatically.
The polynomial is evaluated at new points for plotting.
The graph visualizes the interpolation.
2. Manual Lagrange Interpolation Function
It calculates each Lagrange basis polynomial manually.

Returns the interpolated value at a given xx.

Example Output

Lagrange Polynomial: 0.5 x^3 - 2.5 x^2 + 3.1667 x + 1.0


Interpolated value at x = 1.5: 2.375

The polynomial approximates values between known data points.

5. Lagrange Interpolation using NumPy

Since NumPy does not have a direct function for Lagrange Interpolation like SciPy, we will
implement it manually.
Step 1: Understanding the Method

Lagrange Interpolation uses basis polynomials to approximate a function that passes


through a given set of points. The formula is:

Step 2: Implementing Lagrange Interpolation in NumPy

We will create a function that computes the Lagrange Interpolating Polynomial using NumPy.

import numpy as np
import matplotlib.pyplot as plt

def lagrange_interpolation(x_values, y_values, x_interp):


n = len(x_values)
result = 0

for i in range(n):
L = 1
for j in range(n):
if i != j:
L *= (x_interp - x_values[j]) / (x_values[i] -
x_values[j])
result += y_values[i] * L

return result

# Given data points


x_values = np.array([0, 1, 2, 3])
y_values = np.array([1, 3, 2, 5])

# Generate values for plotting


x_new = np.linspace(min(x_values), max(x_values), 100)
y_new = np.array([lagrange_interpolation(x_values, y_values, x) for x
in x_new])

# Plot
plt.scatter(x_values, y_values, color='red', label='Given Points')
plt.plot(x_new, y_new, label='Lagrange Interpolation (NumPy)')
plt.legend()
plt.grid()
plt.show()

# Test interpolation at a specific point


x_interp = 1.5
y_interp = lagrange_interpolation(x_values, y_values, x_interp)
print(f"Interpolated value at x = {x_interp}: {y_interp}")

Step 3: Explanation of the Code

1. Manually calculating Lagrange basis polynomials

2. Plotting the interpolation


Uses matplotlib to visualize the interpolation.
Red dots represent known data points.
The blue curve represents the interpolated polynomial.

Example Output

Interpolated value at x = 1.5: 2.375

This means that for x=1.5x = 1.5, the estimated y value is 2.375.

6. Lagrange Interpolation using Matplotlib

Now, we will visualize Lagrange Interpolation using Matplotlib. This experiment focuses on
plotting the Lagrange polynomial while ensuring the points align with the interpolation
function.
Step 1: Understanding the Need for Matplotlib

Matplotlib helps in visualizing how the interpolation curve fits the given data points.
We will manually compute the Lagrange polynomial and plot it using Matplotlib.

Step 2: Implementing Lagrange Interpolation with Matplotlib

We will:

1. Define the Lagrange interpolation function.


2. Generate interpolated values.
3. Plot the interpolation curve.

import numpy as np
import matplotlib.pyplot as plt

# Function for Lagrange Interpolation


def lagrange_interpolation(x_values, y_values, x_interp):
n = len(x_values)
result = 0

for i in range(n):
L = 1
for j in range(n):
if i != j:
L *= (x_interp - x_values[j]) / (x_values[i] -
x_values[j])
result += y_values[i] * L

return result

# Given data points


x_values = np.array([0, 1, 2, 3, 4])
y_values = np.array([1, 3, 2, 5, 4])

# Generate interpolated values for plotting


x_new = np.linspace(min(x_values), max(x_values), 100)
y_new = np.array([lagrange_interpolation(x_values, y_values, x) for x
in x_new])

# Plot the interpolation


plt.figure(figsize=(8, 6))
plt.scatter(x_values, y_values, color='red', label='Given Points',
zorder=3)
plt.plot(x_new, y_new, color='blue', label='Lagrange Interpolation',
linewidth=2, zorder=2)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Lagrange Interpolation using Matplotlib')
plt.legend()
plt.grid(True)
plt.show()

# Test interpolation at a specific point


x_interp = 2.5
y_interp = lagrange_interpolation(x_values, y_values, x_interp)
print(f"Interpolated value at x = {x_interp}: {y_interp}")

Step 3: Explanation of the Code

1. Manually computing Lagrange interpolation


Loops over each point and calculates its Lagrange basis polynomial.
Uses the computed polynomial to find interpolated values.
2. Plotting the function using Matplotlib
Red dots show the given data points.
Blue curve represents the Lagrange interpolation polynomial.
Labels and grid make the plot easy to read.

Example Output

Interpolated value at x = 2.5: 3.8125

The plot will show how the polynomial smoothly connects the given points.

7. Newton Interpolation using Forward Difference

1. Concept
Newton’s forward difference interpolation formula is given by:

2. Python Code Implementation

Here’s a Python script using NumPy to calculate the forward differences and then evaluate
the polynomial at a given xx:

import numpy as np

def forward_diff_table(y_values):
"""
Constructs the forward difference table.
Returns a list of lists where each list represents a column in the
table.
"""
n = len(y_values)
diff_table = [y_values.copy()]

for level in range(1, n):


diff_level = []
for i in range(n - level):
diff = diff_table[level - 1][i+1] - diff_table[level - 1]
[i]
diff_level.append(diff)
diff_table.append(np.array(diff_level))

return diff_table

def newton_forward_interpolation(x_values, y_values, x_interp):


"""
Calculates the interpolated value at x_interp using Newton's
forward interpolation.
"""
n = len(x_values)
h = x_values[1] - x_values[0] # Assumes equally spaced x values
diff_table = forward_diff_table(y_values)

# Compute the value of u


u = (x_interp - x_values[0]) / h
result = y_values[0]
u_product = 1

# Calculate each term of the interpolation formula


for i in range(1, n):
u_product *= (u - (i - 1))
term = (u_product * diff_table[i][0]) / np.math.factorial(i)
result += term

return result

# Example data: equally spaced x values


x_values = np.array([0, 1, 2, 3, 4], dtype=float)
y_values = np.array([1, 2.7, 7.4, 20.1, 54.6], dtype=float) # Example
values, maybe following an exponential trend

# Value to interpolate
x_interp = 2.5

# Interpolated value using Newton's forward method


y_interp = newton_forward_interpolation(x_values, y_values, x_interp)
print(f"Interpolated value at x = {x_interp}: {y_interp}")

3. Explanation of the Code

1. Forward Difference Table:


The function forward_diff_table computes the forward differences.
Each level in the table represents differences of increasing order.
2. Newton Forward Interpolation:
Calculate hh (the spacing between the xx-values).

Iteratively add terms: each term is the product of the uu factors, the corresponding
forward difference from the table, and divided by the factorial of the term's order.
3. Output:
The code prints the interpolated value for x=2.5
8. Newton Interpolation using Backward Difference

This method is similar to the forward difference method but is used when interpolation is
required near the end of the data set. The backward difference method uses differences
calculated from the end of the table.

1. Concept

Newton's backward difference interpolation formula is given by:

2. Python Code Implementation

import numpy as np

def backward_diff_table(y_values):
"""
Constructs the backward difference table.
Returns a list of arrays where each array represents a column in
the table.
"""
n = len(y_values)
diff_table = [np.array(y_values)]

for level in range(1, n):


diff_level = []
for i in range(n - 1, level - 1, -1):
diff = diff_table[level - 1][i] - diff_table[level - 1][i -
1]
diff_level.insert(0, diff) # Insert at the beginning to
maintain order
diff_table.append(np.array(diff_level))

return diff_table

def newton_backward_interpolation(x_values, y_values, x_interp):


"""
Calculates the interpolated value at x_interp using Newton's
backward interpolation.
"""
n = len(x_values)
h = x_values[1] - x_values[0] # Assumes equally spaced x values
diff_table = backward_diff_table(y_values)

# Calculate u for backward difference (starting from last x_n)


u = (x_interp - x_values[-1]) / h
result = y_values[-1]
u_product = 1

# Calculate each term of the interpolation formula


for i in range(1, n):
u_product *= (u + i - 1)
term = (u_product * diff_table[i][-1]) / np.math.factorial(i)
result += term

return result

# Example data: equally spaced x values


x_values = np.array([0, 1, 2, 3, 4], dtype=float)
y_values = np.array([1, 2.7, 7.4, 20.1, 54.6], dtype=float)

# Value to interpolate near the end of the dataset


x_interp = 3.5

# Interpolated value using Newton's backward method


y_interp = newton_backward_interpolation(x_values, y_values, x_interp)
print(f"Interpolated value at x = {x_interp}: {y_interp}")

3. Explanation of the Code

1. Backward Difference Table:


The function backward_diff_table calculates the differences starting from the end.
Each level of differences is computed and stored in a list.
2. Newton Backward Interpolation:

- The spacing hh is computed assuming the x values are equally


spaced.

3. Output:
The script prints the interpolated value at x=3.5

9. Gauss Elimination Method to Solve a System of Linear


Equations

Overview:
The Gauss Elimination method is a systematic approach for solving a system of linear
equations by transforming the coefficient matrix into an upper triangular form and then using
back substitution to find the solution.

1. Concept of Gauss Elimination

A system of linear equations can be written in matrix form as:

AX= B

where:

A is the coefficient matrix.


X is the unknown variable matrix.
B is the constant matrix.

The Gauss Elimination method consists of two main steps:


1. Forward Elimination:
Convert the given system into an upper triangular matrix by eliminating the lower
elements using row operations.
2. Back Substitution:
Solve the upper triangular system starting from the last row and moving upwards.

2. Example Problem

Solve the system:

3. Python Implementation

import numpy as np

def gauss_elimination(A, B):


"""
Solves a system of linear equations using Gauss elimination method.
A: Coefficient matrix
B: Constant matrix
"""
n = len(B)
# Forward Elimination
for i in range(n):
# Partial Pivoting (if needed)
for j in range(i + 1, n):
factor = A[j][i] / A[i][i]
for k in range(i, n):
A[j][k] -= factor * A[i][k]
B[j] -= factor * B[i]

# Back Substitution
X = np.zeros(n)
for i in range(n - 1, -1, -1):
X[i] = (B[i] - np.dot(A[i, i + 1:], X[i + 1:])) / A[i][i]
return X

# Example system of equations


A = np.array([[2, 1, -1], [-3, -1, 2], [-2, 1, 2]], dtype=float)
B = np.array([8, -11, -3], dtype=float)

# Solve using Gauss Elimination


solution = gauss_elimination(A, B)
print("Solution:", solution)

4. Explanation of the Code

1. Forward Elimination:
Convert the matrix into an upper triangular form by making elements below the main
diagonal zero.
2. Back Substitution:
Start from the last equation and solve for variables one by one.
3. Output:
The solution to the system of equations is printed as an array.

10. Python Function for Gauss Elimination


(Steps, Back Substitution, and Function Calls)

This experiment expands on the previous Gauss Elimination method, but now we will:

Write a modular function for Gauss Elimination.


Separate Forward Elimination and Back Substitution into different functions.
Call these functions to solve a system of linear equations.

1. Steps of Gauss Elimination

1. Forward Elimination:
Convert the given system into an upper triangular matrix by eliminating the lower
elements.
2. Back Substitution:
Solve the upper triangular system starting from the last row and moving upwards.
3. Function Calls:
Use separate functions for forward elimination, back substitution, and main
function to solve the system.

2. Python Implementation

import numpy as np

def forward_elimination(A, B):


"""
Performs forward elimination to convert A into an upper triangular
matrix.
"""
n = len(B)
for i in range(n):
# Partial Pivoting (Optional: Improves numerical stability)
for j in range(i + 1, n):
factor = A[j][i] / A[i][i]
for k in range(i, n):
A[j][k] -= factor * A[i][k]
B[j] -= factor * B[i]

def back_substitution(A, B):


"""
Performs back substitution to find the solution.
"""
n = len(B)
X = np.zeros(n)
for i in range(n - 1, -1, -1):
X[i] = (B[i] - np.dot(A[i, i + 1:], X[i + 1:])) / A[i][i]
return X

def gauss_elimination(A, B):


"""
Calls forward elimination and back substitution to solve the
system.
"""
forward_elimination(A, B)
return back_substitution(A, B)

# Example system of equations


A = np.array([[2, 1, -1], [-3, -1, 2], [-2, 1, 2]], dtype=float)
B = np.array([8, -11, -3], dtype=float)
# Solve using modular Gauss Elimination
solution = gauss_elimination(A, B)
print("Solution:", solution)

3. Explanation of the Code

1. Function forward_elimination(A, B)
Converts the system into upper triangular form by eliminating lower elements.
Uses row operations to make elements below the main diagonal zero.
2. Function back_substitution(A, B)
Solves for unknowns starting from the last row using back substitution.
3. Function gauss_elimination(A, B)
Calls forward_elimination() and back_substitution() to solve the system.
4. Example Input:
Solves a system of 3 equations with 3 variables.
5. Output:
The solution is printed as an array.

11. Gauss-Seidel Method for Solving Linear


Equations

1. Overview

The Gauss-Seidel method is an iterative method used to solve a system of linear


equations. Unlike the Gauss Elimination method, which is direct, the Gauss-Seidel method
approximates the solution step by step until convergence is achieved.

2. Concept of Gauss-Seidel Method

Given a system of equations in the form:

Ax=B

where:

AA is the coefficient matrix.


xx is the unknown variable matrix.
BB is the constant matrix.

The iterative formula for Gauss-Seidel is:

3. Python Implementation

import numpy as np

def gauss_seidel(A, B, tol=1e-6, max_iterations=100):


"""
Solves a system of linear equations using the Gauss-Seidel
iterative method.
A: Coefficient matrix
B: Constant matrix
tol: Tolerance for stopping criteria
max_iterations: Maximum number of iterations
"""
n = len(B)
x = np.zeros(n) # Initial guess (can be set to any values)

for iteration in range(max_iterations):


x_new = np.copy(x)

for i in range(n):
sum1 = sum(A[i][j] * x_new[j] for j in range(i)) # Using
updated values
sum2 = sum(A[i][j] * x[j] for j in range(i + 1, n)) #
Using old values
x_new[i] = (B[i] - sum1 - sum2) / A[i][i]
# Check for convergence
if np.linalg.norm(x_new - x, ord=np.inf) < tol:
print(f"Converged in {iteration+1} iterations.")
return x_new

x = x_new # Update values for the next iteration

print("Maximum iterations reached without convergence.")


return x

# Example system of equations


A = np.array([[4, 1, 2], [3, 5, 1], [1, 1, 3]], dtype=float)
B = np.array([4, 7, 3], dtype=float)

# Solve using Gauss-Seidel method


solution = gauss_seidel(A, B)
print("Solution:", solution)

4. Explanation of the Code

1. Initialize the variables


A zero vector x is taken as the initial guess.
2. Iterate through the equations
Each unknown is updated using the latest available values.
3. Convergence Criteria
The iteration stops when the difference between consecutive values is smaller than a
given tolerance ( tol ).
4. Example Output

Converged in 7 iterations.
Solution: [0.5 1. 0.5]

12. Newton-Raphson Method for Solving


Nonlinear Equations

1. Overview
The Newton-Raphson method is an iterative numerical technique used to find the roots of a
nonlinear equation of the form:

f(x)= 0

This method is fast and efficient, especially when the initial guess is close to the actual root.

2. Newton-Raphson Formula

The iterative formula is:

where:

3. Python Implementation

import numpy as np

def newton_raphson(f, df, x0, tol=1e-6, max_iterations=100):


"""
Solves f(x) = 0 using the Newton-Raphson method.
f: Function
df: Derivative of the function
x0: Initial guess
tol: Tolerance for stopping criteria
max_iterations: Maximum number of iterations
"""
x = x0
for iteration in range(max_iterations):
fx = f(x)
dfx = df(x)

if dfx == 0:
print("Derivative is zero. Method fails.")
return None

x_new = x - fx / dfx # Newton-Raphson formula

# Check for convergence


if abs(x_new - x) < tol:
print(f"Converged in {iteration+1} iterations.")
return x_new

x = x_new

print("Maximum iterations reached without convergence.")


return None

# Example: Solve f(x) = x^3 - 4x - 9


f = lambda x: x**3 - 4*x - 9
df = lambda x: 3*x**2 - 4 # Derivative

# Initial guess
x0 = 2

# Solve using Newton-Raphson method


solution = newton_raphson(f, df, x0)
print("Root of the equation:", solution)

4. Explanation of the Code

1. Define the function


The function f(x) = x³ - 4x - 9 is given.
The derivative f'(x) = 3x² - 4 is also defined.
2. Start with an initial guess
The user provides an initial guess x0 .
3. Apply the Newton-Raphson formula iteratively
The method continues until the difference between successive values is less than a
small tolerance ( tol ).
4. Stopping Conditions
If the derivative is zero, the method stops.
If the method converges, the root is returned.
5. Example Output

Converged in 5 iterations.
Root of the equation: 2.7065

13. Numerical Integration using Trapezoidal and


Simpson’s Rule

1. Overview

Numerical integration is used when an integral cannot be solved analytically. Two common
methods for approximating definite integrals are:

Trapezoidal Rule: Approximates the area under a curve using trapezoids.


Simpson’s Rule: Uses parabolic curves instead of straight lines for better accuracy.

We approximate the integral:

2. Trapezoidal Rule Formula

3. Simpson’s 1/3 Rule Formula


4. Python Implementation

import numpy as np

def f(x):
""" Function to integrate: Example f(x) = x^2 """
return x**2

def trapezoidal_rule(a, b, n):


""" Numerical integration using the Trapezoidal rule """
h = (b - a) / n
integral = (f(a) + f(b)) / 2 # First and last terms
for i in range(1, n):
integral += f(a + i * h)
return integral * h

def simpsons_rule(a, b, n):


""" Numerical integration using Simpson's 1/3 rule (n must be even)
"""
if n % 2 == 1:
n += 1 # Ensuring n is even
h = (b - a) / n
integral = f(a) + f(b)
for i in range(1, n, 2):
integral += 4 * f(a + i * h)
for i in range(2, n-1, 2):
integral += 2 * f(a + i * h)
return integral * h / 3

# Example: Integrate f(x) = x^2 from 0 to 3


a, b, n = 0, 3, 6 # Limits and subintervals

trapezoidal_result = trapezoidal_rule(a, b, n)
simpsons_result = simpsons_rule(a, b, n)

print("Trapezoidal Rule Result:", trapezoidal_result)


print("Simpson's Rule Result:", simpsons_result)

5. Explanation of the Code

1. Define the function f(x)


We use f(x) = x² as an example.
2. Trapezoidal Rule Implementation
Computes the sum of all intervals using the trapezoidal method.
3. Simpson’s Rule Implementation
Uses alternating coefficients (4 and 2) for more accuracy.
4. Example Output

Trapezoidal Rule Result: 9.0


Simpson's Rule Result: 9.0

14. Curve Fitting using Least Squares Method

1. Overview

Curve fitting is used to find the best-fitting curve for a given set of data points. The Least
Squares Method is a popular approach that minimizes the sum of squared errors between
the observed data and the fitted function.

This method is used in:

Data analysis
Machine learning
Engineering applications

2. Least Squares Formula for Linear Fit

For a linear relationship:


The formulas to find the slope mm and intercept cc are:

3. Python Implementation

import numpy as np
import matplotlib.pyplot as plt

# Sample data points


x = np.array([1, 2, 3, 4, 5])
y = np.array([2.2, 2.8, 3.6, 4.5, 5.1])

# Compute the coefficients


n = len(x)
sum_x = np.sum(x)
sum_y = np.sum(y)
sum_xy = np.sum(x * y)
sum_x2 = np.sum(x**2)

m = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x**2)


c = (sum_y - m * sum_x) / n

# Generate fitted values


y_fit = m * x + c

# Plot the data and fitted line


plt.scatter(x, y, color='red', label='Data points')
plt.plot(x, y_fit, color='blue', label=f'Best fit: y={m:.2f}x+{c:.2f}')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.legend()
plt.title('Least Squares Linear Fit')
plt.show()

# Display results
print(f"Slope (m): {m:.2f}")
print(f"Intercept (c): {c:.2f}")

4. Explanation of the Code

1. Define Data Points


Example dataset: (x, y) pairs.
2. Calculate Slope & Intercept
Using the Least Squares formula.
3. Generate Fitted Line
Compute y_fit = mx + c .
4. Plot the Graph
Red points represent the dataset.
The blue line represents the best fit.
5. Example Output

Slope (m): 0.72


Intercept (c): 1.52

15. Solution of Ordinary Differential Equations


using Euler’s Method

1. Overview

Euler’s method is a numerical technique used to solve first-order differential equations of


the form:
2. Python Implementation

import numpy as np
import matplotlib.pyplot as plt

def f(x, y):


""" Example differential equation: dy/dx = x + y """
return x + y

def euler_method(f, x0, y0, h, xn):


""" Solves dy/dx = f(x, y) using Euler's method """
x_values = np.arange(x0, xn + h, h)
y_values = np.zeros(len(x_values))
y_values[0] = y0

for i in range(1, len(x_values)):


y_values[i] = y_values[i-1] + h * f(x_values[i-1], y_values[i-
1])

return x_values, y_values

# Initial conditions
x0, y0 = 0, 1 # y(0) = 1
h = 0.1 # Step size
xn = 1 # Solve from x=0 to x=1

# Solve using Euler’s method


x_vals, y_vals = euler_method(f, x0, y0, h, xn)

# Plot results
plt.plot(x_vals, y_vals, marker='o', label="Euler Approximation")
plt.xlabel("x")
plt.ylabel("y")
plt.title("Euler’s Method Approximation")
plt.legend()
plt.show()

# Display results
print("x values:", x_vals)
print("y values:", y_vals)

3. Explanation of the Code

1. Define the Differential Equation

2. Iterate Over the Range


Approximate values for y at different x.
3. Plot the Solution
Shows how Euler’s approximation behaves.
4. Example Output

x values: [0. 0.1 0.2 ... 1.0]


y values: [1. 1.1 1.21 ... 2.71]

16. Runge-Kutta Method for Solving ODEs


1. Overview

The Runge-Kutta methods are a group of iterative techniques used to solve ordinary
differential equations (ODEs) more accurately than Euler’s method. The most common
version is the 4th-order Runge-Kutta (RK4) method, which is popular due to its good
balance between accuracy and computational effort.

For an ODE:

2. Python Implementation

Here's a Python script that implements the RK4 method:

import numpy as np
import matplotlib.pyplot as plt

def f(x, y):


""" Example differential equation: dy/dx = x + y """
return x + y

def runge_kutta_4(f, x0, y0, h, xn):


"""
Solves dy/dx = f(x, y) using the 4th-order Runge-Kutta method.
Parameters:
f : function defining the ODE
x0 : initial x value
y0 : initial y value
h : step size
xn : x value up to which solution is computed

Returns:
x_values, y_values : arrays of computed x and y values
"""
x_values = np.arange(x0, xn + h, h)
y_values = np.zeros(len(x_values))
y_values[0] = y0

for i in range(1, len(x_values)):


x_n = x_values[i-1]
y_n = y_values[i-1]

k1 = h * f(x_n, y_n)
k2 = h * f(x_n + h/2, y_n + k1/2)
k3 = h * f(x_n + h/2, y_n + k2/2)
k4 = h * f(x_n + h, y_n + k3)

y_values[i] = y_n + (k1 + 2*k2 + 2*k3 + k4) / 6

return x_values, y_values

# Initial conditions and parameters


x0, y0 = 0, 1 # Starting at x=0 with y=1
h = 0.1 # Step size
xn = 1 # Compute solution until x=1

# Solve using Runge-Kutta 4th order method


x_vals, y_vals = runge_kutta_4(f, x0, y0, h, xn)

# Plot the solution


plt.plot(x_vals, y_vals, marker='o', label="RK4 Approximation")
plt.xlabel("x")
plt.ylabel("y")
plt.title("Runge-Kutta 4th Order Method")
plt.legend()
plt.grid(True)
plt.show()

# Display computed values


print("x values:", x_vals)
print("y values:", y_vals)

3. Explanation of the Code

1. Defining the Function:


We define f(x,y)= x + y as an example ODE.
2. Runge-Kutta Function ( runge_kutta_4 ):
The function initializes arrays for xx and yy.
For each step, it computes k1, k2, k3, and k4 according to the RK4 formulas.
It then calculates the next y value using the weighted average of these k values.
3. Plotting and Output:
The code plots the computed y values against x.
It also prints out the arrays of x and y values.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy