3. 3章の答え・1

(以下では、開発環境が手元にない場合は、paiza.io, onlineGDB, programiz, codechef.com, ideone.com, paiza.ioなどを実行してみると良い。)

#include <stdio.h>
#include <math.h>
#define N 100
#define Xs 0
#define Xe 3.0
double f(double x,double y);
double analytical(double x);
int main(void){
  double h=(Xe-Xs)/N;
  double x=0.0;
  double y=4.0;
  for(double i=0;i<N;i++){
    y=y+h*f(x,y);
    x=x+h;
    printf("%4.4lf\t %4.4lf\t  %4.4lf\n",x,y,analytical(x));
  }
}
double f(double x,double y){
  return x*y;
}
double analytical(double x){
  return 4.0*exp(x*x/2.0);
}