다른 연습. 이 사람은 x-2 = ln (x)에 대한 x 값을 평가할 것을 요청했습니다. 두 가지 접근법 (A와 B)이 있습니다 - 주어진 방정식을 사용하고 더 작은 해를 산출합니다 (x1). 다른 접근법은 e ** (x-2) = x를 사용하고 다른 해 (x2)를 산출합니다.결과 인쇄시 소수 자릿수 적용
프로그램은 그래픽 솔루션을 플롯 한 다음 초기 값 입력을 쿼리합니다. 그런 다음 적절한 접근법을 사용하여 x를 평가합니다. 접근법 A는 초기 조건이 x2보다 작아야하며, 접근법 B는 초기 조건이 x1보다 커야합니다. 두 방법 모두 x1과 x2 사이에있는 초기 조건에서 작동합니다.
코드의 마지막 부분은 고유 솔루션 만 인쇄하도록 출력을 조작합니다.
# imports necessary modules
import matplotlib.pyplot as plt
import numpy as np
# plots the equation to provide insight into possible solutions
p = []
x = np.arange(0,5,0.01)
f = x - 2
g = np.log(x)
plt.plot(x,f)
plt.plot(x,g)
plt.show()
# x - 2 = ln(x)
print
lista = map(float, raw_input("Provide the starting conditions to establish the approximate value of the solutions to x-2=ln(x) (separate with spacebar): ").split(" "))
print
sigdig = int(raw_input("Define the number of significant digits (up to 15): "))
print
results1 = []
results2 = []
results3 = []
results4 = []
results = []
resu = []
for i in lista:
if i > 0.1586:
y = i
left = y - 2
right = np.log(y)
expo = "%d" % sigdig
epsi = 10**(-int(expo)-1)
step = 0
while abs(left - right) > epsi:
y = right + 2
right = np.log(y)
left = y - 2
step += 1
results1.append(y)
results2.append(results1[-1])
if i < 3.1462:
z = i
left = np.e ** (z - 2)
right = z
expo = "%d" % sigdig
epsi = 10**(-int(expo)-1)
step = 0
while abs(left - right) > epsi:
z = np.e ** (right - 2)
left = np.e ** (z - 2)
right = z
step += 1
results3.append(z)
results4.append(results3[-1])
# combines and evaluates the results
results = results2 + results4
for i in range(len(results)):
if round(results[i], sigdig) not in resu:
resu.append(round(results[i], sigdig))
else:
pass
print "For given starting conditions following solutions were found:"
print
for i in range(len(resu)):
printer = '"x_%d = %.' + str(sigdig) + 'f" % (i+1, resu[i])'
print eval(printer)
제 질문
은 : 그것은 그 부호화 그래픽 용액 대신 하드 X1 및 X2 (라인 36, 53)의 대략적인 값을 공급할 수있다? 코드에 eval 해결 방법이없는 소수 자릿수를 적용 할 수 있습니까? resu [i]를 인쇄하면 일반적으로 가장 가까운 10 진수 "0"(코드 끝 부분 근처)에서 끝나는 결과가 생성됩니다. 감사.
당신이 시행하고자하는 번호는 무엇이든 :'round (n, sigfig)'. –
문제는 이미 코딩했는데 작동하지 않는다는 것입니다. 가장 가까운 십진수 0 바로 앞에서 자릅니다. – user2126752
이것은 두 가지 질문이어야합니다. 당신은 전혀 관련이없는 두 가지 다른 것을 요구하고 있습니다. –