# Untitled

unknown
plain_text
a year ago
3.2 kB
2
Indexable
Never
``````package com.company;

import java.math.RoundingMode;
import java.text.DecimalFormat;

private double a = -2, b = 3, dokladnosc = 0.0001;
double x_sr = (a + b) / 2;
double L = b - a;

private double f(double x) {
//return (1.0/3) * Math.pow(x, 3) + 2.05 * Math.pow(x, 2) - 6.36*x + 5;
//return x*x*x - 3 * x * x - 45 * x + 10;
//return Math.pow((100-x), 2);
return -0.2 * x*x*x + 9.9 *x*x + 7.1*x - 2.5;
}

public static void main(String[] args) {
}

private double x1() {
return a + L/4;
}

private double x2() {
return b - L/4;
}

private void liczMaks() {
int iteracja = 1;

System.out.println("xsr: " + x_sr + ", f(xsr): " + f(x_sr) + ", L: " + L);

while (true) {

// krok 2
double x1 = x1(), x2 = x2();

if (f(x1) > f(x_sr)) {
b = x_sr;
x_sr = x1;
} else {
// krok 4.
if (f(x2) > f(x_sr)) {
a = x_sr;
x_sr = x2;

// przejdz do kroku 5
} else {
a = x1;
b = x2;
// przejdz do kroku 5
}
}
System.out.println("x1: " + x1);

System.out.println("Iteracja " + iteracja + ", wynik: " + x_sr);

L = b - a;
System.out.println("x1: " + x1 + " x2:" + x2 + " f(x1):" + f(x1) + " f(x2):" + f(x2) + " L:" + L);
System.out.println("(" + a + "; " + b + ")");

iteracja++;
// krok 5
System.out.println("Maksimum wynosi: " + x_sr);
break;
}
System.out.println();

}
}

private void liczMinimum() {
int iteracja = 1;

System.out.println("xsr: " + x_sr + ", f(xsr): " + f(x_sr) + ", L: " + L);

while (true) {
// krok 2
double x1 = x1(), x2 = x2();

if (f(x1) < f(x_sr)) {
b = x_sr;
x_sr = x1;
} else {
// krok 4.
if (f(x2) < f(x_sr)) {
a = x_sr;
x_sr = x2;

// przejdz do kroku 5
} else {
a = x1;
b = x2;
// przejdz do kroku 5
}
}

System.out.println("Iteracja " + iteracja);

L = b - a;
System.out.println("x1: " + x1 + " x2:" + x2 + " f(x1):" + f(x1) + " f(x2):" + f(x2) + " L:" + L);
System.out.println("(" + a + "; " + b + ")");

iteracja++;

// krok 5