Præsentation er lastning. Vent venligst

Præsentation er lastning. Vent venligst

Ingeniørhøjskolen i Århus Slide 1 Newton approximation ”Oversæt” til algoritme - Step 5: Skriv kode - Step 4: Skriv pseudokode - Step 3: Specificér pre-

Lignende præsentationer


Præsentationer af emnet: "Ingeniørhøjskolen i Århus Slide 1 Newton approximation ”Oversæt” til algoritme - Step 5: Skriv kode - Step 4: Skriv pseudokode - Step 3: Specificér pre-"— Præsentationens transcript:

1 Ingeniørhøjskolen i Århus Slide 1 Newton approximation ”Oversæt” til algoritme - Step 5: Skriv kode - Step 4: Skriv pseudokode - Step 3: Specificér pre- og postconditions Fra numerisk metode til implementeret algoritme - Step 1: Specificér forudsætninger - Step 2: Angiv prototype - Step 6: Test

2 Ingeniørhøjskolen i Århus Slide 2 Newton approximation Step1 - Forudsætninger Et maksimalt antal iterationer (hvorfor?) En acceptabel fejlmargin - 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 Forudsætninger:

3 Ingeniørhøjskolen i Århus Slide 3 Newton approximation Step2 – Angiv prototype bool newton( double (*f)(double), double (*fPrime)(double), double guess), double acceptedError, int maxIterations, double & solution ) bool newton( double (*f)(double), double (*fPrime)(double), double guess), double acceptedError, int maxIterations, double & solution ) f() and fPrime() are function pointers! f() and fPrime() are function pointers!

4 Ingeniørhøjskolen i Århus Slide 4 Newton approximation Step 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. { } 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. { }

5 Ingeniørhøjskolen i Århus Slide 5 Newton approximation Step 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 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

6 Ingeniørhøjskolen i Århus Slide 6 Newton approximation Step 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; return false; } else {solution = xN; return true; } } 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; return false; } else {solution = xN; return true; } }

7 Ingeniørhøjskolen i Århus Slide 7 Newton approximation Step 6 - Test double testFkt( double x ) { return (x*x*x + 3*x*x - 6*x - 8); } 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); } double testFktPrime( double x ) { return (3*x*x + 6*x - 6); }

8 Ingeniørhøjskolen i Århus Slide 8 Newton approximation Step 6 - Test int main() { 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; } int main() { 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; }

9 Ingeniørhøjskolen i Århus Slide 9 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

10 Ingeniørhøjskolen i Århus Slide 10 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


Download ppt "Ingeniørhøjskolen i Århus Slide 1 Newton approximation ”Oversæt” til algoritme - Step 5: Skriv kode - Step 4: Skriv pseudokode - Step 3: Specificér pre-"

Lignende præsentationer


Annoncer fra Google