如何简化代码?

huangapple go评论66阅读模式
英文:

How can i simplify the codes?

问题

我绘制了一些基于代码的图表。但我认为这太复杂了。我该如何简化odeint、array和print代码呢?我是否需要使用forin代码?尽管我已经尝试使用forin代码来简化代码,但我无法完全理解在这种情况下如何使用forin代码。

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
import math

def dy_dx(y, x, z):
    c_1 = 5.0 / (1.38 * 1223.0 * pow(10.0, 28.0) * pow(z, 3.0))
    c_2 = 600000
    c_3 = 9.5 * pow(10, 19)
    c_4 = 9.5 * pow(10, 10)
    c_5 = pow(600000, 2.7)
    return (c_1 * (y / math.sqrt(1.0 + pow(y, 2.0))) * (((1.0 - pow(y, 3.0)) / (z * pow(y, (1.0 / 3.0)))) - (y * c_2))) - (
                ((3 * y * (c_3) * np.exp(-36 * 1570 / 1223)) / ((2 * pow(c_4, 2.7))) * (c_5)))

xs = np.linspace(0, pow(10.0, 12.0), pow(10.0, 6.0))
y_0 = 1.0
z_values = [0.000001, 0.000002, 0.000003, 0.000004, 0.000005, 0.000006, 0.000007, 0.000008, 0.000009, 0.00001,
            0.00002, 0.00003, 0.00004, 0.00005, 0.00006, 0.00007, 0.00008, 0.00009, 0.0001, 0.00011]

y_values = []

for z in z_values:
    y = odeint(dy_dx, y_0, xs, args=(z,))
    y = np.array(y).flatten()
    y_values.append(y)
    print(np.interp(pow(8, 12), xs, y))

plt.rcParams.update({'font.size': 10})
plt.ylim(0, 1.0)
plt.xlabel("x")
plt.ylabel("y")

for i, y in enumerate(y_values):
    plt.plot(xs, y, label=f"z = {z_values[i]}")

plt.legend()
plt.grid(True)
plt.show()

我已经对你的代码进行了一些简化和重构,使用了循环来避免重复的代码块。希望这对你有所帮助。

英文:

I plotted the some graph based on the codes. But I think this is too complicate. How could I simplify the odeint, array and print codes. Do i need to use for, in codes? Although I already tried to use for, in codes to simplify the codes, I couldn't exactly understand how could use for, in codes in this case.

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
import math

def dy_dx(y,x,z):
    c_1 = 5.0 / (1.38 * 1223.0 * pow(10.0, 28.0)*pow(z,3.0))
    c_2 = 600000
    c_3 = 9.5*pow(10,19)
    c_4 = 9.5*pow(10,10)
    c_5 = pow(600000,2.7)
    return (c_1 * (y/math.sqrt(1.0+pow(y, 2.0)) ) * (((1.0-pow(y, 3.0))/(z* pow(y,(1.0/3.0)))) - (y * c_2)))-(((3*y*(c_3)*np.exp(-36*1570/1223)))/((2*pow(c_4,2.7)))*(c_5))

xs = np.linspace(0, pow(10.0, 12.0), pow(10.0, 6.0))
y_0 = 1.0
z = 0.000001
y1 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.000002
y2 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.000003
y3 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.000004
y4 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.000005
y5 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.000006
y6 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.000007
y7 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.000008
y8 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.000009
y9 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.00001
y10 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.00002
y11 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.00003
y12 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.00004
y13 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.00005
y14 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.00006
y15 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.00007
y16 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.00008
y17 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.00009
y18 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.0001
y19 = odeint(dy_dx, y_0, xs, args=(z,))
z = 0.00011
y20 = odeint(dy_dx, y_0, xs, args=(z,))


y1 = np.array(y1).flatten()
y2 = np.array(y2).flatten()
y3 = np.array(y3).flatten()
y4 = np.array(y4).flatten()
y5 = np.array(y5).flatten()
y6 = np.array(y6).flatten()
y7 = np.array(y7).flatten()
y8 = np.array(y8).flatten()
y9 = np.array(y9).flatten()
y10 = np.array(y10).flatten()
y11 = np.array(y11).flatten()
y12 = np.array(y12).flatten()
y13 = np.array(y13).flatten()
y14 = np.array(y14).flatten()
y15 = np.array(y15).flatten()
y16 = np.array(y16).flatten()
y17 = np.array(y17).flatten()
y18 = np.array(y18).flatten()
y19 = np.array(y19).flatten()
y20 = np.array(y20).flatten()

