まんだげり No title
No License C
2020年01月18日
Copy Clone
#include<stdio.h>
#include<math.h>

double f(double x){
    return pow(x,2)+2*x+1;//define function
}

double h(double a,double b,int i){
    return (b-a)/pow(2,i-1);//define h
}

void trapezoid(double a,double b,double r[100][100],int n){
    //fuction of calculating trapezoidal rule
    int i,j;
    double sum = 0;
    for(i=1;i<=n;i++){
        for(j=1;j<=pow(2,i-2);j++){
            sum += f(a+(2*j-1)*h(a,b,i));
        }
        r[i][0] = (r[i-1][0]+h(a,b,i-1)*sum);   
    }
}
void richardson(double r[100][100],int n){
    //function of extrapolation of richardson
    int i,j;
    for(i=1;i<=n;i++){
        for(j=1;j<pow(2,i-1);j++){
            r[i][j] = r[i][j-1]+(r[i][j-1]-r[i-1][j-1])/pow(4,j-1)-1;
        }
    }
}

int main(){
    double a = 0.0;
    double b = 10.0;
    int n = 5;
    int i,j,k;
    double r[n][n];
    r[0][0] = h(a,b,1)/2*(f(a)+f(b));
    /*printf("%f\n",a);
    k = h(a,b,1);
    printf("%f\n",k);*/
    printf("%f\n",r[0][0]);//1
    trapezoid(a,b,r,n);
    printf("%f\n",r[0][0]);//2
    richardson(r,n);
    printf("%f\n",r[0][0]);//3
    for(i=1;i<=n;i++){
        for(j=0;j<=i;j++){
            printf("%f ",r[i][j]);
        }
        printf("\n");
    }
    return 0;
}
#include<stdio.h>
#include<math.h>

double f(double x){
    return pow(x,2)+2*x+1;//define function
}

double h(double a,double b,int i){
    return (b-a)/pow(2,i-1);//define h
}

void trapezoid(double a,double b,double r[100][100],int n){
    //fuction of calculating trapezoidal rule
    int i,j;
    double sum = 0;
    for(i=1;i<=n;i++){
        for(j=1;j<=pow(2,i-2);j++){
            sum += f(a+(2*j-1)*h(a,b,i));
        }
        r[i][0] = (r[i-1][0]+h(a,b,i-1)*sum);   
    }
}
void richardson(double r[100][100],int n){
    //function of extrapolation of richardson
    int i,j;
    for(i=1;i<=n;i++){
        for(j=1;j<pow(2,i-1);j++){
            r[i][j] = r[i][j-1]+(r[i][j-1]-r[i-1][j-1])/pow(4,j-1)-1;
        }
    }
}

int main(){
    double a = 0.0;
    double b = 10.0;
    int n = 5;
    int i,j,k;
    double r[n][n];
    r[0][0] = h(a,b,1)/2*(f(a)+f(b));
    /*printf("%f\n",a);
    k = h(a,b,1);
    printf("%f\n",k);*/
    printf("%f\n",r[0][0]);//1
    trapezoid(a,b,r,n);
    printf("%f\n",r[0][0]);//2
    richardson(r,n);
    printf("%f\n",r[0][0]);//3
    for(i=1;i<=n;i++){
        for(j=0;j<=i;j++){
            printf("%f ",r[i][j]);
        }
        printf("\n");
    }
    return 0;
}
No one still commented. Please first comment.