metoda simpsona

0
Proszę o pomoc z fragmentem kodu

package metod_simpsona;
import java.util.Scanner;

public class Metod_simpsona {
 
private static double funkcja(double x) {
    double wynik;
   wynik =  x*x+2*x;
return wynik;
}
 
public static void main(String[] args) {
Scanner k = new Scanner(System.in);
System.out.println("Podaj dolna granice: ");
double a = k.nextDouble();

System.out.println("Podaj gorna granice: ");
double b = k.nextDouble();

System.out.println("Podaj ilosc wezlow: "); 
double wezly =k.nextDouble(); 
double h = (b - a) / wezly;
 System.out.println("Dolna granica wynosi: "+a+" Gorna granica wynosi: "+b+" Wezly wynosza: "+wezly);
 System.out.println("H wynosi: "+h); 
double c = 0;
double t = 0;

for (int i=1; i<wezly; i++) 
{
	double x = a + i * h;
	t += funkcja(x - h / 2); __**// DLACZEGO W TYM MOMENCIE KODU JEST x-h**__
	c += funkcja(x);
}
	
t += funkcja(b - h / 2);
System.out.println((h/6) * (funkcja(a) + funkcja(b) + 2*c + 4*t));
	
}
}
0

package metoda_simpsona;
import java.util.Scanner;

public class Metoda_simpsona {
 
private static double funkcja(double x) {
    double wynik;
   wynik =  x*x+2*x;
return wynik;
}
 
public static void main(String[] args) {
Scanner k = new Scanner(System.in);
System.out.println("Podaj dolna granice: ");
double a = k.nextDouble();

System.out.println("Podaj gorna granice: ");
double b = k.nextDouble();

System.out.println("Podaj ilosc wezlow: "); 
double wezly =k.nextDouble(); 
double h = (b - a) / wezly;
 System.out.println("Dolna granica wynosi: "+a+" Gorna granica wynosi: "+b+" Wezly wynosza: "+wezly);
 System.out.println("H wynosi: "+h); 
double c = 0;
double t = 0;

for (int i=1; i<wezly; i++) 
{
	double x = a + i * h;
	t += funkcja(x - h / 2); __**// DLACZEGO W TYM MOMENCIE KODU JEST x-h**__
	c += funkcja(x);
}
	
t += funkcja(b - h / 2);
System.out.println((h/6) * (funkcja(a) + funkcja(b) + 2*c + 4*t));
	
}
}

Proszę, poprawiłem go.

0

Kod działał poprawnie, chodziło mi bardziej o wyjaśnienie jeden linki kodu

for (int i=1; i<wezly; i++) 
{
    double x = a + i * h;
    t += funkcja(x - h / 2); __**// DLACZEGO W TYM MOMENCIE KODU JEST x-h**__
    c += funkcja(x);
}
0

http://pl.wikipedia.org/wiki/Metoda_Simpsona
Przy czym w wiki h=x2-x1=x1-x0, natomiast w Twoim kodzie h=x2-x0 i dlatego jest brane wszędzie h/2.

1 użytkowników online, w tym zalogowanych: 0, gości: 1