File "D:\toandaicuong\main.py", line 4, in x = np.linalg.solvea,b File "", line 180, in solve File "D:\toandaicuong\venv\lib\site- packages\numpy\linalg\linalg.py", line 393, in solve
Trang 1TRƯỜNG ĐẠI HỌC THƯƠNG MẠI
KHOA HTTTKT & TMĐT
BÀI THẢO LUẬN HỌC PHẦN: TOÁN CAO CẤP NHIỆM VỤ: SỬ DỤNG PYTHON GIẢI CÁC BÀI TOÁN
Lớp học phần : 2213AMTA1011
Giảng viên : Lê Văn TuấnNhóm thực hiện : Nhóm 8
1
Trang 3[72]
[31]]
B Tính định thức, ma trận nghịch đảo của các ma trận bài 1.8 (với m= 8) :
Trang 4Kết quả: 3
Hạng của ma trận = 3
B Tìm hạng và xét tính độc lập tuyến tính, phụ thuộc tuyến tính của các hệ vectơ trong bài 2.4
print(np.linalg.matrix_rank(C))
Kết quả: 4
Hạng ma trận = 4
Vì hạng ma trận = 4 = số véc tơ (4) => Hệ độc lập tuyến tính
Trang 5Nhập:
import numpy as np
D = np.array([[1, 0, -1, 2, -2],[2, 1, 0, 3, -5],[5, 2, -1, 8, 8-64],[-3,-1,1,-5,8+3]])
Trang 6 Hệ có nghiệm [ 0.46666667 -0.00833333 0.525 0.375 ]
Trang 7File "D:\toandaicuong\main.py", line 4, in <module>
x = np.linalg.solve(a,b)
File "< array_function internals>", line 180, in solve File "D:\toandaicuong\venv\lib\site-
packages\numpy\linalg\linalg.py", line 393, in solve
r = gufunc(a, b, signature=signature, extobj=extobj) File "D:\toandaicuong\venv\lib\site-
packages\numpy\linalg\linalg.py", line 88, in
_raise_linalgerror_singular
raise LinAlgError("Singular matrix")
numpy.linalg.LinAlgError: Singular matrix
Hệ vô nghiệm hoặc vô số nghiệm
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Trang 8Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Trang 9Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Trang 10Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Trang 11Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Trang 12Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Trang 13Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Trang 14B Vẽ đồ thị các hàm số trong bài 7.3 trên miền x [8,16]; y [1,8]
7.3.1
Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Trang 15Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Trang 16Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = np.arange(8,16,0.4)
y = np.arange(1,8,0.4)
X, Y = np.meshgrid(x, y)
z = np.log((X*Y)+(((X**2)*(Y**2))**(1/3)))fig = plt.figure()
Trang 21z_y = sympy lambdify((x, y), z_y)
x, y = sympy.symbols ('x y', real = True)
z = sympy.log(x + sympy.sqrt(x**2 + y**2))z_x = sympy.diff(z, x)
z_x = sympy lambdify((x, y), z_x)
Trang 23<lambdifygenerated-2>:2: RuntimeWarning: invalid value encountered in sqrt
return -y*arctan(sqrt(x**2 - y**2))/(x**2 + y**2)**(3/2)
- y/(sqrt(x**2 - y**2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1))
Trang 25Nhập :
import sympy
x, y = sympy.symbols ('x y', real = True)
z = sympy.log(x + sympy.sqrt(x**2 + y**2))z_x = sympy.diff(z, x)
Trang 28y**2)*(x**2 + y**2)**(3/2)*(x**2 - y**2 + 1)) - x**2/((x**2 - y**2)**(3/2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1)) - arctan(sqrt(x**2 - y**2))/(x**2 + y**2)**(3/2) + 1/(sqrt(x**2 - y**2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1))
<lambdifygenerated-2>:2: RuntimeWarning: invalid value encountered in sqrt
return 3*x*y*arctan(sqrt(x**2 - y**2))/(x**2 +
y**2)**(5/2) + 2*x*y/(sqrt(x**2 - y**2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1)**2) + x*y/((x**2 -
y**2)**(3/2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1))
<lambdifygenerated-3>:2: RuntimeWarning: invalid value encountered in sqrt
return 3*y**2*arctan(sqrt(x**2 - y**2))/(x**2 +
y**2)**(5/2) - 2*y**2/(sqrt(x**2 - y**2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1)**2) + 2*y**2/(sqrt(x**2 -
y**2)*(x**2 + y**2)**(3/2)*(x**2 - y**2 + 1)) - y**2/((x**2 - y**2)**(3/2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1)) - arctan(sqrt(x**2 - y**2))/(x**2 + y**2)**(3/2) - 1/(sqrt(x**2 - y**2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1))
28
Trang 29Giải thích : Lỗi do tập xác định vì => x>y mà x<y (8<16) => không thỏa mãn
Trang 30Function evaluations: 30
[0.5]
1.3147681753683518e-31
Ghi chú Hàm số đạt min tại x = 0.5
Giá trị min: 1.3147681753683518e-31
Trang 31Kết quả :
Optimization terminated successfully
Current function value: -0.250000 Iterations: 18
from scipy.optimize import fmin
def f(x): return x/math.exp(x)
Trang 326.10.4,
Nhập: import math
from scipy.optimize import fmin
def f(x): return x*(math.log(x))**2
from scipy.optimize import fmin
def f(x): return math.exp(x)/(x+1)
32
Trang 33min_loc = fmin(f, 1.0)
print(min_loc)
print(f(*min_loc))
Kết quả :
Optimization terminated successfully
Current function value: 1.000000
from scipy.optimize import fmin
def f(x): return (x+1)*((math.pow(x,2)-1)**1/3)min_loc = fmin(f, 1.0)
print(min_loc)
print(f(*min_loc))
Kết quả :
Optimization terminated successfully
Current function value: -0.395062
33
Trang 34Optimization terminated successfully.
Current function value: -0.128300
Trang 35Ghi chú Giá trị max vượt quá giá trị lớn nhất của
Python (hàm số tiến ra vô cùng)
Trang 36Ghi chú Giá trị max vượt quá giá trị lớn nhất của
Python (hàm số tiến ra vô cùng)
6.10.3,
Nhập: import math
from scipy.optimize import fmin
def f(x): return -(x/math.e**x)
min_loc = fmin(f, 1.0)
print(min_loc)
print(-f(*min_loc))
Kết quả :
Optimization terminated successfully
Current function value: -0.367879
Trang 37Ghi chú Hàm số đạt max tại x = 1
Giá trị max : 0.36787944117144233
6.10.4,
Nhập: import math
from scipy.optimize import fmin
def f(x): return -(x*math.log(x)**2)
Ghi chú Giá trị max vượt quá giá trị lớn nhất của
Python (hàm số tiến ra vô cùng)
6.10.5,
Nhập: import math
from scipy.optimize import fmin
def f(x): return -(math.exp(x)/(x+1))
min_loc = fmin(f, 1.0)
37
Trang 38from scipy.optimize import fmin
def f(x): return -((x+1)*((math.pow(x,2)-1)**1/3))
Ghi chú Giá trị max vượt quá giá trị lớn nhất của
Python (hàm số tiến ra vô cùng)
6.10.7,
Nhập: import math
38
Trang 39from scipy.optimize import fmin
Ghi chú Giá trị max vượt quá giá trị lớn nhất của
Python (hàm số tiến ra vô cùng)
7.9
* Tìm MIN
7.9.1,
Nhập: import math
from scipy.optimize import fmin
def f(x,y): return x**2 + x*y +y**2 +x-y+1
min_loc = fmin(lambda vec: f(vec[0],
Trang 40Kết quả :
Optimization terminated successfully
Current function value: 0.000000
Giá trị min: 9.504592668463374e-10
7.9.2,
Nhập: import math
from scipy.optimize import fmin
def f(x,y): return 2*x**4+y**4-x**2-2*y**2min_loc = fmin(lambda vec: f(vec[0],
vec[1]), [0.1, 0.1])
print(min_loc)
print(f(*min_loc))
Kết quả :
Optimization terminated successfully
Current function value: -1.125000 Iterations: 46
Function evaluations: 89
40
Trang 42Nhập :
import sympy
x, y = sympy.symbols ('x y', real = True)
z = sympy.log(x + sympy.sqrt(x**2 + y**2)) z_x = sympy.diff(z, x)
Trang 47y**2)*(x**2 + y**2)**(3/2)*(x**2 - y**2 + 1)) - x**2/ ((x**2 - y**2)**(3/2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1)) - arctan(sqrt(x**2 - y**2))/(x**2 + y**2)**(3/2) + 1/ (sqrt(x**2 - y**2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1))
<lambdifygenerated 2>:2: RuntimeWarning: invalid
Trang 48<lambdifygenerated-2>:2: RuntimeWarning: invalid value encountered in sqrt
return 3*x*y*arctan(sqrt(x**2 - y**2))/(x**2 +
y**2)**(5/2) + 2*x*y/(sqrt(x**2 - y**2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1)**2) + x*y/((x**2 -
y**2)**(3/2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1))
<lambdifygenerated-3>:2: RuntimeWarning: invalid value encountered in sqrt
return 3*y**2*arctan(sqrt(x**2 - y**2))/(x**2 +
y**2)**(5/2) - 2*y**2/(sqrt(x**2 - y**2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1)**2) + 2*y**2/(sqrt(x**2 -
y**2)*(x**2 + y**2)**(3/2)*(x**2 - y**2 + 1)) - y**2/ ((x**2 - y**2)**(3/2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1)) - arctan(sqrt(x**2 - y**2))/(x**2 + y**2)**(3/2) - 1/ (sqrt(x**2 - y**2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1))
28