#include <stdio.h>
#include <math.h>
double func(double x){
return x*x*x;
}
void threepoint(double xi[], double yi[], int N, double h);
void fivepoint(double xi[], double yi[], int N, double h);
void threepoint(double xi[], double yi[], int N, double h){
double dy;
for (int i=0;i<N+1;i++) {
if(i==0) {
dy = (-yi[2]+4*yi[1]-3*yi[0])/(2*h);
}else if(i==N) {
dy = (3*yi[N]-4*yi[N-1]+yi[N-2])/(2*h);
}else{
dy = (yi[i+1]-yi[i-1])/(2*h);
}
printf("y'%d = %f\n", i
+1, dy
); }
}
void fivepoint(double xi[], double yi[], int N, double h){
double dy;
for (int i=0;i<N+1;i++) {
if(i==0) {
dy = (-11*yi[0]+18*yi[1]-9*yi[2]+2*yi[3])/(6*h);
}else if(i==1){
dy = (-2*yi[0]-3*yi[1]+6*yi[2]-yi[3])/(6*h);
}
else if(i==N-1) {
dy = (yi[N-3]-6*yi[N-2]+3*yi[N-1]+2*yi[N])/(6*h);
}else{
dy = (-2*yi[i-3]+9*yi[i-2]-18*yi[i-1]+11*yi[i])/(6*h);
}
printf("y'%d = %f\n", i
+1, dy
); }
}
int main(void) {
double xl = 0;
double xr = 1;
double h = 0.1;
int N = (int)((xr - xl) / h);
double yi[N+1];
double xi[N+1];
xi[0] = xl;
yi[0] = func(xi[0]);
for (int i=1;i<N+1;i++) {
xi[i] = xi[i-1] + h;
yi[i] = func(xi[i]);
}
threepoint(xi, yi, N, h);
fivepoint(xi, yi, N, h);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+Cgpkb3VibGUgZnVuYyhkb3VibGUgeCl7CiAgICByZXR1cm4geCp4Kng7Cn0KCnZvaWQgdGhyZWVwb2ludChkb3VibGUgeGlbXSwgZG91YmxlIHlpW10sIGludCBOLCBkb3VibGUgaCk7CnZvaWQgZml2ZXBvaW50KGRvdWJsZSB4aVtdLCBkb3VibGUgeWlbXSwgaW50IE4sIGRvdWJsZSBoKTsKCnZvaWQgdGhyZWVwb2ludChkb3VibGUgeGlbXSwgZG91YmxlIHlpW10sIGludCBOLCBkb3VibGUgaCl7CiAgICBkb3VibGUgZHk7CiAgICBwcmludGYoIu+8keasoeW+ruS/guaVsO+8mu+8k+eCuVxuIik7CiAgICAKICAgIGZvciAoaW50IGk9MDtpPE4rMTtpKyspIHsKICAgICAgICBpZihpPT0wKSB7CiAgICAgICAgICAgIGR5ID0gKC15aVsyXSs0KnlpWzFdLTMqeWlbMF0pLygyKmgpOwogICAgICAgIH1lbHNlIGlmKGk9PU4pIHsKICAgICAgICAgICAgZHkgPSAoMyp5aVtOXS00KnlpW04tMV0reWlbTi0yXSkvKDIqaCk7CiAgICAgICAgfWVsc2V7CiAgICAgICAgICAgIGR5ID0gKHlpW2krMV0teWlbaS0xXSkvKDIqaCk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigieSclZCA9ICVmXG4iLCBpKzEsIGR5KTsKICAgIH0KICAgIHByaW50ZigiXG4iKTsKfQoKdm9pZCBmaXZlcG9pbnQoZG91YmxlIHhpW10sIGRvdWJsZSB5aVtdLCBpbnQgTiwgZG91YmxlIGgpewoJZG91YmxlIGR5OwoJcHJpbnRmKCLvvJHmrKHlvq7kv4LmlbDvvJrvvJXngrlcbiIpOwoJCiAgICBmb3IgKGludCBpPTA7aTxOKzE7aSsrKSB7CiAgICAgICAgaWYoaT09MCkgewogICAgICAgICAgICBkeSA9ICgtMTEqeWlbMF0rMTgqeWlbMV0tOSp5aVsyXSsyKnlpWzNdKS8oNipoKTsKICAgICAgICB9ZWxzZSBpZihpPT0xKXsKICAgICAgICAJZHkgPSAoLTIqeWlbMF0tMyp5aVsxXSs2KnlpWzJdLXlpWzNdKS8oNipoKTsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZihpPT1OLTEpIHsKICAgICAgICAgICAgZHkgPSAoeWlbTi0zXS02KnlpW04tMl0rMyp5aVtOLTFdKzIqeWlbTl0pLyg2KmgpOwogICAgICAgIH1lbHNlewogICAgICAgICAgICBkeSA9ICgtMip5aVtpLTNdKzkqeWlbaS0yXS0xOCp5aVtpLTFdKzExKnlpW2ldKS8oNipoKTsKICAgICAgICB9CiAgICAgICAgcHJpbnRmKCJ5JyVkID0gJWZcbiIsIGkrMSwgZHkpOwogICAgfQogICAgcHJpbnRmKCJcbiIpOwp9CgppbnQgbWFpbih2b2lkKSB7CiAgICBkb3VibGUgeGwgPSAwOwogICAgZG91YmxlIHhyID0gMTsKICAgIGRvdWJsZSBoID0gMC4xOwogICAgCiAgICBpbnQgTiA9IChpbnQpKCh4ciAtIHhsKSAvIGgpOyAKICAgIAogICAgZG91YmxlIHlpW04rMV07ICAKICAgIGRvdWJsZSB4aVtOKzFdOyAKCiAgICB4aVswXSA9IHhsOwogICAgeWlbMF0gPSBmdW5jKHhpWzBdKTsKICAgIAogICAgZm9yIChpbnQgaT0xO2k8TisxO2krKykgewogICAgICAgIHhpW2ldID0geGlbaS0xXSArIGg7CiAgICAgICAgeWlbaV0gPSBmdW5jKHhpW2ldKTsKICAgIH0KICAgIAogICAgdGhyZWVwb2ludCh4aSwgeWksIE4sIGgpOwogICAgZml2ZXBvaW50KHhpLCB5aSwgTiwgaCk7CgogICAgcmV0dXJuIDA7Cn0K