To Solve a Differential Equation Using Modified Euler's Method - C Data Structure

C examples for Data Structure:Algorithm

Description

To Solve a Differential Equation Using Modified Euler's Method

Demo Code

#include<stdio.h>
#include<math.h>

#define MAX 50//from w ww.  j  a  v a 2  s  .  c o m

float euler(float p, float q){
  float r;
  r = p * p + q;
  return(r);
}

int main(){
  int i = 1, j, k;
  float x[MAX], y[MAX], store1[MAX], store2[MAX];
  float b, h, u, v, w;
  printf("\nFunction for calculation of slope: y' = x * x + y\n");
  
  x[0] = 2;
  b = 4;
  y[0] = 5;
  h = 7;
  
  store2[0] = y[0];
  while(x[i-1] < b) {
    w = 100.0;
    x[i] = x[i-1] + h;
    store1[i] = euler(x[i-1], y[i-1]);
    k = 0;
    while(w > 0.0001) {
      u = euler(x[i], store2[k]);
      v = (store1[i] + u)/2;
      store2[k+1] = y[i-1] + v * h;
      w = store2[k] - store2[k+1];
      w = fabs(w);
      k = k + 1;
    }
    y[i] = store2[k];
    i = i + 1;
  }
  printf("\nThe Values of X and Y are: \n");
  printf("\nX-values        Y-values\n");
  for(j=0; j < i; j++) {
    printf("%f        %f\n", x[j], y[j]);
  }
}

Result


Related Tutorials