Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- public class Principal {
- public static void main (String[] args){
- Min_Search_Golden_Section(0, 4, 0.1);
- }
- static double f1 (double x){
- return (5+2*(x-3)*(x-3));
- }
- static double f (double x){
- return ((x-2)*(x-2)+4); //Parabola
- }
- static void Min_Search_Golden_Section(double a, double b, double tolerancia){
- double mu = 0.382; // 1/gama^2
- double x1, x2;
- double fx1, fx2;
- int n = 2; // numero de pontos x calculados
- DecimalFormat df2 = new DecimalFormat(".###");
- x1 = a + mu * (b - a);
- x2 = b - mu * (b - a);
- fx1 = f(x1);
- fx2 = f(x2);
- System.out.println("n\t x1 \t x2 \t a \t b \tf(a) \tf(b)");
- System.out.println(n+"\t"+df2.format(x1)+"\t"+df2.format(x2)+"\t"+df2.format(a)+"\t"+df2.format(b)+"\t"+df2.format(f(a))+"\t"+df2.format(f(b)));
- while ((b - a) > tolerancia && x1<x2) {
- n++;
- if (fx1 > fx2) {
- a = x1; // Descartar intervalo de a a x1
- x1 = x2;
- fx1 = fx2;
- //x2 = b - mu* (b - a); // Determinar novo ponto
- x2 = b + (a-x2);
- fx2 = f(x2);
- } else if (fx2>fx1){
- b = x2; // Descartar intervalo de x2 a b
- x2 = x1;
- fx2 = fx1;
- //x1 = a + mu*(b - a); // Determinar novo ponto
- x1 = a + (b-x1);
- fx1 = f(x1);
- } else {
- n++;
- b = x2; // Descartar intervalo de x2 a b e de a a x1
- a = x1;
- x2 = b - mu * (b - a);
- x1 = a + mu * (b - a);
- fx1 = f(x1);
- fx2 = f(x2);
- }
- System.out.println(n+"\t"+df2.format(x1)+"\t"+df2.format(x2)+"\t"+df2.format(a)+"\t"+df2.format(b)+"\t"+df2.format(f(a))+"\t"+df2.format(f(b)));
- }
- System.out.println("Resultado final "+n+"\t"+df2.format(x1)+"\t"+df2.format(x2)+"\t"+df2.format(f(a))+"\t"+df2.format(f(b)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement