/* ******************************************************************************* MŽtodos NumŽricos Jorge Juan G—mez Basanta 23 / Febrero / 1998 Este programa fue desarrollado por Jorge Juan G—mez Basanta ********************************************************************************** */ import java.applet.Applet; import java.awt.Button; import java.awt.Color; import java.awt.Event; import java.awt.Graphics; import java.awt.Label; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ComponentEvent; public class NewtonRaphson extends Applet { //Declaraci—n de controles Button botonTabular, botonNR; Label etiqueta, etiqueta2, etiqueta3, etiqueta4, etiqError, titulo, mensaje, ecuacion, autor; TextField campoLimInf, campoLimSup, campoDeltax, campoTol; TextArea campo; //Declaraci—n de variables globales double limInf, limSup, deltaX, x0, tolerancia; int contr = 0, tabb = 0; Color gris = new Color(204, 204, 204); //****************************init public void init() { //definir arreglos //valoresy = new double[100]; //interface de usuario setLayout(null); resize(550, 250); titulo = new Label("Bienvenido al Programa de Newton-Raphson", Label.CENTER); add(titulo); titulo.setBounds(0, 5, 500, 18); autor = new Label("desarrollado por Jorge Juan G\u00F3mez Basanta", Label.CENTER); add(autor); autor.setBounds(0, 19, 500, 18); //***** ecuacion = new Label("Ecuaci\u00F3n: 3 x^2 - e^x = 0"); add(ecuacion); ecuacion.setBounds(getInsets().left + 6, getInsets().top + 40, 200, 15); etiqueta = new Label("L\u00EDmite inferior:"); add(etiqueta); etiqueta.setBounds(getInsets().left + 6, getInsets().top + 62, 84, 15); campoLimInf = new TextField(8); add(campoLimInf); campoLimInf.setBounds(getInsets().left + 90, getInsets().top + 60, 72, 18); //***** etiqueta2 = new Label("L\u00EDmite superior:"); add(etiqueta2); etiqueta2.setBounds(getInsets().left + 6, getInsets().top + 82, 84, 15); campoLimSup = new TextField(8); add(campoLimSup); campoLimSup.setBounds(getInsets().left + 90, getInsets().top + 80, 72, 18); //***** etiqueta3 = new Label("\u0394x:"); add(etiqueta3); etiqueta3.setBounds(getInsets().left + 6, getInsets().top + 102, 84, 15); campoDeltax = new TextField(8); add(campoDeltax); campoDeltax.setBounds(getInsets().left + 90, getInsets().top + 100, 72, 18); //***** etiqError = new Label(""); add(etiqError); etiqError.setBounds(15, getInsets().top + 235, 500, 15); etiqError.setBackground(gris); botonTabular = new Button("Tabular funci\u00F3n"); add(botonTabular); botonTabular.setBounds(6, 120, 152, 18); //botonTabular.setBackground(Color.orange); //***** newton rapshon etiqueta4 = new Label("Tolerancia:"); add(etiqueta4); etiqueta4.setBounds(getInsets().left + 6, getInsets().top + 144, 84, 15); campoTol = new TextField(8); add(campoTol); campoTol.setBounds(getInsets().left + 90, getInsets().top + 142, 72, 18); botonNR = new Button("Newton-Raphson"); add(botonNR); botonNR.setBounds(6, 162, 152, 18); //botonNR.setBackground(Color.orange); // ***** zona derecha campo = new TextArea(5, 20); add(campo); campo.setBounds(getInsets().left + 165, getInsets().top + 60, 370, 120); mensaje = new Label(""); add(mensaje); mensaje.setBounds(getInsets().left + 165, getInsets().top + 182, 370, 45); campoLimInf.requestFocus(); } //****************************event public boolean processEvent(Event event) { if (event.id == Event.ACTION_EVENT && event.target == botonTabular) { int error = 0; etiqError.setText(""); etiqError.setBackground(gris); try { deltaX = (Double.valueOf(campoDeltax.getText())).doubleValue(); } catch (NumberFormatException e) { etiqError.setBackground(Color.red); etiqError.setText("ERROR! Debes escribir \u0394x"); error = 1; } try { limSup = (Double.valueOf(campoLimSup.getText())).doubleValue(); } catch (NumberFormatException e) { etiqError.setBackground(Color.red); etiqError.setText("ERROR! Debes escribir el l\u00EDmite superior"); error = 1; } try { limInf = (Double.valueOf(campoLimInf.getText())).doubleValue(); } catch (NumberFormatException e) { etiqError.setBackground(Color.red); etiqError.setText("ERROR! Debes escribir el l\u00EDmite inferior"); error = 1; } if (limSup < limInf) { etiqError.setBackground(Color.red); etiqError.setText("ERROR! El l\u00EDmite inferior debe ser menor que el l\u00EDmite superior."); error = 1; } if (deltaX < 0) { etiqError.setBackground(Color.red); etiqError.setText("ERROR! \u0394x debe ser positivo."); error = 1; } if (deltaX == 0) { etiqError.setBackground(Color.red); etiqError.setText("ERROR! \u0394x no puede ser cero."); error = 1; } if (deltaX > Math.abs(limSup - limInf)) { etiqError.setBackground(Color.red); etiqError.setText("ERROR! El valor de \u0394x es inaceptable."); error = 1; } System.out.println("L\u00EDmite Inferior: "); System.out.println(limInf); System.out.println("L\u00EDmite Superior: "); System.out.println(limSup); System.out.println("\u0394x: "); System.out.println(deltaX); if (error == 0) tabular(); return true; } else if (event.id == Event.ACTION_EVENT && event.target == botonNR) { int error = 0; etiqError.setText(""); try { tolerancia = (Double.valueOf(campoTol.getText())).doubleValue(); } catch (NumberFormatException e) { etiqError.setText("ERROR! Debes escribir la tolerancia"); error = 1; } if (tolerancia > 0.1) { etiqError.setText("ERROR! La tolerancia es muy alta."); error = 1; } if (tolerancia < 0) { etiqError.setText("ERROR! La tolerancia debe ser positiva."); error = 1; } if (tolerancia == 0) { etiqError.setText("ERROR! La tolerancia no puede ser cero."); error = 1; } System.out.println("Tolerancia: "); System.out.println(tolerancia); if (error == 0) newtonraphson(); return true; } return true; //return super.processComponentEvent((ComponentEvent)event); //return super.processEvent(event); } //*************************** evaluarfuncion public double evaluarfuncion(double num) { double resul = (3 * Math.pow(num, 2)) - Math.exp(num); return resul; } //*************************** evaluarderiv public double evaluarderiv(double num) { double resul = (6 * num) - Math.exp(num); return resul; } //*************************** evaluarsegderiv public double evaluarsegderiv(double num) { double resul = (6) - Math.exp(num); return resul; } //*************************** tabular public void tabular() { etiqError.setBackground(gris); etiqError.setText(""); campo.setText(""); tabb = 1; int encontreraiz = 0; double convergencia, incremento = 0; campo.append("x\t\t\t\t\t\t\t\t\t\tf(x)\n"); campo.append("ÑÑÑÑÑÑÑÑÑÑÑ\n"); campo.append(String.valueOf(limInf)); campo.append("\t\t\t\t\t\t\t\t\t" + String.valueOf(evaluarfuncion(limInf))); campo.append("\n"); limInf += deltaX; for (double x = limInf; x < limSup + deltaX; x += deltaX) { campo.append(String.valueOf(x)); campo.append("\t\t\t\t\t\t\t\t\t\t"); campo.append(String.valueOf(evaluarfuncion(x))); if ((evaluarfuncion(x - deltaX) * evaluarfuncion(x)) < 0) { campo.append("\nHay una raiz entre "); campo.append(String.valueOf(x - deltaX) + " y " + String.valueOf(x)); contr++; if (contr == 1) { for (int factor = 1; factor <= 10; factor++) { incremento = (x - (x - deltaX)) / 10; x0 = (x - deltaX) + factor * incremento; System.out.println("x0 "); System.out.println(x0); System.out.println("incremento "); System.out.println(incremento); convergencia = Math.abs((evaluarfuncion(x0) * evaluarsegderiv(x0)) / (evaluarderiv(x0) * evaluarderiv(x0))); if (convergencia < 1) { mensaje.setText( "Se elige a " + String.valueOf(x0) + " como x0 para el m\u00E9todo de Newton Rapshon, ya que cumple con el criterio de convergencia. Presiona Newton-Raphson para encontrar la ra\u00EDz."); factor = 10; encontreraiz = 1; } //if converg } //for factor if (encontreraiz == 0) contr = 0; } //for raices } // if encuentra raiz campo.append("\n"); } // fin for if (contr == 0) mensaje.setText("No encontr\u00E9 ra\u00EDces."); } //*************************** newtonraphson public void newtonraphson() { int diverge = 0; double x = x0; if (contr == 0) { if (tabb == 0) mensaje.setText("Primero debes tabular."); else mensaje.setText("No se encontraron raices, intenta con otros l\u00EDmites."); } else { double raizirr, evalfx, evalderiv, cociente; int cont = 0; evalfx = evaluarfuncion(x0); evalderiv = evaluarderiv(x0); cociente = (evalfx / evalderiv); System.out.println("cociente "); System.out.println(cociente); while (Math.abs(cociente) >= tolerancia) { raizirr = x - cociente; evalfx = evaluarfuncion(raizirr); evalderiv = evaluarderiv(raizirr); x = raizirr; cont++; cociente = (evalfx / evalderiv); System.out.println("cociente "); System.out.println(cociente); if (cont == 25) { cociente = tolerancia + 1; mensaje.setText("El m\u00E9todo no converge."); diverge = 1; } } //while if (diverge == 0) mensaje.setText("La raiz es: " + String.valueOf(x)); } } //****************************paint public void paint(Graphics g) { } } //fin de clase