我已经花了18个小时,所以我觉得是时候问专家了。我只是开始学习python所以我不能使用索引或任何东西,只是数组和列表。
我的程序已达到某一点,但由于某种原因,我可以在图表上制作的唯一情节是直线,我不知道为什么会这样。
# Program for radioactive isotope graph
from numpy import array, empty
import numpy as np
import math
from math import *
import matplotlib.pyplot as plt
#gathering user data on isotope name, half life, mass and time elapsed.
while True:
isotope = input("\nWhat is the name of your isotope? If you would like to end the program type 'stop'.")
if isotope == "stop":
break
else:
half_life = input("\nWhat is the half life of your isotope (in seconds)?")
mass = input("\nWhat is the amount of isotope you have (in grams)?")
time = input("\nHow long has time elapsed (in seconds)?")
#float user integers
half_life = float (half_life)
mass = float (mass)
time = float (time)
#Calculates amount of isotope remaining.
time_life = time/half_life
P_over_PO = 0.5**time_life
isotope_remain = P_over_PO*mass
print "\nThe amount of", isotope, "that remains after the elapsed time is:", isotope_remain, "grams."
#Calculates initial mass of isotope so after 30 seconds, 5g are left
half_life_5grams = 30/half_life
P_over_PO_5grams = 0.5**half_life_5grams
initial_mass = 5/P_over_PO_5grams
print "\nThe initial mass of", isotope, "needs to be", initial_mass, "grams to ensure that after a thirty second half life 5 g are left."
#Creating a graph with user data
#Creating variables for elements to be put into arrays
graph_time = 0
user_data = 0
graph_mass = list()
while user_data < 10:
isotope_half_life = mass * exp(-log(2)*(time/half_life))
graph_mass.append(isotope_half_life)
graph_time = graph_time + 1
user_data = user_data + 1
y_cooridinates = array(graph_mass, float)
x=np.linspace(0,10,10)
print "x =", x
y=np.array(y_cooridinates,float)
print "y=", y
plt.plot (x, y)
plt.title ('A graph showing the amount of isotope remaining as a function of elapsed time')
plt.xlabel ("Time/seconds")
plt.ylabel ("Amount of isotope/grams")
plt.show ()
break
我已经尝试创建一个数组,在其中输入我的用户输入,然后使用它来计算同位素半衰期。
任何帮助将非常感激,甚至指向正确的方向。
谢谢。
发布于 2018-09-18 14:03:07
我不确定我是否完全清楚你要做什么。我不知道你为什么使用numpy
数组,因为你可以简单地绘制python数组。使用linspace函数对我来说似乎微不足道。
主要问题是你没有更新循环中的时间。您可能不应该在循环中绘制图形,因为您的程序尚未完成计算所有数据点。
这是我修改过的代码:
graph_time = 0
graph_mass = list()
while graph_time < 1000:
isotope_half_life = mass * exp(-log(2) * (graph_time / half_life))
graph_mass.append(isotope_half_life)
graph_time = graph_time + 1
plt.plot(range(0,graph_time), graph_mass)
plt.title('A graph showing the amount of isotope remaining as a function of elapsed time')
plt.xlabel("Time/seconds")
plt.ylabel("Amount of isotope/grams")
plt.show()
https://stackoverflow.com/questions/-100002672
复制相似问题