# Define aquí la funcion y el intervalo [a,b] de comienzo. También la amplitud del intervalo en la que parar (tolerancia)
f(x) = sqrt(x) * sin(x) - x^3 +2
a=1;
b=2;
tolerancia=1/12000
def signo(x):
if (x>0):
return '+'
if (x<0):
return '-'
else:
return '0'
num=0;
print 'Biseccion para ' , f
print
print 'n' , '\t' , 'a' ,'\t' ,'\t','\t' , 'b' ,'\t' ,'\t' ,'\t' , 'c' ,'\t' ,'\t' ,'\t' , 'f(a)' ,'\t' , 'f(b)' ,'\t' , 'f(c)'
print '---------------------------------------------------------------------'
while ((abs(a-b))>tolerancia):
c=(a+b)/2;
print num ,'\t' , n(a) ,'\t' , n(b) ,'\t' ,n(c) ,'\t' , signo(f(a)) ,'\t' , signo(f(b)) ,'\t' , signo(f(c))
if (f(a) * f(c) > 0):
a=c;
else:
b=c;
num=num+1
print
print 'Solucion: ' , c
|
|
Biseccion para x |--> -x^3 + sqrt(x)*sin(x) + 2
n a b c f(a) f(b) f(c)
---------------------------------------------------------------------
0 1.00000000000000 2.00000000000000 1.50000000000000 + - -
1 1.00000000000000 1.50000000000000 1.25000000000000 + - +
2 1.25000000000000 1.50000000000000 1.37500000000000 + - +
3 1.37500000000000 1.50000000000000 1.43750000000000 + - +
4 1.43750000000000 1.50000000000000 1.46875000000000 + - +
5 1.46875000000000 1.50000000000000 1.48437500000000 + - -
6 1.46875000000000 1.48437500000000 1.47656250000000 + - -
7 1.46875000000000 1.47656250000000 1.47265625000000 + - +
8 1.47265625000000 1.47656250000000 1.47460937500000 + - +
9 1.47460937500000 1.47656250000000 1.47558593750000 + - -
10 1.47460937500000 1.47558593750000 1.47509765625000 + - -
11 1.47460937500000 1.47509765625000 1.47485351562500 + - +
12 1.47485351562500 1.47509765625000 1.47497558593750 + - +
13 1.47497558593750 1.47509765625000 1.47503662109375 + - -
Solucion: 24167/16384
Biseccion para x |--> -x^3 + sqrt(x)*sin(x) + 2
n a b c f(a) f(b) f(c)
---------------------------------------------------------------------
0 1.00000000000000 2.00000000000000 1.50000000000000 + - -
1 1.00000000000000 1.50000000000000 1.25000000000000 + - +
2 1.25000000000000 1.50000000000000 1.37500000000000 + - +
3 1.37500000000000 1.50000000000000 1.43750000000000 + - +
4 1.43750000000000 1.50000000000000 1.46875000000000 + - +
5 1.46875000000000 1.50000000000000 1.48437500000000 + - -
6 1.46875000000000 1.48437500000000 1.47656250000000 + - -
7 1.46875000000000 1.47656250000000 1.47265625000000 + - +
8 1.47265625000000 1.47656250000000 1.47460937500000 + - +
9 1.47460937500000 1.47656250000000 1.47558593750000 + - -
10 1.47460937500000 1.47558593750000 1.47509765625000 + - -
11 1.47460937500000 1.47509765625000 1.47485351562500 + - +
12 1.47485351562500 1.47509765625000 1.47497558593750 + - +
13 1.47497558593750 1.47509765625000 1.47503662109375 + - -
Solucion: 24167/16384
|