1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string>
#define JMAX 20
using namespace std;
void exponentielle(float x, float* fl, float* df)
{
*fl = exp(x);
*df = (exp(x + 0.01) - exp(x - 0.01)) / 0.02;
}
float rtnewt (void (*func)(float,float *,float *),float x1,float x2,float xacc){
int j;
float df,dx,f,rtn;
rtn=0.5*(x1+x2);
for(j=1;j<=JMAX;j++){
(*func)(rtn,&f,&df);
dx=f/df;
if((x1-rtn)*(rtn-x2)<0.0)
cout<<"e";
if (fabs(dx)<xacc) return rtn;
}
return 0;
}
int main(){
float x1=-10;
float x2=10;
float prec=0.001;
float x=1;
float* y=0;
float* z=0;
rtnewt(&exponentielle(x,y,z),x1,x2,prec);
} |
Partager