Witam!
Mam problem z implementacją tych metod, ze względu na wyskakujące błędy w obu funkcjach (siecznych i bisekcji):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() <-- Linia 26 oraz 36
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 12 17:52:17 2020
@author: Sajmon
"""
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(-3,3,0.1)#zbior argumentow na ktorych pracuje program
n=int(input("Podaj stopien rownania: "))
fun=input("Podaj rownanie liniowe ktore program ma sie wykonac: ")
fun=eval(fun)
metoda = int(input(" Wybierz metode wykonania - 1(siecznych) czy 2(bisekcja) ? "))
print("Wybrales metode: {metoda}".format(metoda=metoda))
def fx(x):
global fun
x=fun
return x
def secant(fx,a,b,err):
fb=fx(b)
while np.absolute(fb)>err:
midPoint=b-(b-a)*fb/(fb-fx(a))
a=b
b=midPoint
fb=fx(b)
return b
def bis(fx,a,b,err):
while np.absolute(b-a)>err:
midPoint=(a+b)*0.5
if fx(midPoint)*fx(a)<0:
b=midPoint
midPoint=(a+b)*0.5
if fx(midPoint)*fx(b)<0:
a=midPoint
return b-(b-a)*fx(b)/(fx(b)-fx(a))
print("Graficzne zestawienie podanej funkcji:")
y=fx(x)
plt.plot(x,y,'r.')
plt.grid(True)
plt.show()
while metoda!=1 and metoda!=2:
print("Podales złe dane")
metoda = int(input("1(siecznych) czy 2(bisekcja) ? "))
print("Wybrales metode {metoda}".format(metoda=metoda))
if metoda==1: #siecznych
i=1
a=int(input("Podaj pierwszy kraniec przedziału: "))
b=int(input("Podaj drugi kraniec przedziału: "))
while i<n:
err=0.0001
print("x= ",secant(fx,a,b,err))
i=i+1
if metoda==2:#bisekcja
i=1
a=int(input("Podaj pierwszy kraniec przedziału: "))
b=int(input("Podaj drugi kraniec przedziału: "))
while i<n:
err=0.0001
print("x= ",bis(fx,a,b,err))
i=i+1
Mam prośbę o ewentualną pomoc, rozwiązanie tego problemu :)
Pozdrawiam.