Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Question R1.
The roots of the equation is obtained with the roots function, which returned an array of the
(possibly complex) roots.
def f(z):
y = z**4.0 + z + 1.0
return y
z0 = 1.0
z = fsolve(f, z0)
print("z = ", z)
print("f(z) = ", f(z))
import numpy as np
import numpy.polynomial.polynomial as poly
c = np.array([1.0, 0.0, 0.0, 1.0, 1.0])
r = poly.polyroots(c)
print("Roots of the polynomial")
print(r)
z = [-0.62962079]
f(z) = [ 0.52752988]
[ 0.72713608+0.93409929j 0.72713608-0.93409929j -0.72713608+0.43001429j
-0.72713608-0.43001429j]
C:\Program Files\Anaconda3\lib\site-packages\scipy\optimize\minpack.py:161: RuntimeWarning:
The iteration is not making good progress, as measured by the
improvement from the last five Jacobian evaluations.
warnings.warn(msg, RuntimeWarning)
Linear Algebra
Question L2.
The Jacobi method is a matrix iterative method used to solve the equation Ax=b for a known square
matrix A of size nn and known vector b or length n. The algorithm for the Jacobi method is
relatively straightforward. We begin with the following matrix equation:
Ax=bAx=b
AA is split into the sum of two separate matrices, DD and RR, such
that A=D+RA=D+R. Dii=AiiDii=Aii, but Dij=0Dij=0, for ijij. RR is essentially the
opposite. Rii=0Rii=0, but Rij=AijRij=Aij for ijij. The solution to the equation, i.e. the value
of xx, is given by the following iterative equation:
x(k+1)=D1(bRx(k)).x(k+1)=D1(bRx(k)).
We will make use of the NumPy library to speed up the calculation of the Jacobi method. NumPy is
significantly more efficient than writing an implementation in pure Python. The iterative nature of
the Jacobi method means that any increases in speed within each iteration can have a large impact
on the overall calculation.
import numpy as np
from scipy.linalg import solve
D = np.diag(A)
R = A - np.diagflat(D)
for i in range(n):
x = (b - np.dot(R,x))/ D
print(x)
return x
'''___Main___'''
x = jacobi(A, b, x, n)
print(solve(A, b))
Numerical Integral
Question I2.
Compute a definite integral. Integrate func from a to b using a technique from the Fortran library
QUADPACK.
def integrand(x):
return sqrt(sin (x))
print(round(ans,8))
2.39628047