Dobrý den, potřeboval bych poradit, jak naprogramovat hledání kořenů půlením intervalu a metodou tečen? Děkuji za odpovědi.
Fórum › Python
Python- půlení intervalů

No, poznáš, jestli se blížíš správnému výsledku. Takže se mu dokolečka (cyklus) budeš snažit přibližovat, dokud ho nenajdeš. V pseudokódu úplně jednoduše třeba takhle:
vysledek = None
zkusebni_hodnota = vychozi_hodnota
while True:
vysledek = vypocet(zkusebni_hodnota)
if vysledek vyhovuje:
break
elif vysledek napravo:
zkusebni_hodnota = posunout doleva
else:
zkusebni_hodnota = posunout doprava
vypsat vysledek
Jako přímo ten rozsah? Normálně do nějakých proměnných. Třeba a,b. Uprostřed bude c.
def znamenko(x):
return -1 if x < 0 else 1
def funkce(x):
return (x*x*x)-x-1
a = 1
b = 2
maxchyba = 0.001
while True:
c = (a + b) / 2
chyba = (b - a) / 2
fa = funkce(a)
fc = funkce(c)
fb = funkce(b)
print('{: .4f} {: .4f} {: .4f} | {: .4f} {: .4f} {: .4f} | {: .4f}'.format(a,c,b, fa,fc,fb, chyba))
if fc == 0: # koren nalezen
break
if chyba < maxchyba: # takova presnost nam staci
break
# prejdeme na polovinu, kde se meni znamenko
if znamenko(fa) != znamenko(fc):
b = c
else:
a = c
1.0000 1.5000 2.0000 | -1.0000 0.8750 5.0000 | 0.5000
1.0000 1.2500 1.5000 | -1.0000 -0.2969 0.8750 | 0.2500
1.2500 1.3750 1.5000 | -0.2969 0.2246 0.8750 | 0.1250
1.2500 1.3125 1.3750 | -0.2969 -0.0515 0.2246 | 0.0625
1.3125 1.3438 1.3750 | -0.0515 0.0826 0.2246 | 0.0312
1.3125 1.3281 1.3438 | -0.0515 0.0146 0.0826 | 0.0156
1.3125 1.3203 1.3281 | -0.0515 -0.0187 0.0146 | 0.0078
1.3203 1.3242 1.3281 | -0.0187 -0.0021 0.0146 | 0.0039
1.3242 1.3262 1.3281 | -0.0021 0.0062 0.0146 | 0.0020
1.3242 1.3252 1.3262 | -0.0021 0.0020 0.0062 | 0.0010

Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 28 hostů
Podobná vlákna
Pomoc-puleni intervalu — založil huck
Opakované půlení pole — založil john22
Prvocisla v intervalu — založil zemlra
Horni, dolni mez intervalu — založil vitamin
Refresh RichTextBoxu v časovém intervalu — založil Brouk Pytlík
Moderátoři diskuze