Advertisement
Guest User

NEWTON

a guest
Feb 20th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. /*
  2. * Created by SharpDevelop.
  3. * User: itis15
  4. * Date: 20/02/2017
  5. * Time: 09:13
  6. *
  7. * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8. */
  9.  
  10.  
  11. using System;
  12.  
  13. namespace bisezione
  14. {
  15. class Program
  16. {
  17. public static double f( double x )
  18. {
  19. double ff = x*x*x - 2*(x*x) +x -4;
  20. return(ff);
  21. }
  22.  
  23. public static double fprimo( double x )
  24. {
  25. double ff = 3*x*x - 4*x +1;
  26. return(ff);
  27. }
  28.  
  29. public static double fsecondo( double x )
  30. {
  31. double ff = 6*x - 4 ;
  32. return(ff);
  33. }
  34.  
  35.  
  36. public static void Main(string[] args)
  37. {
  38. // Funzione f(x) di cui trovare lo zero
  39.  
  40. Console.WriteLine("Soluzione dell'equazione: x^3-2x^2+x-4 col metodo di newton");
  41.  
  42. double a, b, ak, bk, xk, fa, fb, estremo, f1estremo, festremo, f2a, f2b, fx, largh, eps;
  43. int cont;
  44.  
  45. // Parametri iniziali
  46.  
  47. Console.WriteLine("Inserire l'estremo sinistro dell'intervallo (a)");
  48. a=Convert.ToDouble(Console.ReadLine());
  49. Console.WriteLine("Inserire l'estremo destro dell'intervallo (b)");
  50. b=Convert.ToDouble(Console.ReadLine());
  51. Console.WriteLine("Tolleranza nel calcolo dello zero (eps)");
  52. eps=Convert.ToDouble((Console.ReadLine()));
  53.  
  54. // Check sull'intervallo iniziale
  55.  
  56. if ( f( a ) * f( b ) > 0.0 )
  57. {
  58. Console.WriteLine("Errore! L\'intervallo [" + a + "," + b + "] non contiene uno zero!");
  59. }
  60. else {
  61. ak=a;
  62. bk=b;
  63. estremo=0;
  64. f2a= fsecondo(ak);
  65. f2b= fsecondo(bk);
  66. fa = f(ak);
  67. fb = f(bk);
  68. if ( fa * f2a > 0.0 ) estremo = ak;
  69. if ( fb * f2b > 0.0 ) estremo = bk;
  70. cont = 0;
  71. largh = Math.Abs(ak - bk);
  72. Console.WriteLine( "cont \t ak \t bk \t xk \t largh");
  73.  
  74. do
  75. {
  76. f1estremo=fprimo(estremo);
  77. festremo=f(estremo);
  78. xk = estremo-(festremo/f1estremo);
  79. Console.WriteLine(cont + "t" + ak + "\t" + bk + "\t" + xk + "\t" + largh);
  80. fx = f(xk);
  81. if (fx == 0.0)
  82. {
  83. Console.WriteLine( "Zero della funzione in: " + xk + "+- 0.0");
  84. Console.WriteLine( "Numero di iterazioni: " + cont);
  85. break;
  86. }
  87. else
  88. {
  89. fa = f(ak);
  90. fb = f(bk);
  91. f2a= fsecondo(ak);
  92. f2b= fsecondo(bk);
  93. if ( fa * f2a > 0.0 ) {
  94. estremo = ak;
  95. ak=xk;
  96. }
  97. if ( fb * f2b > 0.0 ) {
  98. estremo = bk;
  99. bk=xk;
  100. }
  101. largh = Math.Abs( bk -ak );
  102. cont++;
  103. }
  104. } while (largh > eps);
  105.  
  106. f1estremo=fprimo(estremo);
  107. festremo=f(estremo);
  108. xk = estremo-(festremo/f1estremo);
  109. Console.WriteLine( cont + "\t" + ak + "\t" + bk + "\t" + xk + "\t" + largh);
  110. Console.WriteLine( "Zero della funzione in: " + xk + " +- " + 0.5 * largh);
  111. Console.WriteLine( "Valore della funzione nello zero: " + f( xk ));
  112. Console.WriteLine( "Numero di iterazioni necessarie per il calcolo dello zero con la tolleranza voluta: " + cont);
  113. }
  114.  
  115. Console.Write("Press any key to continue . . . ");
  116. Console.ReadKey(true);
  117. }
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement