0

my project consists of 6 files with .py. The first is the main project file, which I called Metodos.py and whose code is:

#!/usr/bin/python 
# coding: latin-1

#import os, sys

import Menu
from numpy import *

Menu.elmenu() 

Let's see the code Menu.py file:

#!/usr/bin/python 
# coding: latin-1

from math import *

import Biseccion 
import PuntoFijo 
import Secante

def op1 (): 
a = float(input(“Introduce el extremo inferior del intervalo\n”)) 
b = float(input(“Introduce el extremo superior del intervalo\n”)) 
TOL = float(input(“Introduce la tolerancia del metodo\n”)) 
Biseccion.biseccion(a, b, TOL, 100) 
Biseccion.dibujar(a, b, TOL, 100) 
def op2 (): 
po = float(input(“Introduce el valor inicial\n”)) 
TOL = float(input(“Introduce la tolerancia del metodo\n”)) 
PuntoFijo.puntofijo(po, TOL, 100) 
PuntoFijo.dibujar(po, TOL, 100) 
def op3 (): 
po = float(input(“Introduce el primer valor inicial\n”)) 
p1 = float(input(“Introduce el segundo valor inicial\n”)) 
TOL = float(input(“Introduce la tolerancia del metodo\n”)) 
Secante.secante(po, p1, TOL, 100) 
Secante.dibujar(po, p1, TOL, 100) 
def errhandler (): 
print “Tu eleccion no ha sido la correcta\n”

Next comes Biseccion.py code, which is:

#!/usr/bin/python

import Evaluar 
from pylab import * 
from Numeric import *

def biseccion(a, b, TOL, N): 
Evaluar.dicc_seguro[‘x’]=a 
fa = eval(Evaluar.funcion, {“__builtins__”:None}, Evaluar.dicc_seguro) 
vectorx = zeros(N, Float64) 
vectory = zeros(N, Float64)

i = 1 
while i<=N : 
p = (a+b)/2.0 
vectorx[i-1] = p 
Evaluar.dicc_seguro['x']=p 
fp = eval(Evaluar.funcion, {"__builtins__":None}, Evaluar.dicc_seguro) 
vectory[i-1]=fp 
if (fp == 0.0) or ( (b-a)/2.0) break 
i = i+1 
if (fa*fp)>0 : 
a = p 
else : 
b = p 
print “La raiz buscada es: “,p, “con”, i-1,”iteraciones” 
return [vectorx, vectory]

def dibujar(a,b,TOL, N): 
x = arange(a,b,0.1) 
vectores=biseccion(a, b, TOL, N)

subplot(211) 
plot(x, eval(Evaluar.funcion), linewidth=1.0) 
xlabel(‘Abcisa’) 
ylabel(‘Ordenada’) 
title(‘Metodo Biseccion con f(x)=’ + Evaluar.funcion) 
grid(True) 
axhline(linewidth=1, color=’r’) 
axvline(linewidth=1, color=’r’)

subplot(212) 
plot(vectores[0], vectores[1], ‘k.’) 
xlabel(‘Abcisa’) 
ylabel(‘Ordenada’) 
grid(True) 
axhline(linewidth=1, color=’r’) 
axvline(linewidth=1, color=’r’)

show() 

Likewise I have written the method of fixed point PuntoFijo.py

#!/usr/bin/python

import Evaluar 
from pylab import * 
from Numeric import *

def puntofijo(po,TOL, N): 
vectorx = zeros(N, Float64) 
vectory = zeros(N, Float64)

i = 1 
while i<=N : 
vectorx[i-1] = po 
Evaluar.dicc_seguro['x']=po 
fp = eval(Evaluar.funcion, {"__builtins__":None}, Evaluar.dicc_seguro) 
vectory[i-1]=fp 
if fabs(po-fp) print "La raiz buscada es: ",po, "con", i-1, "iteraciones" 
break 
i = i+1 
po = fp 
return [vectorx, vectory]

def dibujar(po,TOL, N): 
x = arange(po-2,po+2,0.1) 
vectores=puntofijo(po, TOL, N)

