ppt code
ppt code
Use Theorem 2.1 to find a bound for the number of iterations needed to achieve an
approximation with accuracy 10−4 to the solution of x3 −x−1 = 0 lying in the interval [1, 2].
Find an approximation to the root with this degree of accuracy
1. Given values:
• 𝑎=1a=1
• 𝑏=2b=2
• 𝜖=10−4ϵ=10−4
𝑛≥log(2−1)−log(10−4)log(2)=log(1)−log(10−4)log(2)n
≥log(2)log(2−1)−log(10−4)=log(2)log(1)−log(10−4)
𝑛≥−log(10−4)log(2)=log(104)log(2)=4log(10)log(2)n≥l
og(2)−log(10−4)=log(2)log(104)=log(2)4log(10)
3. Substitute values:
• log(10)≈2.302585log(10)≈2.302585
• log(2)≈0.693147log(2)≈0.693147
4. Calculate:
𝑛≥4×2.3025850.693147≈9.210340.693147≈13.28n≥0.6931474×2.302
585≈0.6931479.21034≈13.28
import math
# Given values
a = 1
b = 2
epsilon = 1e-4
# Initial interval
a = 1
b = 2
tolerance = 1e-4
# Initial interval
a = 0
b = 1
tolerance = 1e-5
root_high_precision = bisection_method(a, b, tolerance)
print(f"Root: {root_high_precision}")
import math
def f(x):
return math.exp(x) - x**2 + 3*x - 2
# Initial interval
a = 0
b = 1
tolerance = 1e-5
import math
def f(x):
return 2 * x * math.cos(2 * x) - (x + 1) ** 2
# Intervals
intervals = [(-3, -2), (-1, 0)]
tolerance = 1e-5
roots = []
for a, b in intervals:
root = bisection_method(a, b, tolerance)
roots.append(root)
print(f"Root in interval [{a}, {b}]: {root}")
roots
import math
def f(x):
return x * math.cos(x) - 2 * x**2 + 3 * x - 1
# Intervals
intervals = [(0.2, 0.3), (1.2, 1.3)]
tolerance = 1e-5
roots = []
for a, b in intervals:
root = bisection_method(a, b, tolerance)
roots.append(root)
print(f"Root in interval [{a}, {b}]: {root}")
roots
def g(x):
return (3 * x**2 + 3)**0.25
# Initial guess
p0 = 1
tolerance = 1e-2
import math
def g(x):
return (1 / math.pi) * (math.asin((-1 / 2) * x) + 2 * math.pi)
# Initial guess
p0 = 1
tolerance = 1e-2
def f(x):
return x**3 - 2*x**2 - 5
def df(x):
return 3*x**2 - 4*x
def f(x):
return x**3 + 3*x**2 - 1
def df(x):
return 3*x**2 + 6*x
def f(x):
return x - math.cos(x)
def df(x):
return 1 + math.sin(x)
import math
def f(x):
return x - 0.8 - 0.2 * math.sin(x)
def df(x):
return 1 - 0.2 * math.cos(x)
math.log(x,base) default la e
import numpy as np
x = np.zeros(n)
for i in range(n):
T = [j for j in E[i:,i] if j !=0]
if len(T) == 0:
print("No unique solution")
return
p = i + list(E[i:,i]).index(T[0])
if p != i:
print("E", p, "<-> E", i)
E[[i, p]] = E[[p, i]]
print(E, "\n")
for j in range(i+1, n):
m = E[j, i]/E[i, i]
print("m=", m)
E[j, :] = E[j, :] - m*E[i, :]
print(E, "\n")
if E[n-1, n-1] == 0:
print("No solution exists")
return
x[n-1] = E[n-1, n]/E[n-1, n-1]
for i in reversed(range(n-1)):
x[i] = (E[i, n] - sum([E[i, j]*x[j] for j in range(i,
n)]))/E[i, i]
print("solution :", x)
x = np.zeros(n)
for i in range(n):
T = [j for j in E[i:,i] if j !=0]
if len(T) == 0:
print("No unique solution")
return
p = i + list(E[i:,i]).index(T[0])
if p != i:
print("E", p, "<-> E", i)
E[[i, p]] = E[[p, i]]
for j in range(i+1, n):
m = rnd(E[j, i]/E[i, i], 2)
print("m=", m)
temp = rnd_v(m*E[i, :], 2)
E[j, :] = rnd_v(E[j, :] - temp, 2)
print(E, "\n")
if E[n-1, n-1] == 0:
print("No solution exists")
return
x[n-1] = rnd(E[n-1, n]/E[n-1, n-1], 2)
for i in reversed(range(n-1)):
temp = [rnd(E[i, j]*x[j], 2) for j in range(i, n)]
temp = rnd(sum(temp), 2)
temp = rnd(E[i, n] - temp, 2)
x[i] = rnd(temp/E[i, i],2)
print(x)
# problem (a)
A = np.array([[4, -1, 1], [2, 5, 2], [1, 2, 4]], dtype=float)
b = np.array([[8], [3], [11]], dtype = float)
g_back_subs(A, b)
# problem (b)
A = np.array([[4, 1, 2], [2, 4, -1], [1, 1, -3]], dtype=float)
b = np.array([[9], [-5], [-9]], dtype = float)
g_back_subs(A, b)
import numpy as np
# Forward substitution
for i in range(n):
y[i] = b[i] - np.dot(L[i, :i], y[:i])
# Backward substitution
for i in range(n-1, -1, -1):
x[i] = (y[i] - np.dot(U[i, i+1:], x[i+1:])) / U[i, i]
return x
# Coefficient matrix A
A = np.array([[3, 9, 6],
[18, 48, 39],
[9, -27, 42]])
for _ in range(iterations):
for i in range(n):
sum_ax = sum(A[i][j] * x[j] for j in range(n) if j != i)
x[i] = (b[i] - sum_ax) / A[i][i]
return x
# System of equations
A = np.array([[10, 1, 1],
[1, 10, 1],
[1, 1, 10]])
b = np.array([6, 6, 6])
# Initial guess
x0 = np.array([1, 1, 1], dtype=float)
# Number of iterations
iterations = 5
import numpy as np
for _ in range(iterations):
for i in range(n):
sum_ax = sum(A[i][j] * x[j] for j in range(n) if j != i)
x[i] = round((b[i] - sum_ax) / A[i][i], 6)
return x
# System of equations
A = np.array([[10, 1, 1],
[1, 10, 1],
[1, 1, 10]])
b = np.array([6, 6, 6])
# Initial guess
x0 = np.array([1, 1, 1], dtype=float)
# Number of iterations
iterations = 5
import numpy as np
for _ in range(iterations):
for i in range(n):
sum_ax = sum(A[i][j] * x[j] for j in range(n) if j != i)
x_new[i] = round((b[i] - sum_ax) / A[i][i], 6)
x = x_new.copy()
return x
# System of equations
A = np.array([[5, 1, 2],
[1, 4, -2],
[2, 3, 8]])
b = np.array([19, -2, 39])
# Initial guess
x0 = np.array([1, 1, 1], dtype=float)
# Number of iterations
iterations = 5
# Value to interpolate
x = 8.4
# Degree 1 interpolation
x_values_deg1 = x_values[:2]
y_values_deg1 = y_values[:2]
f_approx_deg1 = lagrange_interpolating_polynomial(x_values_deg1,
y_values_deg1, x)
# Degree 2 interpolation
x_values_deg2 = x_values[:3]
y_values_deg2 = y_values[:3]
f_approx_deg2 = lagrange_interpolating_polynomial(x_values_deg2,
y_values_deg2, x)
# Degree 3 interpolation
f_approx_deg3 = lagrange_interpolating_polynomial(x_values, y_values, x)
import numpy as np
# Degree 1 interpolation
approx_a_deg1 = newton_backward_polynomial(x_values_a, y_values_a,
x_to_approx_a, 1)
# Degree 2 interpolation
approx_a_deg2 = newton_backward_polynomial(x_values_a, y_values_a,
x_to_approx_a, 2)
# Degree 3 interpolation
approx_a_deg3 = newton_backward_polynomial(x_values_a, y_values_a,
x_to_approx_a, 3)
# Degree 1 interpolation
approx_b_deg1 = newton_backward_polynomial(x_values_b, y_values_b,
x_to_approx_b, 1)
# Degree 2 interpolation
approx_b_deg2 = newton_backward_polynomial(x_values_b, y_values_b,
x_to_approx_b, 2)
# Degree 3 interpolation
approx_b_deg3 = newton_backward_polynomial(x_values_b, y_values_b,
x_to_approx_b, 3)
import numpy as np
import matplotlib.pyplot as plt
# Given data
xi = np.array([4.0, 4.2, 4.5, 4.7, 5.1, 5.5, 5.9, 6.3, 6.8, 7.1])
yi = np.array([102.56, 113.18, 130.11, 142.05, 167.53, 195.14, 224.87,
256.73, 299.50, 326.72])
plt.xlabel('xi')
plt.ylabel('yi')
plt.legend()
plt.title('Least Squares Polynomial Fits')
plt.show()
import numpy as np
from scipy.stats import linregress
# Given data
xi = np.array([4.0, 4.2, 4.5, 4.7, 5.1, 5.5, 5.9, 6.3, 6.8, 7.1])
yi = np.array([102.56, 113.18, 130.11, 142.05, 167.53, 195.14, 224.87,
256.73, 299.50, 326.72])
# Compute a and b
a = slope
B = intercept
b = np.exp(B)
print("a:", a)
print("b:", b)
print("Error:", error)
import numpy as np
from scipy.stats import linregress
# Given data
xi = np.array([4.0, 4.2, 4.5, 4.7, 5.1, 5.5, 5.9, 6.3, 6.8, 7.1])
yi = np.array([102.56, 113.18, 130.11, 142.05, 167.53, 195.14, 224.87,
256.73, 299.50, 326.72])
# Compute a and b
a = slope
B = intercept
b = 10 ** B
print("a:", a)
print("b:", b)
print("Error:", error)
# Given data
x_values = [1.1, 1.2, 1.3, 1.4]
f_values = [9.025013, 11.02318, 13.46374, 16.44465]
h = x_values[1] - x_values[0] # Assuming equally spaced points
"""
Approximates the integral of f(x) from a to b using the Trapezoidal rule with n subintervals.
"""
h = (b - a) / n
integral_sum = 0
# Compute the sum of the function values at the endpoints and the midpoint of each
subinterval
for i in range(n):
x0 = a + i * h
x1 = a + (i + 1) * h
return integral_sum
def integrand(x):
return x**4
lower_limit = 0.5
upper_limit = 1
num_subintervals = 1000
# Approximate the integral using the Trapezoidal rule
print("The approximate value of the integral using the Trapezoidal rule is:", result)
return integral_sum
import numpy as np
return error
# Initialize t and y
t = a
y = y0
# Update t
t += h
# Initialize t and y
t = a
y = y0
# Update t
t += h
import sympy as sp
# Initialize t and y
t = a
y = y0
# Update t
t += h
import sympy as sp
# Initialize t and y
t = a
y = y0
# Update t
t += h
# Example usage:
# Define the differential equation y' = f(t, y)
def f(t, y):
return t * sp.exp(3 * t) - 2 * y
# Example usage:
# Define the differential equation y' = f(t, y)
def f(t, y):
return t * sp.exp(3 * t) - 2 * y
# Update t
t += h
# Corrector step
w_next = w + h / 24 * (9 * f(t + h, w_star) + 19 * f(t, w) - 5
* f(t - h, w_values[-2]) + f(t - 2*h, w_values[-3]))
import numpy as np
from scipy.interpolate import CubicSpline
np.log(np.exp(x) + 2)
import numpy as np
s_prime_0 = 1
s_prime_2 = 1
y_eval = cubic_spline(x_eval)
print(cubic_spline.c)
plt.xlabel('x')
plt.ylabel('s(x)')
plt.legend()
plt.grid(True)
plt.show()