100 likes | 276 Views
Newton approximation ”Oversæt” til algoritme. Fra numerisk metode til implementeret algoritme. - Step 1: Specificér forudsætninger. - Step 2: Angiv prototype. - Step 3: Specificér pre- og postconditions. - Step 4: Skriv pseudokode. - Step 5: Skriv kode. - Step 6: Test.
E N D
Newton approximation ”Oversæt” til algoritme Fra numerisk metode til implementeret algoritme - Step 1: Specificér forudsætninger - Step 2: Angiv prototype - Step 3: Specificér pre- og postconditions - Step 4: Skriv pseudokode - Step 5: Skriv kode - Step 6: Test
Newton approximation Step1 - Forudsætninger Forudsætninger: - Algoritmen – hvad gør den? - regneforskrift – her: - Funktionen f(x) - Funktionens 1. afledede f’(x) - Et godt “gæt” x0 som startværdi - Hvornår skal beregningen stoppe • En acceptabel fejlmargin • Et maksimalt antal iterationer (hvorfor?)
bool newton( double (*f)(double), double (*fPrime)(double), double guess), double acceptedError, int maxIterations, double & solution ) f() and fPrime() are function pointers! Newton approximation Step2 – Angiv prototype
Newton approximationStep 3 - Pre- og postconditions bool newton( double (*f)(double), double (*fPrime)(double), double guess), double acceptedError, int maxIterations, double & solution ) // Precondition: // f() er en differentiabel funktion med 1. afledet fPrime(). // maxIterations > 0, acceptedError > 0, fPrime(guess) != 0 // Postcondition: // Hvis der på maxIterations (eller mindre) er fundet en løsning x // således, at |f(x)| < acceptedError, sættes solution til x, og der // returneres true. I modsat sættes solution til 0 (garbage), og der // returneres false. { }
Newton approximationStep 4 - Pseudokode bool newton( double (*f)(double), double (*fPrime)(double), double guess, double acceptedError, int maxIterations, double & solution ) - definer nødvendige lokale variable - gør følgende: - foretag ny iteration (Newton) - sæt xN = xNPlus1 - beregn afvigelse - tæl antal iterationer 1 op - fortsæt sålænge: - afvigelse > acceptedError OG antal iterationer < maxIterationer - hvis afvigelse >= acceptedError - solution = 0 - returner false - ellers - solution = xN - returner true
Newton approximationStep 5 - Implementering bool newton( double (*f)(double), double (*fPrime)(double), double guess, double acceptedError, int maxIterations, double & solution ) // Precondition: // ...... { unsigned int iterations = 0; double xN = guess, xNPlus1, error; do{ xNPlus1 = xN - f(xN)/fPrime(xN); xN = xNPlus1; error = abs( f(xNPlus1) ); }while( error >= acceptedError && ++iterations < maxIterations ); if( error >= acceptedError) { solution = 0; returnfalse; } else { solution = xN; returntrue; } }
Newton approximationStep 6 - Test double testFkt( double x ) { return (x*x*x + 3*x*x - 6*x - 8); } double testFktPrime( double x ) { return (3*x*x + 6*x - 6); }
Newton approximationStep 6 - Test intmain() { const double INITIAL_GUESS = 1.5000; const double ACCEPTED_ERROR = 0.0001; const int MAX_ITERATIONS = 25; bool converges = false; double result = 0; converges = newton( testFkt, testFktPrime, INITIAL_GUESS, ACCEPTED_ERROR, MAX_ITERATIONS, result ); if( converges ) cout << "Result OK, approximated value = " << result << endl; else cout << "No convergence" << endl; }
Bemærk, at prototypen KUNNE have været: double newton( double (*f)(double), double (*fPrime)(double), double guess), double acceptedError, int maxIterations, bool & succes ) Hvad vil det betyde / kræve ? • Ændring af postconditions • Løsningen returneres direkte • Boolean svar “returneres” ved reference
Numeriske metoder - generelt • Brug af computer algoritmer til at (forsøge at) løse matematiske eller real-world problemer • Ting at tage stilling til: • Nøjagtighed • Tilstrækkelighed • Hastighed