subplot(211) 
plot(x, eval(Evaluar.funcion), linewidth=1.0) 
xlabel('Abcisa') 
ylabel('Ordenada') 
title('Metodo Punto Fijo con f(x)= x - ' + Evaluar.funcion) 
grid(True) 
axhline(linewidth=1, color='r') 
axvline(linewidth=1, color='r')

subplot(212) 
plot(vectores[0], vectores[1], 'k.') 
xlabel('Abcisa') 
ylabel('Ordenada') 
grid(True) 
axhline(linewidth=1, color='r') 
axvline(linewidth=1, color='r')

show() 

the secant I've put in Secante.py and its code is:

#!/usr/bin/python

import Evaluar 
from pylab import * 
from Numeric import *

def secante(po, p1, TOL, N): 
Evaluar.dicc_seguro['x']=po 
qo = eval(Evaluar.funcion, {"__builtins__":None}, Evaluar.dicc_seguro) 
Evaluar.dicc_seguro['x']=p1 
q1 = eval(Evaluar.funcion, {"__builtins__":None}, Evaluar.dicc_seguro) 
vectorx = zeros(N, Float64) 
vectorx1 = zeros(N, Float64) 
vectory = zeros(N, Float64) 
vectory1 = zeros(N, Float64)

i = 2 
while i<=N : 
p = p1-(q1*(p1-po)/(q1-qo)) 
vectorx[i-2] = po 
vectorx1[i-2] = p1 
Evaluar.dicc_seguro['x']=p 
fp = eval(Evaluar.funcion, {"__builtins__":None}, Evaluar.dicc_seguro) 
vectory[i-2]=qo 
vectory1[i-2]=q1 
if fabs(po-p1) print "La raiz buscada es: ",p, "con", i-2,"iteraciones" 
break

i = i+1 
po = p1 
qo = q1 
p1 = p 
q1 = fp

return [vectorx, vectory, vectorx1, vectory1]

def dibujar(po, p1, TOL, N): 
x = arange(po-2,po+2,0.1) 
vectores=secante(po, p1, TOL, N)

subplot(211) 
plot(x, eval(Evaluar.funcion), linewidth=1.0) 
xlabel('Abcisa') 
ylabel('Ordenada') 
title('Metodo Secante con f(x)= ' + Evaluar.funcion) 
grid(True) 
axhline(linewidth=1, color='r') 
axvline(linewidth=1, color='r')

subplot(212) 
plot(vectores[0], vectores[1], 'r.', vectores[2], vectores[3], 'b.') 
xlabel('Abcisa') 
ylabel('Ordenada') 
grid(True) 
axhline(linewidth=1, color='r') 
axvline(linewidth=1, color='r')

show() 

Finally, there is Evaluar.py code, which is:

from math import *

lista_segura = ['math','acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'de grees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'] 
dicc_seguro = dict([ (k, locals().get(k, None)) for k in lista_segura ])

print"METODOS ITERATIVOS PARA APROXIMAR RAICES." 
print "________________________________________\n" 
print "1. Metodo de la Biseccion\n" 
print "2. Metodo del Punto Fijo\n" 
print "3. Metodo de la Secante\n"

funcion = raw_input("Introduce la funcion para la cual vas a aplicar uno de los metodos\n") 

and the erros that show

 File "C:\Users\manuela\Desktop\Metodos\PuntoFijo.py", line 20
    break 
    ^
IndentationError: unexpected indent

thanks for your help

2
Contributors
1
Reply
40
Views
1 Year
Discussion Span
Last Post by Gribouillis
2

I'm afraid the indention didn't make it through the web editor. The best way to indent python code is with 4 space characters such as in

if x == 0:
    print("hello") # <- see the 4 space at the beginning ?

It means that you must configure your editor so that it inserts 4 space characters when you hit the tab key. Which code editor are you using ? Look for the preference menu.

About your existing code, you can reindent it correctly by installing Tim Peters' reindent module. After installing reindent (pip install reindent in a console window may work), you can type for example

reindent Secante.py

in a console window, and this will indent the Secante.py file with spaces.

DO NOT indent python code with tab characters '\t' if you want to avoid indention issues.

Edited by Gribouillis

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.