Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.Math.*;
- class liniowa implements Function{
- public double getValue(double x){
- return x-5;
- }
- }
- class kwadratowa implements Function{
- public double getValue(double x){
- return x*2*x+x-8;
- }
- }
- class wielomianowa implements Function{
- public double getValue(double x)
- {
- return 4*x*x*x*x+3*x*x*x+2*x*x-3*x+8;
- }
- }
- class bisekcja{
- static double E = 0.00000001;
- public static double oblicz(double a, double b, Function f){
- double x0;
- while(Math.abs(a - b) > E)
- {
- x0 = (a + b) / 2;
- if(f.getValue(x0) == 0)
- break;
- else if((f.getValue(a) * f.getValue(x0) < 0) && (f.getValue(x0) * f.getValue(b) > 0))
- b = x0;
- else if((f.getValue(x0) * f.getValue(a) > 0) && (f.getValue(x0) * f.getValue(b)) < 0)
- a =x0;
- else
- break;
- }
- return (a + b) / 2;
- }
- public static void main(String[] args){
- double x;
- double a = Double.parseDouble(args[1]);
- double b = Double.parseDouble(args[2]);
- if(args[0].equals("liniowa"))
- {
- Function f = new liniowa();
- x = oblicz(a, b, f);
- System.out.println("Pierwiastek z funkcji: " + args[0].toString() + " wynosi: " + x);
- }
- else if(args[0].equals("kwadratowa"))
- {
- Function f = new kwadratowa();
- x = oblicz(a, b, f);
- System.out.println("Pierwiastek z funkcji: " + args[0].toString() + " wynosi: " + x);
- }
- else if(args[0].equals("wielomianowa"))
- {
- Function f = new wielomianowa();
- x = oblicz(a, b, f);
- System.out.println("Pierwiastek z funkcji: " + args[0].toString() + " wynosi: " + x);
- }
- else
- System.out.println("Brak/bledne argumenty");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement