#๐ Help with Next assignment!
34 messages ยท Page 1 of 1 (latest)
@naive mulch
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt
def make_random_array(scale, size):
result = np.zeros(size)
for i in range(0,size):
result[i] = rd.exponential(scale=scale)
return result
def scale_from_halflife(halflife):
return halflife/np.log(2)
def part_c():
return make_random_array(scale_from_halflife(10), 10)
def part_d(random_array):
sorted_array = np.sort(random_array)
index_of_first_greater_than_5 = 0
for i in range(0, sorted_array.size):
if sorted_array[i] > 5:
index_of_first_greater_than_5 = i
break
return index_of_first_greater_than_5
def part_e():
result = np.zeros(10000)
for i in range(10000):
rand_array = part_c()
result[i] = part_d(rand_array)
return result
def part_f():
result = part_e()
plt.hist(result)
plt.xlabel('Number of decays in 5 days')
plt.ylabel('Count')
plt.title('Histogram of number of decayed atoms at 5 days')
plt.savefig("Histogram_Decays_Count.png")
def part_g():
result = part_e()
plt.hist(result, density=True)
plt.xlabel('Density of decays in 5 days')
plt.ylabel('Density')
plt.title('Density Histogram of number of decayed atoms at 5 days')
plt.savefig("Density_Histogram_Decays_Count.png")
def part_h():
result_part_e = part_e()
histogram, _ = np.histogram(result_part_e, density=True)
result = 0
for i in range(len(histogram)):
result = result + i*histogram[i]
return result
Hey @naive mulch!
It looks like you're trying to paste code into this channel.
Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
def backup_main():
sizes_to_test = [100,1000,10000,1000000]
for size in sizes_to_test:
scale = scale_from_halflife(10)
randarray = make_random_array(scale, size)
plt.hist(randarray,bins=100)
plt.savefig("Histogram_"+str(size)+".png")
print("Done! \n")
if name == "main":
result_part_c = part_c()
f = open("result_part_c.txt","w")
f.write(str(result_part_c))
f.close()
result_part_d = part_d(result_part_c)
f = open("result_part_d.txt","w")
f.write(str(result_part_d))
f.close()
result_part_e = part_e()
f = open("result_part_e.txt", "w")
f.write("")
f.close()
f = open("result_part_e.txt", "a")
for i in range(0,result_part_e.size):
f.write(str(result_part_e[i]) + ", ")
f.close()
result_part_h = part_h()
f = open("result_part_h.txt","w")
f.write(str(result_part_h))
f.close()
The likelihood to decay in 5 days is the cumilative distribution at 5.
We can calculate f(5). Hence f(5)=1-e^(-lamda*5)
Lamda = ln(2)/half-life
Since we have 10 atoms, we can multiply f(5)=1-e^(-lamda*5) by 10.
The value received is approximately three, which alligns with part h.
part_f()
part_g()
prior code ^^
could you please use syntax highlighting in your pastes
wait that might be a tad bit small
im having trouble with it as my code is way too long
``import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt
def make_random_array(scale, size):
result = np.zeros(size)
for i in range(0,size):
result[i] = rd.exponential(scale=scale)
return result
def scale_from_halflife(halflife):
return halflife/np.log(2)
def part_c():
return make_random_array(scale_from_halflife(10), 10)
def part_d(random_array):
sorted_array = np.sort(random_array)
index_of_first_greater_than_5 = 0
for i in range(0, sorted_array.size):
if sorted_array[i] > 5:
index_of_first_greater_than_5 = i
break
return index_of_first_greater_than_5
def part_e():
result = np.zeros(10000)
for i in range(10000):
rand_array = part_c()
result[i] = part_d(rand_array)
return result
def part_f():
result = part_e()
plt.hist(result)
plt.xlabel('Number of decays in 5 days')
plt.ylabel('Count')
plt.title('Histogram of number of decayed atoms at 5 days')
plt.savefig("Histogram_Decays_Count.png")
def part_g():
result = part_e()
plt.hist(result, density=True)
plt.xlabel('Density of decays in 5 days')
plt.ylabel('Density')
plt.title('Density Histogram of number of decayed atoms at 5 days')
plt.savefig("Density_Histogram_Decays_Count.png")
def part_h():
result_part_e = part_e()
histogram, _ = np.histogram(result_part_e, density=True)
result = 0
for i in range(len(histogram)):
result = result + i*histogram[i]
return result``
``def backup_main():
sizes_to_test = [100,1000,10000,1000000]
for size in sizes_to_test:
scale = scale_from_halflife(10)
randarray = make_random_array(scale, size)
plt.hist(randarray,bins=100)
plt.savefig("Histogram_"+str(size)+".png")
print("Done! \n")
if name == "main":
result_part_c = part_c()
f = open("result_part_c.txt","w")
f.write(str(result_part_c))
f.close()
result_part_d = part_d(result_part_c)
f = open("result_part_d.txt","w")
f.write(str(result_part_d))
f.close()
result_part_e = part_e()
f = open("result_part_e.txt", "w")
f.write("")
f.close()
f = open("result_part_e.txt", "a")
for i in range(0,result_part_e.size):
f.write(str(result_part_e[i]) + ", ")
f.close()
result_part_h = part_h()
f = open("result_part_h.txt","w")
f.write(str(result_part_h))
f.close()
The likelihood to decay in 5 days is the cumilative distribution at 5.
We can calculate f(5). Hence f(5)=1-e^(-lamda*5)
Lamda = ln(2)/half-life
Since we have 10 atoms, we can multiply f(5)=1-e^(-lamda*5) by 10.
The value received is approximately three, which alligns with part h.
part_f()
part_g()
``
def backupmain():
sizes_to_test = [100,1000,10000,1000000]
for size in sizes_to_test:
scale = scale_from_halflife(10)
randarray = make_random_array(scale, size)
plt.hist(randarray,bins=100)
plt.savefig("Histogram"+str(size)+".png")
print("Done! \n")
if name == "main":
result_part_c = part_c()
f = open("result_part_c.txt","w")
f.write(str(result_part_c))
f.close()
result_part_d = part_d(result_part_c)
f = open("result_part_d.txt","w")
f.write(str(result_part_d))
f.close()
result_part_e = part_e()
f = open("result_part_e.txt", "w")
f.write("")
f.close()
f = open("result_part_e.txt", "a")
for i in range(0,result_part_e.size):
f.write(str(result_part_e[i]) + ", ")
f.close()
result_part_h = part_h()
f = open("result_part_h.txt","w")
f.write(str(result_part_h))
f.close()```
wait lol how did you do that
!code
it doesnt let me submit files on here
if its too long then use !paste
!paste
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
ohh files, yeah u cant do that
lol can i send a screenshot
if its readable
Okay, so i have an assignment - this was my last assingment but i need to continue it. Let me send my last assignment
this is my new assignment
wait i think its supposed to be hw1, i sent the wrong code. mb. give me second
what should i do first?
Hi, let me know if im confusing you guys
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.