1 / 10

Newton approximation ”Oversæt” til algoritme

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.

gzifa
Download Presentation

Newton approximation ”Oversæt” til algoritme

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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?)

  3. 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

  4. 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. { }

  5. 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

  6. 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; } }

  7. 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); }

  8. 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; }

  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. 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

More Related