Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package metoda_simpsona;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11. *
  12. * @author Student
  13. */
  14. public class Metoda_Simpsona {
  15.  
  16.  
  17. public static double caleczka (double x)
  18. {
  19. return Math.sin(1.1*x-0.3)/(2.5 + Math.pow(x, 2));
  20.  
  21. }
  22. /**
  23. * @param args the command line arguments
  24. */
  25. public static void main(String[] args) {
  26. // TODO code application logic here
  27. double a=0.4;
  28. double b=2.2;
  29. double wyn,h;
  30. int n ;
  31.  
  32. Scanner el_wejsciowy =new Scanner(System.in);
  33. System.out.println("Podaj n");
  34. n=el_wejsciowy.nextInt();
  35.  
  36. h=(b-a)/n;
  37.  
  38. double [] x = new double[n+1];
  39.  
  40.  
  41. for (int i=0;i<x.length;i++)
  42. {
  43. x[i]= (a + (i*(b-a)/n));
  44. }
  45. double [] y = new double[n];
  46. for (int i=0;i<y.length;i++)
  47. {
  48. y[i]= (x[i] + x[i+1])/2;
  49. }
  50.  
  51. wyn= 0;
  52. for(int i=0;i< x.length;i++)
  53. {
  54. if(i==(x.length-1))
  55. {
  56. wyn += caleczka(x[i]);
  57. }
  58. else {
  59. wyn += caleczka(x[i])*2;
  60. }
  61. }
  62. for (int i=0;i<y.length;i++)
  63. {
  64. wyn += caleczka(y[i])*4;
  65. }
  66. wyn *=(h/6);
  67. System.out.println("Calka wynosi:" + wyn);
  68. }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement