Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created by SharpDevelop.
- * User: itis15
- * Date: 20/02/2017
- * Time: 09:13
- *
- * To change this template use Tools | Options | Coding | Edit Standard Headers.
- */
- using System;
- namespace bisezione
- {
- class Program
- {
- public static double f( double x )
- {
- double ff = x*x*x - 2*(x*x) +x -4;
- return(ff);
- }
- public static double fprimo( double x )
- {
- double ff = 3*x*x - 4*x +1;
- return(ff);
- }
- public static double fsecondo( double x )
- {
- double ff = 6*x - 4 ;
- return(ff);
- }
- public static void Main(string[] args)
- {
- // Funzione f(x) di cui trovare lo zero
- Console.WriteLine("Soluzione dell'equazione: x^3-2x^2+x-4 col metodo di newton");
- double a, b, ak, bk, xk, fa, fb, estremo, f1estremo, festremo, f2a, f2b, fx, largh, eps;
- int cont;
- // Parametri iniziali
- Console.WriteLine("Inserire l'estremo sinistro dell'intervallo (a)");
- a=Convert.ToDouble(Console.ReadLine());
- Console.WriteLine("Inserire l'estremo destro dell'intervallo (b)");
- b=Convert.ToDouble(Console.ReadLine());
- Console.WriteLine("Tolleranza nel calcolo dello zero (eps)");
- eps=Convert.ToDouble((Console.ReadLine()));
- // Check sull'intervallo iniziale
- if ( f( a ) * f( b ) > 0.0 )
- {
- Console.WriteLine("Errore! L\'intervallo [" + a + "," + b + "] non contiene uno zero!");
- }
- else {
- ak=a;
- bk=b;
- estremo=0;
- f2a= fsecondo(ak);
- f2b= fsecondo(bk);
- fa = f(ak);
- fb = f(bk);
- if ( fa * f2a > 0.0 ) estremo = ak;
- if ( fb * f2b > 0.0 ) estremo = bk;
- cont = 0;
- largh = Math.Abs(ak - bk);
- Console.WriteLine( "cont \t ak \t bk \t xk \t largh");
- do
- {
- f1estremo=fprimo(estremo);
- festremo=f(estremo);
- xk = estremo-(festremo/f1estremo);
- Console.WriteLine(cont + "t" + ak + "\t" + bk + "\t" + xk + "\t" + largh);
- fx = f(xk);
- if (fx == 0.0)
- {
- Console.WriteLine( "Zero della funzione in: " + xk + "+- 0.0");
- Console.WriteLine( "Numero di iterazioni: " + cont);
- break;
- }
- else
- {
- fa = f(ak);
- fb = f(bk);
- f2a= fsecondo(ak);
- f2b= fsecondo(bk);
- if ( fa * f2a > 0.0 ) {
- estremo = ak;
- ak=xk;
- }
- if ( fb * f2b > 0.0 ) {
- estremo = bk;
- bk=xk;
- }
- largh = Math.Abs( bk -ak );
- cont++;
- }
- } while (largh > eps);
- f1estremo=fprimo(estremo);
- festremo=f(estremo);
- xk = estremo-(festremo/f1estremo);
- Console.WriteLine( cont + "\t" + ak + "\t" + bk + "\t" + xk + "\t" + largh);
- Console.WriteLine( "Zero della funzione in: " + xk + " +- " + 0.5 * largh);
- Console.WriteLine( "Valore della funzione nello zero: " + f( xk ));
- Console.WriteLine( "Numero di iterazioni necessarie per il calcolo dello zero con la tolleranza voluta: " + cont);
- }
- Console.Write("Press any key to continue . . . ");
- Console.ReadKey(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement