You are on page 1of 8

1. import java.lang.

Math;
2. import java.io.*;
3. //Clase ProyectoJava
4. public class ProyectoJava{
5.
6. //Metodo Constructor
7. public static void main(String args[]){
8. ProyectoJava Proyecto = new ProyectoJava();
9. Proyecto.menu(); //solo se manda a llamar a menu desde aqui, ya
que menu llama a los demas metodos posteriromente
10. }
11.
12. //(1)Metodo Biseccion
13. public void MetodoBiseccion(){
14. double a;
15. double b;
16. double tol;
17. System.out.println("\t\t\t\"METODO DE BISECCION\"");
18. System.out.println("Extremo Izquierdo: ");
19. a=lee();
20. System.out.println("Extremo Derecho: ");
21. b=lee();
22. System.out.println("Tolerancia: ");
23. tol=lee();
24. double c;
25. do{
26. c=(a+b)/2.0;
27. if(((c*c-5)*(a*a-5))<0){ b=c; } else{ a=c; }
}while(Math.abs(a-b)>tol);
28. System.out.println("La raiz es: "+c);
29. }
30.
31. //(2)Metodo NewRaphson
32. public void MetodoNewtonRaphson(){
33. double a;
34. double tol;
35. double b;
36. double c;
37. System.out.println("\t\t\t\"METODO DE NEWTON-RAPHSON\"");
38. System.out.println("Primera Aproximacion: ");
39. a=lee();
40. System.out.println("Tolerancia: ");
41. tol=lee();
42. do{
43. b=a-(a*a-a-2)/(2*a-1);
44. c=Math.abs(a-b);
45. a=b;
46. }while(c>tol);

47. System.out.println("La raiz es: "+b);


48. }
49.
50. //(3) metodo gauss
51. public void MetodoGauss(){
52. int i,j,k,n;
53. double a[][] = new double [5][6];
54. double cte,x1,x2,x3;
55. double x[] = new double [5];
56. System.out.println("\t\t\t\"METODO DE GAUSS\"");
57. System.out.println("Dame el numero de incognitas (de hasta 5)
");
58. n=leeint();
59. System.out.println("Ingrese coeficientes");
60. for(i=0;i {
61. System.out.println("Renglon "+(i+1));
62. for(j=0;j<=n;j++)
63. {
64. System.out.println(" Ingrese a "+(i+1)+" "+(j+1));
65. a[i][j]=lee();
66. }
67. }
68. for(i=0;i for(j=i+1;j<=n;j++){
69. cte=(-a[j][i])/(a[i][i]);
70. for(k=i;k a[j][k]=((a[i][k])*cte)+a[j][k];
71. }
72. }
73. }
74. x3=a[n-1][n]/a[n-1][n-1];
75. x2=(a[n-2][n]-x3*a[n-2][n-1])/a[n-2][n-2];
76. x1=(a[n-3][n]-x2*a[n-3][n-2]-x3*a[n-3][n-1])/a[n-3][n-3];
77. System.out.println("x0= "+x1+" \nx1= " +x2+" \nx2= " +x3);
78. }
79.
80. //(4)gauss seidel
81. public void MetodoGaussSeidel(){
82. double x0,x1,x2,tol,e;
83. int i,j;
84. double a[][]=new double [3][4];
85. System.out.println("\t\t\t\"METODO DE GAUSS-SEIDEL 3
ECUACIONES\"");
86. System.out.println("Ingrese tolerancia");
87. tol=lee();
88. System.out.println("Ingrese coeficientes");
89. for(i=0;i<3;i++)
90. {
91. System.out.println("Renglon "+(i+1));
92. for(j=0;j<=3;j++) { System.out.println(" Ingrese a "+(i+1)+"
"+(j+1)); a[i][j]=lee(); } } x1=0.0; x2=0.0; do{ e=x1; x0=(a[0][3]-

x1*a[0][1]-x2*a[0][2])/a[0][0]; x1=(a[1][3]-x0*a[1][0]-x2*a[1]
[2])/a[1][1]; x2=(a[2][3]-x0*a[2][0]-x1*a[2][1])/a[2]
[2]; }while(Math.abs(e-x1)>tol);
93. System.out.println("x0= "+x0+" \nx1= " +x1+" \nx2= " +x2);
94. }
95.
96. //(5) gauss jordan
97. public void MetodoGaussJordan(){
98. int i,j,k,n;
99. double a[][] = new double [5][6];
100. double cte;
101. double x[] = new double [5];
102. System.out.println("\t\t\t\"METODO DE GAUSS-JORDAN\"");
103. System.out.println("No. de incognitas (maximo 5): ");
104. n=leeint();
105. System.out.println("Dame los coeficientes: ");
106. for(i=0;i {
107. System.out.println("Renglon "+(i+1));
108. for(j=0;j<=n;j++)
109.
{
110. System.out.println(" Ingrese a "+(i+1)+" "+(j+1));
111. a[i][j]=lee();
112.
}
113.
}
114. for(i=0;i for(j=i+1;j<=n;j++){
115. cte=(-a[j][i])/(a[i][i]);
116. for(k=i;k a[j][k]=((a[i][k])*cte)+a[j][k];
117.
}
118.
}
119.
}
120. for(i=0;i {
121. System.out.println("Renglon "+(i+1));
122.for(j=0;j<=n;j++)
123.
{
124. System.out.println(" a "+(i+1)+" "+(j+1)+" =" +a[i][j]);
125.
}
126.
}
127.
}
128.
129.
//(6) LU
130. public void MetodoLu(){
131. int i,j;
132. double Y1,Y2,Y3,X1,X2,X3;
133. double A[][] = new double [3][3];
134. double L[][] = new double [3][3];
135. double U[][] = new double [3][3];
136. double B[] = new double [3];
137. System.out.println("\t\t\t\"METODO DE LU 3 ECUACIONES\"");
138. System.out.println("Ingrese coeficientes d");

139. for(i=0;i<3;i++)
140.
{
141. System.out.println("Renglon "+(i+1));
142. for(j=0;j<3;j++)
143.
{
144. System.out.println(" Ingrese a "+(i+1)+" "+(j+1));
145. A[i][j]=lee();
146.
}
147.
}
148. System.out.println("Ingrese los terminos independientes");
149. for(i=0;i<3;i++){
150. System.out.println("Termino "+(i+1));
151. B[i]=lee();
152.
}
153. for(i=0;i<3;i++){
154. L[i][0]=A[i][0];
155. U[i][i]=1;
156.
}
157. U[0][1]=A[0][1]/L[0][0];
158. L[1][1]=A[1][1]-L[1][0]*U[0][1];
159. L[2][1]=A[2][1]-L[2][0]*U[0][1];
160. U[0][2]=A[0][2]/L[0][0];
161. U[1][2]=(A[1][2]-L[1][0]*U[0][2])/L[1][1];
162. L[2][2]=A[2][2]-L[2][0]*U[0][2]-L[2][1]*U[1][2];
163. Y1=B[0]/L[0][0];
164. Y2=(B[1]-L[1][0]*Y1)/L[1][1];
165. Y3=(B[2]-L[2][0]*Y1-L[2][1]*Y2)/L[2][2];
166.
X3=Y3;
167. X2=Y2-U[1][2]*X3;
168. X1=Y1-U[0][1]*X2-U[0][2]*X3;
169. System.out.println("El resultado es:\nx0= "+X1+" \nx1=
" +X2+" \nx2= " +X3);
170.
}
171.
172.
//(7) Interpolacion Newton
173. public void MetodoInterNewton(){
174. double a[][] = new double [5][2];
175. double x,y,fx1x0,fx2x1,fx3x2,fx4x3,fx2x1x0,fx3x2x1,fx4x3x2,fx3x2
x1x0,fx4x3x2x1,fx4x3x2x1x0;
176. int i;
177. System.out.println("\t\t\t\"INTERPOLACION DE DIFERENCIAS DE
NEWTON P/ 5 PTOS.\"");
178. System.out.println("Valor a interpolar: ");
179. x=lee();
180. System.out.println("Dame los 5 pares de puntos");
181. for(i=0;i<5;i++){
182. System.out.println("Dame x "+i);
183. a[i][0]=lee();
184. System.out.println("Dame f(x) "+i);

185. a[i][1]=lee();
186.
}
187. fx1x0=(a[1][1]-a[0][1])/(a[1][0]-a[0][0]);
188. fx2x1=(a[2][1]-a[1][1])/(a[2][0]-a[1][0]);
189. fx3x2=(a[3][1]-a[2][1])/(a[3][0]-a[2][0]);
190. fx4x3=(a[4][1]-a[3][1])/(a[4][0]-a[3][0]);
191. fx2x1x0=(fx2x1-fx1x0)/(a[2][0]-a[0][0]);
192. fx3x2x1=(fx3x2-fx2x1)/(a[3][0]-a[1][0]);
193. fx4x3x2=(fx4x3-fx3x2)/(a[4][0]-a[2][0]);
194. fx3x2x1x0=(fx3x2x1-fx2x1x0)/(a[3][0]-a[0][0]);
195. fx4x3x2x1=(fx4x3x2-fx3x2x1)/(a[3][0]-a[0][0]);
196. fx4x3x2x1x0=(fx4x3x2x1-fx3x2x1x0)/(a[4][0]-a[0][0]);
197. y=a[0][1]+fx1x0*(x-a[0][0])+fx2x1x0*(x-a[0][0])*(x-a[1][0])
+fx3x2x1x0*(x-a[0][0])*(x-a[1][0])*(x-a[2][0])+fx4x3x2x1x0*(x-a[0]
[0])*(x-a[1][0])*(x-a[2][0])*(x-a[3][0]);
198. System.out.println("f(x) en ese punto es: "+y);
199.
}
200.
201.
//(8) Interpolacion Lagrange
202.public void MetodoInterLagrange(){
203. int n,i;
204. double x,y;
205. double a[][] = new double [4][2];
206. System.out.println("\t\t\t\"METODO DE INTERPOLACION LAGRANGE 2-4
PTOS.\"");
207. do{
208. System.out.println("Dame el numero de puntos");
209. n=leeint();
210. }while(n<2 || n>4);
211. System.out.println("Dame los pares de puntos");
212.for(i=0;i System.out.println("Dame x "+i);
213. a[i][0]=lee();
214. System.out.println("Dame f(x) "+i);
215. a[i][1]=lee();
216.
}
217. System.out.println("Dame el valor a interpolar ");
218. x=lee();
219. switch(n){
220. case 2:
221. y=(((((x-a[1][0])*a[0][1])/(a[0][0]-a[1][0]))+(((x-a[0][0])*a[1]
[1])/(a[1][0]-a[0][0]))));
222.System.out.println("f(x) en ese punto es: "+y);
223.break;
224.case 3:
225.y=((x-a[1][0])*(x-a[2][0])*a[0][1])/((a[0][0]-a[1][0])*(a[0][0]a[2][0]))+((x-a[0][0])*(x-a[2][0])*a[1][1])/((a[1][0]-a[0][0])*(a[1]
[0]-a[2][0]))+((x-a[1][0])*((x-a[0][0])*a[2][1])/((a[2][0]-a[0]
[0])*(a[2][0]-a[1][0])));
226.System.out.println("f(x) en ese punto es: "+y);

227.break;
228.case 4:
229.y=((x-a[1][0])*(x-a[3][0])*(x-a[2][0])*a[0][1])/((a[0][0]-a[1]
[0])*(a[0][0]-a[2][0])*(a[0][0]-a[3][0]))+((x-a[0][0])*(x-a[2]
[0])*(x-a[3][0])*a[1][1])/((a[1][0]-a[0][0])*(a[1][0]-a[2][0])*(a[1]
[0]-a[3][0]))+((x-a[0][0])*(x-a[1][0])*((x-a[3][0])*a[2][1])/((a[2]
[0]-a[0][0])*(a[2][0]-a[1][0])*(a[2][0]-a[3][0]))+((x-a[0][0])*(xa[1][0])*((x-a[2][0])*a[
230. 3][1])/((a[3][0]-a[0][0])*(a[3][0]-a[1][0])*(a[3][0]-a[2]
[0]))));
231. System.out.println("f(x) en ese punto es: "+y);
232.break;
233.
default:
234.System.out.println("INVALIDO");
235.break;
236.
}
237.
}
238.
239.
//para leer desde teclado
240. public double lee(){
241. double num;
242.try{
243.InputStreamReader isr = new InputStreamReader (System.in);
244.BufferedReader br = new BufferedReader(isr);
245.String sdato;
246.sdato = br.readLine();
247.num = Double.parseDouble(sdato);
248.
}
249.catch(IOException ioe){
250. num=0.0;
251.
}
252.return num;
253.
}
254.
255.
//para leer un entero
256.public int leeint(){
257.int num;
258.try{
259.InputStreamReader isr = new InputStreamReader (System.in);
260. BufferedReader br = new BufferedReader(isr);
261. String sdato;
262.sdato = br.readLine();
263.num = Integer.parseInt(sdato);
264.
}
265.catch(IOException ioe){
266.num=0;
267.
}
268.return num;
269.
}

270.
271.
//para salir del programa
272.public int Fuera(){
273.int sal;
274.System.out.println("\n\n\nSI DESEAS OTRO METODO PRESIONA [1]");
275.sal=leeint();
276.return sal;
277.
}
278.
279.
//despliega menu
280. public void menu(){
281. int a;
282.int p;
283.do{
284.do{
285.System.out.println("\n\n\t\t\tMETODOS NUMERICOS\n\n");
286.System.out.println("\t1.-Biseccion\n\t2.-Newton-Raphson\n\t3.Gauss\n\t4.-Gauss-Seidel\n\t5.-Gauss-Jordan\n\t6.-LU\n\t7.Interpolacion Newton\n\t8.-Interpolacion Lagrange");
287.System.out.println("\n\nEscoja el numero del metodo que desea
usar:");
288.a=leeint();
289.}while(a<1 || a>8);
290. switch(a){
291. case 1:
292.MetodoBiseccion(); //manda a llamara a cada uno de los metodos
293.p=Fuera();
294.break;
295.case 2:
296.MetodoNewtonRaphson();
297.p=Fuera();
298.break;
299.case 3:
300. MetodoGauss();
301. p=Fuera();
302. break;
303. case 4:
304. MetodoGaussSeidel();
305. p=Fuera();
306. break;
307. case 5:
308. MetodoGaussJordan();
309. p=Fuera();
310. break;
311. case 6:
312. MetodoLu();
313. p=Fuera();
314. break;
315. case 7:

316. MetodoInterNewton();
317. p=Fuera();
318. break;
319. case 8:
320. MetodoInterLagrange();
321. p=Fuera();
322.break;
323.
default:
324.System.out.println("Opcion incorrecta");
325.p=1;
326.break;
327.
}
328.}while(p==1);
329.
}
330.
}

You might also like