from package import redact_ex from package import \ gauss_seidel_solve, \ check_sys_sols, \ deprox_arr import numpy as np EXERCISE_07 = """\ Make a program that is able to solve a system of equations using the Gauss-Seidel method.\ """ redact_ex(EXERCISE_07, 7) a = np.array([[1, 2, -1], [2, 4, 5], [3, -1, -2]], dtype=float) b = np.array([[2], [25], [-5]], dtype=float) a = a.reshape(len(a), len(a)) b = b.reshape(len(b), 1) ab = np.concatenate((a, b), axis=1) print("Matrix of coefficients A (given):", a, sep='\n\n', end='\n\n') print("Vector of independent terms B (given):", b, sep='\n\n', end='\n\n') print("Expanded matrix (AB):", ab, sep='\n\n', end='\n\n') sols = gauss_seidel_solve(ab) print("Obtained solutions:", sols, sep='\n\n', end='\n\n') sols = deprox_arr(sols)
from package import redact_ex from package import montecarlo_integrate import numpy as np EXERCISE_02 = """\ Make a program that is able to compute the integral between 0 and infinity for f(x) = e**(-x)\ """ redact_ex(EXERCISE_02, 2) def f(y): return (1 / y**2) * np.exp(-1 / y + 1) interval = [0, 1] integral, integral_err = montecarlo_integrate(f, interval) print(f"Integral between {interval!s} for f(x): {integral}") print(f"Error for the aboveshown integration: {integral_err:.2e}")
from package import redact_ex from package import dkf EXERCISE_01 = """\ Make a program that is able to compute the numerical k-order derivative of a given function using the method of undetermined coefficients. Apply that to the case f(x) = x**3 - 3*x**2 - x + 3 on x0 = 1.2.\ """ redact_ex(EXERCISE_01, 1) def f(x): return x**3 - 3 * x**2 - x + 3 # df = 3*x**2 - 6*x - 1, on x0 (1.2) = -3.88 # d2f = 6*x - 6, on x0 (1.2) = 1.2 # d3f = 6, on x0 (1.2) = 6 for k in range(1, 6): for h in [1 / d for d in [10**n for n in range(10)]]: v = dkf(f, 1.2, k, delta=h) print('f({k}({x0}) = {v} (h = {h})'.format(k=k, x0=1.2, v=v, h=h), end='\n\n') # print(dkf(f, 1.2, 1, delta = 1e-6)) # print(dkf(f, 1.2, 2, delta = 1e-4)) # print(dkf(f, 1.2, 3, delta = 1e-2))
from package import redact_ex from package import \ gauss_reduce, \ solve_triang_mat, \ check_sys_sols, \ deprox_arr import numpy as np EXERCISE_04 = """\ Make a program that is able to solve a system of equations by the Gauss-Jordan elimination method with full pivoting.\ """ redact_ex(EXERCISE_04, 4) a = np.array([[1, 2, -1], [2, 4, 5], [3, -1, -2]], dtype=float) b = np.array([[2], [25], [-5]], dtype=float) a = a.reshape(len(a), len(a)) b = b.reshape(len(b), 1) ab = np.concatenate((a, b), axis=1) print("Matrix of coefficients A (given):", a, sep='\n\n', end='\n\n') print("Vector of independent terms B (given):", b, sep='\n\n', end='\n\n') print("Expanded matrix (AB):", ab, sep='\n\n', end='\n\n') tmat = gauss_reduce(ab, method='gj-elim', pivoting='total') print("Triangularized matrix (T):", tmat, sep='\n\n', end='\n\n') sols = solve_triang_mat(tmat, method='gj-elim')
from package import \ gauss_reduce, \ solve_triang_mat, \ check_sys_sols, \ deprox_arr import numpy as np EXERCISE_03 = """\ Make a program that is able to solve a system of equations by the Gauss elimination method with full pivoting and solution computing by regressive substitution.\ """ redact_ex(EXERCISE_03, 3) a = np.array([[1, 2, -1], [2, 4, 5], [3, -1, -2]], dtype=float) b = np.array([[2], [25], [-5]], dtype=float) a = a.reshape(len(a), len(a)) b = b.reshape(len(b), 1) ab = np.concatenate((a, b), axis=1) print("Matrix of coefficients A (given):", a, sep='\n\n', end='\n\n') print("Vector of independent terms B (given):", b, sep='\n\n', end='\n\n') print("Expanded matrix (AB):", ab, sep='\n\n', end='\n\n') tmat = gauss_reduce(ab, pivoting='total') print("Triangularized matrix (T):", tmat, sep='\n\n', end='\n\n') sols = solve_triang_mat(tmat)
from package import redact_ex import numpy as np import random EXERCISE_06 = """\ Make a program that computes the volume of the part of a torus shown in the figure (<figure>) using the Monte Carlo method.\ """ redact_ex(EXERCISE_06, 6) a, b = 1, 3 npoints = 1e6 n = int(npoints) inc = 0 for i in range(n): x, y, z = random.uniform(1, 4), random.uniform(-3, 4), random.uniform(-1, 1) if (np.sqrt(x**2 + y**2) - b)**2 + z**2 <= a**2: inc += 1 area = 3*7*2 volume = inc*area/n print("Obtained volume:", volume, sep = '\n', end = '\n\n')
from package import redact_ex from package import recursive_integrate import numpy as np EXERCISE_05 = """\ Make a program that computes the integral of a function f(x) in an interval [a, b] using the recursive rule of Simpson 1/3. Apply that to the case f(x) = (x**2 + x + 1) * cos(x) and a = 0, b = pi/2\ """ redact_ex(EXERCISE_05, 5) def f(x): return (x**2 + x + 1) * np.cos(x) interval = [0, np.pi / 2] integral_ab = recursive_integrate(f, interval, method='simpson', prec=1e-12) print(f"Integrating f(x) in {interval!s} yields (recursive Simpson 1/3):", integral_ab, sep='\n')
from package import redact_ex from package import euler_differentiate import numpy as np import warnings warnings.filterwarnings('ignore') EXERCISE_00 = """\ Make a program that is able to graphically solve the equation dx/dt = sin(x) using the Euler method.\ """ redact_ex(EXERCISE_00, 0) # dx/dt = sin(x) def inct(dt, *o): return dt def incx(dt, t, x): return dt*np.sin(x) print("Computing Euler method...", end='\n\n') euler_differentiate([inct, incx], bounds = [0, 1], delta = 1e-2, itern = 1e4, title = r"Euler method for function" + r"$\:\:\frac{dx}{dt} = \sin(x)$")
from package import redact_ex from package import \ euler_differentiate, \ range_kutta_differentiate import numpy as np EXERCISE_02i = """\ Make a program that is able to graphically solve the equation d\u00B2x/dt\u00B2 + b dx/dt + \u03C9_0\u00B2 x = 0 using the Euler method.\ """ redact_ex(EXERCISE_02i, '2i') # dx/dt = y # dy/dt = -b*y - omega b = 1/2; omega_0 = 2 def inct(dt, *o): return dt def incx(dt, t, x, y): return dt * y def incy(dt, t, x, y, b = b, omega_0 = omega_0): return dt * (-b*y - omega_0**2*x)