You are on page 1of 1

#include<stdio.

h>
#include<math.h>
/*Program to solve given Ordinary Differential Equation using Implicit Euler
*method*/
//To calculate Function F
float F(float t)
{return 10-(10+t)*exp(-t);
}//end of float F()

float derF(float t)
{//Finding Derivative of F
float a=0.001;
float d=(F(t+a)-F(t-a))/(2*a);
return d;
}//end of derF()

float dery(float t,float y)


{//Finding the derivative of y at t
float dy=-200*(y-F(t))+derF(t);
return dy;
}//end of dery()

int main()
{//accepting values from the user
float h,t,tf,ti,y;
printf("Enter initial value of t(t0):");
scanf("%f",&ti);
printf("Enter step size:");
scanf("%f",&h);
printf("Enter value of t for which y is calculated:");
scanf("%f",&tf);
float an=10-(10+tf)*exp(-tf)+10*exp(-200*tf);
printf("Enter initial value of y:");
scanf("%f",&y);

//Solving the ODE using IMplicit Euler and printing the table
float yp=y;float yc=y;int i=0;t=ti;
printf("i\t x\tF(x) \tF'(x)\typ\tyc");
while(t<tf)
{printf("\n%d\t%.3f\t%.2f\t%.2f\t%.2f\t%.2f",i,t,F(t),derF(t),yp,yc);
yp=yc+h*dery(t,yc);
yc=yc+h*dery(t+h,yp);
i++;t+=h;
}
//Displaying the Results
printf("\n%d\t%.3f\t%.2f\t%.2f\t%.2f\t%.2f",i,t,F(t),derF(t),yp,yc);
printf("\nResult(From Implicit Euler)=%f",yc);
//Finding the result from explicit Euler's method
float ye=y;t=ti;
while(t<tf)
{ye+=h*dery(t,ye);
t+=h;
}
printf("\nResult(From Explicit Euler)=%f",ye);
printf("\nAnalytical Result=%f",an);
}//end of main()

You might also like