y= sin(), part -01
(高顯忠, sjgau4311@gmail.com, 2011-01-27 17:23)
1樓
#include <cstdlib>
#include <iostream>
using namespace std;
// ----------------------------------------------
#include "bbb.h"
#define pi (4.0*atan(1.0))
// abs_diff(x, y)
double abs_diff(double x, double y)
{
//
return(fabs(x - y));
}
double rel_diff(double x, double y)
{
double ax, ay, max;
ax= fabs(x); ay= fabs(y);
max= ((ax<ay)?ay:ax);
if (max > 0.0) {
return(abs_diff(x, y)/max);
}
else {
//
return 0.0;
}
}
// ----------------------------------------------
int main()
{
double x1, y1, x2, x3, x4, x5;
double y2, y3, y4, y5, dx, dy, m1, m2;
// y= sin(x), --> cos(x)
x1= 1.0;
y1= cos(x1);
printf("\n y1= %.6lf \n", y1);
pause();
//
x2= 0.0;
x3= pi/2.0;
dx= (x3 - x2)/10.0;
for (;;) {
x4= x1-dx;
x5= x1+dx;
y4= sin(x4);
y5= sin(x5);
m1= (y5 - y4)/(x5 - x4);
// m2= ???
dx/= 2.0;
x4= x1-dx;
x5= x1+dx;
y4= sin(x4);
y5= sin(x5);
m2= (y5 - y4)/(x5 - x4);
if (rel_diff(m1, m2) < 1e-8) break;
dx= dx/2.0;
printf("\n dx= %.6le \n", dx);
pause();
}
// ----------------------------
printf("\n m2= %.8lf \n", m2);
pause();
return(0);
}// end of main()