我怎样才能找到这个方程的近似解?
How might I find an approximate solution to this equation?
下面的代码是为了找出一个比特币意味着多少
考虑到预期 inflation、持有期和节省的美元金额,为了收支平衡而买入:
#!/usr/bin/env python3
from sympy import Eq, EmptySet, init_printing, symbols, solveset
from sys import float_info
init_printing(use_unicode=True)
x = symbols("x", real=True, positive=True)
years = float(input("How many years, i.e. the time horizon of your investment?\n"))
times = float(input("How many times do you intend to multiply your fiat money with Bitcoin?\n"))
times_yearly = times / years#How much on average Bitcoin will multiply per year
cpi = float(input("What yearly inflation do you expect to happen throughout the period?\n")) / 100
a = float(input("What is the amount of savings that you start with?\n"))
i = 1
while i <10000:
left = a
right = ((times_yearly * x + (a - x)) * (1 - cpi)) * years
eqn = Eq(left, right)
if solveset(eqn) != EmptySet:
print("In order to break even you need to buy ")
print(solveset(eqn))
print(a)
a = a + float_info.min
i = i + 1
当程序输入 years=3
、times=4
、cpi=8
和 a=1000
时,它找不到解决方案。因此,我引入了一个循环,旨在为 a
找到最小大于 1000
的解决方案。可悲的是,无济于事。我怎样才能找到这个方程的近似解? sympy
是完成这项工作的好工具吗?我的代码正确还是我遗漏了什么?
这里的问题是你定义了x
只允许正数(定义x
为符号时的positive=True
)。
但是给定数字的方程式的解是 x
等于 -1913.04
.
我想你必须仔细检查你的等式,否则这是一项糟糕的投资:)
from sympy import solve, init_printing, symbols, solveset, Eq, EmptySet
from sys import float_info
init_printing(use_unicode=True)
x = symbols("x", real=True) # I removed the positive=True statement here
years = float(input("How many years, i.e. the time horizon of your investment?\n"))
times = float(input("How many times do you intend to multiply your fiat money with Bitcoin?\n"))
times_yearly = times / years#How much on average Bitcoin will multiply per year
cpi = float(input("What yearly inflation do you expect to happen throughout the period?\n")) / 100
a = float(input("What is the amount of savings that you start with?\n"))
left = a
right = ((times_yearly * x + (a - x)) * (1 - cpi)) * years
eqn = Eq(left, right)
print('Solution for x: ', solveset(eqn))
Output:
How many years, i.e. the time horizon of your investment?
3
How many times do you intend to multiply your fiat money with Bitcoin?
4
What yearly inflation do you expect to happen throughout the period?
8
What is the amount of savings that you start with?
1000
Solution for x: {-1913.04347826087}
聊天讨论后:
from sympy import solve, init_printing, symbols, solveset, Eq, EmptySet
from sys import float_info
x = symbols("x", real=True)
years = list(range(1,5,1))
multiplier = list(range(1,5,1))
infl = [8/100, 4/100, 2/100]
start_cash = [1000, 10000, 100000]
res=[]
for year in years:
for m in multiplier:
for cpi in infl:
for a in start_cash:
left = a
right = ( x*m + (a-x) ) * ((1-cpi)**year)
eqn = Eq(left, right)
out = solveset(eqn)
if out:
res.append([year,m,cpi,a,out.args[0]])
print(f"{year=} {m=} {cpi=} {a=} --> {out=}")
df = pd.DataFrame(res, columns=['years', 'multiplier', 'inflation', 'startcash', 'result'])
df['result'] = df['result'].astype(int)
output = df.pivot(index=['years','multiplier', 'inflation'], columns='startcash', values='result')
print(output)
startcash 1000 10000 100000
years multiplier inflation
1 2 0.02 20 204 2040
0.04 41 416 4166
0.08 86 869 8695
3 0.02 10 102 1020
0.04 20 208 2083
0.08 43 434 4347
4 0.02 6 68 680
0.04 13 138 1388
0.08 28 289 2898
2 2 0.02 41 412 4123
0.04 85 850 8506
0.08 181 1814 18147
3 0.02 20 206 2061
0.04 42 425 4253
0.08 90 907 9073
4 0.02 13 137 1374
0.04 28 283 2835
0.08 60 604 6049
3 2 0.02 62 624 6248
0.04 130 1302 13028
0.08 284 2842 28421
3 0.02 31 312 3124
0.04 65 651 6514
0.08 142 1421 14210
4 0.02 20 208 2082
0.04 43 434 4342
0.08 94 947 9473
4 2 0.02 84 841 8416
0.04 177 1773 17737
0.08 395 3958 39588
3 0.02 42 420 4208
0.04 88 886 8868
0.08 197 1979 19794
4 0.02 28 280 2805
0.04 59 591 5912
0.08 131 1319 13196
下面的代码是为了找出一个比特币意味着多少 考虑到预期 inflation、持有期和节省的美元金额,为了收支平衡而买入:
#!/usr/bin/env python3
from sympy import Eq, EmptySet, init_printing, symbols, solveset
from sys import float_info
init_printing(use_unicode=True)
x = symbols("x", real=True, positive=True)
years = float(input("How many years, i.e. the time horizon of your investment?\n"))
times = float(input("How many times do you intend to multiply your fiat money with Bitcoin?\n"))
times_yearly = times / years#How much on average Bitcoin will multiply per year
cpi = float(input("What yearly inflation do you expect to happen throughout the period?\n")) / 100
a = float(input("What is the amount of savings that you start with?\n"))
i = 1
while i <10000:
left = a
right = ((times_yearly * x + (a - x)) * (1 - cpi)) * years
eqn = Eq(left, right)
if solveset(eqn) != EmptySet:
print("In order to break even you need to buy ")
print(solveset(eqn))
print(a)
a = a + float_info.min
i = i + 1
当程序输入 years=3
、times=4
、cpi=8
和 a=1000
时,它找不到解决方案。因此,我引入了一个循环,旨在为 a
找到最小大于 1000
的解决方案。可悲的是,无济于事。我怎样才能找到这个方程的近似解? sympy
是完成这项工作的好工具吗?我的代码正确还是我遗漏了什么?
这里的问题是你定义了x
只允许正数(定义x
为符号时的positive=True
)。
但是给定数字的方程式的解是 x
等于 -1913.04
.
我想你必须仔细检查你的等式,否则这是一项糟糕的投资:)
from sympy import solve, init_printing, symbols, solveset, Eq, EmptySet
from sys import float_info
init_printing(use_unicode=True)
x = symbols("x", real=True) # I removed the positive=True statement here
years = float(input("How many years, i.e. the time horizon of your investment?\n"))
times = float(input("How many times do you intend to multiply your fiat money with Bitcoin?\n"))
times_yearly = times / years#How much on average Bitcoin will multiply per year
cpi = float(input("What yearly inflation do you expect to happen throughout the period?\n")) / 100
a = float(input("What is the amount of savings that you start with?\n"))
left = a
right = ((times_yearly * x + (a - x)) * (1 - cpi)) * years
eqn = Eq(left, right)
print('Solution for x: ', solveset(eqn))
Output:
How many years, i.e. the time horizon of your investment?
3
How many times do you intend to multiply your fiat money with Bitcoin?
4
What yearly inflation do you expect to happen throughout the period?
8
What is the amount of savings that you start with?
1000
Solution for x: {-1913.04347826087}
聊天讨论后:
from sympy import solve, init_printing, symbols, solveset, Eq, EmptySet
from sys import float_info
x = symbols("x", real=True)
years = list(range(1,5,1))
multiplier = list(range(1,5,1))
infl = [8/100, 4/100, 2/100]
start_cash = [1000, 10000, 100000]
res=[]
for year in years:
for m in multiplier:
for cpi in infl:
for a in start_cash:
left = a
right = ( x*m + (a-x) ) * ((1-cpi)**year)
eqn = Eq(left, right)
out = solveset(eqn)
if out:
res.append([year,m,cpi,a,out.args[0]])
print(f"{year=} {m=} {cpi=} {a=} --> {out=}")
df = pd.DataFrame(res, columns=['years', 'multiplier', 'inflation', 'startcash', 'result'])
df['result'] = df['result'].astype(int)
output = df.pivot(index=['years','multiplier', 'inflation'], columns='startcash', values='result')
print(output)
startcash 1000 10000 100000
years multiplier inflation
1 2 0.02 20 204 2040
0.04 41 416 4166
0.08 86 869 8695
3 0.02 10 102 1020
0.04 20 208 2083
0.08 43 434 4347
4 0.02 6 68 680
0.04 13 138 1388
0.08 28 289 2898
2 2 0.02 41 412 4123
0.04 85 850 8506
0.08 181 1814 18147
3 0.02 20 206 2061
0.04 42 425 4253
0.08 90 907 9073
4 0.02 13 137 1374
0.04 28 283 2835
0.08 60 604 6049
3 2 0.02 62 624 6248
0.04 130 1302 13028
0.08 284 2842 28421
3 0.02 31 312 3124
0.04 65 651 6514
0.08 142 1421 14210
4 0.02 20 208 2082
0.04 43 434 4342
0.08 94 947 9473
4 2 0.02 84 841 8416
0.04 177 1773 17737
0.08 395 3958 39588
3 0.02 42 420 4208
0.04 88 886 8868
0.08 197 1979 19794
4 0.02 28 280 2805
0.04 59 591 5912
0.08 131 1319 13196