Post on 06-Jan-2016
description
Ingeniørhøjskolen i ÅrhusSlide 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
Ingeniørhøjskolen i ÅrhusSlide 2
Newton approximation Step1 - Forudsætninger
)x('f
)x(fxx
n
nn1n )x('f
)x(fxx
n
nn1n
• 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:
Ingeniørhøjskolen i ÅrhusSlide 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!
Ingeniørhøjskolen i ÅrhusSlide 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.
{
}
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.
{
}
Ingeniørhøjskolen i ÅrhusSlide 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
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
Ingeniørhøjskolen i ÅrhusSlide 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;
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; }
}
)x('f
)x(fxx
n
nn1n )x('f
)x(fxx
n
nn1n
Ingeniørhøjskolen i ÅrhusSlide 7
Newton approximationStep 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);
}6x6x3)x('f
8x6x3x)x(f
2
23
double testFktPrime( double x )
{
return (3*x*x + 6*x - 6);
}
double testFktPrime( double x )
{
return (3*x*x + 6*x - 6);
}
Ingeniørhøjskolen i ÅrhusSlide 8
Newton approximationStep 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;
}
Ingeniørhøjskolen i ÅrhusSlide 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
Ingeniørhøjskolen i ÅrhusSlide 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