2

A different exercise. This one asked to evaluate the value of x for x-2=ln(x). There are two approaches (A and B) - one makes use of the given equation and yields the smaller solution (x1). The other approach uses e**(x-2)=x and yields the other solution (x2).

Program plots the graphical solution and then queries for initial value input. Then it evaluates x using appropriate approaches. Approach A requires initial condition lesser than x2, approach B requires initial condition greater than x1. Both approaches work for initial conditions that lay somewhere between x1 and x2.

The last part of the code manipulates the output to print only unique solutions.

# 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)

My questions are: is it possible to feed the approximate values of x1 and x2 (lines 36 and 53) from the graphical solution instead of hard coding them? Is possible to enforce the number of decimals without the eval work-around present in the code? Printing resu[i] normally yields results that end before nearest decimal "0" (near the end of code). Thanks.

4

2 回答 2

1

您可以*以与 C/C++ 类似的方式在 python 格式字符串中使用。当%运算符在格式字符串中遇到 a*时,它会在列表中查找整数并将其用作格式字符串中的常量。

以下应该适用于您正在尝试做的事情:

print "x_%d = %.*f" % (sigdig,sigdig,resu[i])

如果resultis1.23456sigdigis 3,这将输出:

x_3 = 1.234

请注意,python 浮点数是IEEE794 双精度值,这意​​味着它们有大约 15 个有效数字。例如:

>>> print "x_%d = %.*f" % (30,30,1.0/3.0)
x_30 = 0.333333333333333314829616256247

请注意,小数点后 17 位以外的所有内容本质上都是随机垃圾。

于 2013-03-02T17:44:35.177 回答
0

我认为您的问题https://stackoverflow.com/questions/15709496(现已关闭)非常有趣。很遗憾看到它关闭了,所以我将继续作为练习,并将其发布在以下博客上:http: //nickburns2013.wordpress.com/2013/04/01/3d-water-model /。随意评论/添加点点滴滴。

这与这里的这个问题完全无关。向任何关注此线程的人道歉。

于 2013-04-01T02:19:11.417 回答