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 r
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
2213AMTA1011
Giảng viên : Lê Văn TuấnNhóm thực hiện : Nhóm 8
Trang 5NHIỆM VỤ 3 : Giải hệ PTTT của bài 3.4, 3.6, 3.9 với m =
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 x =
np.arange(8,16,0.4)
y = x*(((1-2*x)**2)**(1/3)) plt.plot(x,y) plt.xlabel("Truc x")
plt.ylabel("Truc y") plt.title('Toan cao cap') plt.show()
Kết quả :
Trang 8Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D x =
Trang 9Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D x =
Trang 10Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D x =
Trang 11Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D x =
Trang 12Nhậ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 =(x+1)*(((x**2)-1)**(1/3)) plt.plot(x,y) plt.xlabel("Truc x")
plt.ylabel("Truc y") plt.title('Toan cao cap') plt.show()
Kết quả :
Trang 13Nhậ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 =x*(((x**2)-1)**(1/8)) plt.plot(x,y) plt.xlabel("Truc x")
plt.ylabel("Truc y") plt.title('Toan cao cap') plt.show()
Kết quả :
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 x =
Trang 15Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D x =
Trang 16Nhập:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D x =
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))
Trang 23return -x*arctan(sqrt(x**2 - y**2))/(x**2 + y**2)**(3/2) +
x/(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 -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))
Trang 28<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 -
(sqrt(x**2 - y**2)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1))
Trang 29Giải thích : Lỗi do tập xác định vì => x>y mà x<y (8<16)
Trang 30Optimization terminated successfully
Current function value: 0.000000 Iterations: 15
Function evaluations: 30[0.5]
Trang 31Kết quả :
Optimization terminated successfully
Current function value: -0.250000 Iterations: 18
Function evaluations: 36[-0.5]
from scipy.optimize import fmin
def f(x): return x/math.exp(x)
Trang 32Nhập: import math
from scipy.optimize import fmin
def f(x): return x*(math.log(x))**2
min_loc = fmin(f, 1.0)
print(min_loc)
print(f(*min_loc))
Kết quả :
Optimization terminated successfully
Current function value: 0.000000 Iterations: 10
Function evaluations: 20[1.]
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 Iterations: 17
Function evaluations: 34[-8.8817842e-16]
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
Trang 34Iterations: 16Function evaluations: 32[0.33330078]
Optimization terminated successfully
Current function value: -0.128300 Iterations: 15
Function evaluations: 30[0.57734375]
-0.12830005979537965
Ghi chú Hàm số đạt min tại x = 0.57734375 Giá
trị min: -0.12830005979537965
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 Iterations: 10
Function evaluations: 20[1.]
0.36787944117144233
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)
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
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],
vec[1]), [0.1, 0.1])
print(min_loc)
print(f(*min_loc))
Trang 40Kết quả :
Optimization terminated successfully
Current function value: 0.000000 Iterations: 39
Function evaluations: 76[-1.00001244 1.00003511]
from scipy.optimize import fmin
def f(x,y): return 2*x**4+y**4-x**2-2*y**2
min_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
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 45import sympy
x, y = sympy.symbols ('x y', real = True)
Trang 46z xy sympy diff(z x y)
Trang 49<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)*sqrt(x**2 + y**2)*(x**2 - y**2 + 1))
28