print(np.interp(pow(8,12),xs,y1))
print(np.interp(pow(8,12),xs,y2))
print(np.interp(pow(8,12),xs,y3))
print(np.interp(pow(8,12),xs,y4))
print(np.interp(pow(8,12),xs,y5))
print(np.interp(pow(8,12),xs,y6))
print(np.interp(pow(8,12),xs,y7))
print(np.interp(pow(8,12),xs,y8))
print(np.interp(pow(8,12),xs,y9))
print(np.interp(pow(8,12),xs,y10))
print(np.interp(pow(8,12),xs,y11))
print(np.interp(pow(8,12),xs,y12))
print(np.interp(pow(8,12),xs,y13))
print(np.interp(pow(8,12),xs,y14))
print(np.interp(pow(8,12),xs,y15))
print(np.interp(pow(8,12),xs,y16))
print(np.interp(pow(8,12),xs,y17))
print(np.interp(pow(8,12),xs,y18))
print(np.interp(pow(8,12),xs,y19))
print(np.interp(pow(8,12),xs,y20))




plt.rcParams.update({'font.size': 10})
plt.ylim(0,1.0)
plt.xlabel("x")
plt.ylabel("y")
plt.plot(xs, y1, 'r-')
plt.plot(xs, y2, 'b-')
plt.plot(xs, y3, 'g-')
plt.grid(True)
plt.show()

答案1

得分: 1

以下是代码部分的翻译:

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
import math

def dy_dx(y,x,z):
    c_1 = 5.0 / (1.38 * 1223.0 * pow(10.0, 28.0)*pow(z,3.0))
    c_2 = 600000
    c_3 = 9.5*pow(10,19)
    c_4 = 9.5*pow(10,10)
    c_5 = pow(600000,2.7)
    return (c_1 * (y/math.sqrt(1.0+pow(y, 2.0)) ) * (((1.0-pow(y, 3.0))/(z* pow(y,(1.0/3.0)))) - (y * c_2)))-(((3*y*(c_3)*np.exp(-36*1570/1223)))/((2*pow(c_4,2.7)))*(c_5))

xs = np.linspace(0, pow(10, 12), pow(10, 6))
y_0 = 1.0

zVals = [0.000001, 0.000002, 0.000003, 0.000004, 0.000005, 0.000006, 0.000007, 0.000008, 0.000009, 0.00001, 0.00002, 0.00003, 0.00004, 0.00005, 0.00006, 0.00007, 0.00008, 0.00009, 0.0001, 0.00011]

yDict = {}

for i in range(1,21):
    z = zVals[i-1]
    yDict["y{0}".format(i)] = odeint(dy_dx, y_0, xs, args=(z,))

for y in yDict:
    yDict[y] = np.array(yDict[y]).flatten()
    print(np.interp(pow(8,12),xs,yDict[y]))

plt.rcParams.update({'font.size': 10})
plt.ylim(0,1.0)
plt.xlabel("x")
plt.ylabel("y")
plt.plot(xs, yDict['y1'], 'r-')
plt.plot(xs, yDict['y2'], 'b-')
plt.plot(xs, yDict['y3'], 'g-')
plt.grid(True)
plt.show()

希望这能帮助你理解代码的内容。如果你有任何问题,请随时提问。

英文:

While your code is quite long and does take some time to read through it is pretty straightforward. It can be made simpler by using a dictionary to keep track of your y values and a few for loops for the repetitive lines as shown below -

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
import math

def dy_dx(y,x,z):
    c_1 = 5.0 / (1.38 * 1223.0 * pow(10.0, 28.0)*pow(z,3.0))
    c_2 = 600000
    c_3 = 9.5*pow(10,19)
    c_4 = 9.5*pow(10,10)
    c_5 = pow(600000,2.7)
    return (c_1 * (y/math.sqrt(1.0+pow(y, 2.0)) ) * (((1.0-pow(y, 3.0))/(z* pow(y,(1.0/3.0)))) - (y * c_2)))-(((3*y*(c_3)*np.exp(-36*1570/1223)))/((2*pow(c_4,2.7)))*(c_5))

xs = np.linspace(0, pow(10, 12), pow(10, 6))
y_0 = 1.0


zVals = [0.000001, 0.000002, 0.000003, 0.000004, 0.000005, 0.000006, 0.000007, 0.000008, 0.000009, 0.00001, 0.00002, 0.00003, 0.00004, 0.00005, 0.00006, 0.00007, 0.00008, 0.00009, 0.0001, 0.00011]

yDict = {}

for i in range(1,21):
	z = zVals[i-1]
	yDict["y{0}".format(i)] = odeint(dy_dx, y_0, xs, args=(z,))

for y in yDict:
	yDict[y] = np.array(yDict[y]).flatten()
	print(np.interp(pow(8,12),xs,yDict[y]))

plt.rcParams.update({'font.size': 10})
plt.ylim(0,1.0)
plt.xlabel("x")
plt.ylabel("y")
plt.plot(xs, yDict['y1'], 'r-')
plt.plot(xs, yDict['y2'], 'b-')
plt.plot(xs, yDict['y3'], 'g-')
plt.grid(True)
plt.show()

huangapple
  • 本文由 发表于 2020年1月3日 15:59:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定