from sympy import x1 x2 symbols x1 x2 def profit_max p1 float input Вв

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from sympy import *
x1, x2, y, z, p = symbols('x1 x2 y z p')
def profit_max():
p1 = float(input("Введите цену товара 1: "))
p2 = float(input("Введите цену товара 2: "))
m = float(input("Введи доход (m): "))
utility = sympify(input("Введите функцию полезности x1, и x2: ")) # utility function U(x1,x2)
print("Utility: U(x1,x2)=",utility)
print()
Lagrange = utility + y*(m-(p1*x1)-(p2*x2))
print("Функция Лагранжа для максимальной полезности: ")
print("L =", simplify(Lagrange))
print()
x1_foc = diff(Lagrange, x1) # solves the foc with respect to x1
x2_foc = diff(Lagrange, x2) # solves the foc with respect to x2
y_foc = diff(Lagrange, y) # solves the foc with respect to y (lambda)
###
#
# prints the Foc's, and x1,x2 solved respectively for y (lambda)
#
###
print("Систему линейных уравнений ")
print("--------------")
print()
print("[x1]: ",simplify(x1_foc))
print('[x2]: ',expand(x2_foc))
print('[y]: ',cancel(y_foc))
print()
a = solve(x1_foc, y) # Solves x1's foc for lambda(y)
b = solve(x2_foc, y) # Solves x2's foc for lambda(y)
print("Лямбда для x1: ",a)
print("Лямбда для x2: ",b)
print()
mrs = (p1/p2)
print("Отношение 1 товара к 2 = ",mrs)
x1_star = (solve(Eq(a[0],b[0]), x1))[0]
if type(x1_star) != float:
x2_star = (solve(m-(p1*x1_star)-(p2*x2),x2))[0]
x1_star = x1_star.subs(x2,x2_star)
else:
x2_star = (solve(m-(p1*x1_star)-(p2*x2),x2))[0]
print("x1* =",x1_star) # 4 * x2
#x2_star = (solve(m-(p1*x1_star)-(p2*x2),x2))[0]
print("x2*=",x2_star) # 2
print("кривая безразличия",utility)
#x2_star = x2_star.subs(x1,x1_star)
#x1_star = x1_star.subs(x2,x2_star)
#print("x2*=",x2_star)
print()
profit_max()