Improved Euler's Method and Euler's Method with their C Program

Improved Euler’s Method


The Improved Euler’s method, also known as the Heun formula or the average slope method, gives a more accurate approximation than the Euler rule and gives an explicit formula for computing yn+1. The basic idea is to correct some errors of the original Euler's method. The syntax of the Improved Euler’s method is similar to that of the trapezoid rule, but the y value of the function in terms of yn+1 consists of the sum of the y value and the product of h and the function in terms of xn and yn.


Improved Euler formula or the average slope method is commonly referred to as the Heun method:

yn+1=yn
+h/2[f(xn,yn)+f(xn+1,yn+hf(xn,yn))],n=0,1,2,….

Since it is actually the simplest version of the predictor-corrector method, the recurrence can be written as

pn+1
yn+1=yn+hf(xn,yn),
yn+1
=yn+h/2[f(xn,yn)+f(xn+1,pn+1)],n=0,1,2,….


C program to find the value using Heun's method


#include<stdio.h>
#include<math.h>
#include<conio.h>
#define f(x,y) 2*(x)+(y)

int main(){
 float x,xp,x0,y0,y,h,m1,m2;
 printf("\n Heuns Method\n");
 printf("\nEnter initial values of x and y\n");
 scanf("%f%f",&x0,&y0);
 printf("\nEnter x at which function to be evaluated\n");
 scanf("%f",&xp);
 printf("\nEnter the step size \n");
 scanf("%f",&h);
 y=y0;
 x=x0;
 for(x=x0;x<xp;x=x+h){
  m1=f(x,y);
  printf("\nm1=%f",m1);
  m2=f(x+h,y+(h*m1));
  printf("\nm2=%f",m2);
  y=y+h/2*(m1+m2);
  printf("\ny=%f",y);
 }
 
 printf("Function value at x=%f is %f\n",xp,y);
 getch();
 return 0; 
 
}


Euler's Method

Euler's Method assumes our solution is written in the form of a Taylor’s Series.
That is, we'll have a function of the form:

This gives us a reasonably good approximation if we take plenty of terms, and if the value of h is reasonably small.
For Euler's Method, we just take the first 2 terms only.

The last term is just h times our dx/dy expression, so we can write Euler's Method as follows:

How do we use this formula?

We start with some known value for y, which we could call y0. It has this value when X=x0. (We make use of the initial value (x0,Y0)).
The result of using this formula is the value for y, one h step to the right of the current value. Let's call it Y1So we have:

where
Y1 is the next estimated solution value;
Y0 is the current value;
h is the interval between steps; and
f(x0,y0) is the value of the derivative at the starting point,(x0,y0).

Next value: 
To get the next value Y2, we would use the value we just found for Y1 as follows:

Where
Y2 is the next estimated solution value;
Y1 is the current value;
h is the interval between steps;
X1=X0+h; and
f(x1,y1) is the value of the derivative at the current (x1,y1) point.
We continue this process for as many steps as required.


C Program for Euler's Method


#include<stdio.h>
#include<math.h>
#include<conio.h>
#define f(x,y) 2*(x)+(y)
//#define f(x,y) 3*(x)*(x)+1

int main(){
 float x,xp,x0,y0,y,h,m1,m2,m3,m4;
 printf("\n Euler's method'\n");
 printf("\nEnter initial values of x and y\n");
 scanf("%f%f",&x0,&y0);
 printf("\nEnter x at which function to be evaluated\n");
 scanf("%f",&xp);
 printf("\nEnter the step size \n");
 scanf("%f",&h);
 y=y0;
 x=x0;
 for(x=x0;x<xp;x=x+h){
  m1=f(x,y);
  printf("\nm1=%f",m1);
  y=y+h*(m1);
  printf("\ny=%f",y);
 }
 
 printf("Function value at x=%f is %f\n",xp,y);
 getch();
 return 0; 
 
}

Comments

Popular posts from this blog

C Program for SCAN Disk Scheduling Algorithm | C Programming

C program to Find Cartesian Product of Two Sets | C programming

C Program To Check The String Is Valid Identifier Or Not | C Programming