Beginner programmer here:)) I am working on a school project, where the assignment is to find the roots to five functions.
In one of my functions there are two roots, and my code can only find one. Does anyone have any tips or advices? Everything is appreciated!!
PS! (sorry the long code, didn't know what to exclude...) PS!nr.2 (the text and variables are in Norwegian :P)
Just ask if there is something that´s weird;)
def b(x: float):
return -x**2+x+6
def bgraf():
xlim(a1, b1)
ylim(-15, 25)
x = linspace(-5, 10, 1000)
y = b(x)
plot(x, y, 'r')
return
funksjoner = [0, 1, 2, 3, 4]
while response not in funksjoner:
i = int(input(" Vennligst velg en av funksjonene, fra 0 til og med 4. \n"))
response = i
if response in funksjoner:
print("Du har valgt graf nr. ", int(funksjoner[i]))
a1 = float(input("1. punkt: "))
b1 = float(input("2. punkt: "))
z = a1
y = b1
m = a1
n = b1
NP = True
if int(funksjoner[i]) == funksjoner[1]:
while abs(y-z) > 1e-10:
null1 = (z+y)/2
if b(z)*b(null1)>0 and b(y)*b(null1)>0:
NP = False
print('Det finnes ingen nullpunkt i dette intervallet, se graf.')
bgraf()
break
elif b(null1) == 0:
break
elif b(z)*b(null1)>0:
z = null1
else :
y = null1
while abs(n-m) > 1e-10:
null1_2 = (m+n)/2
if b(m)*b(null1_2)>0 and b(n)*b(null1_2)>0:
NP = False
print('Det finnes ingen nullpunkt i dette intervallet, se graf.')
bgraf()
break
elif b(null1_2) == 0:
break
elif b(m)*b(null1_2)>0:
m = null1_2
else :
n = null1_2
if NP :
print('\n Vi har i dette intervallet et nullpunkt når x =', round(((z+y)/2), 1))
if null1 != null1_2:
print('og når x =', round(((m+n)/2), 1))
bgraf()
scatter(null1, 0)
if null1 != null1_2:
scatter(null1_2, 0)
It looks like python is ignoring the second while-loop I placed under the if-statement. Is there another way I could this?
(The reason I put everything under the while-loop, is because I have all the other graphs in the same file, and do not want to include them, only the one I choose after running the code.)
Thanks for your attention!
Aucun commentaire:
Enregistrer un commentaire