/* C- Program for Euler's Method by farani */
/* Function is defined in line no. 51 and */
/* and can thereby be change if needed */
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
double a, b, alpha, h, t, w;
int i, N;
double F(double, double);
/* Input */
printf("This is Eulers Method.\n");
printf("The Function is: y'=y-t*t+1.\n");
printf("Please enter a: ");
scanf("%lf", &a);
printf("\nPlease enter b: ");
scanf("%lf", &b);
printf("\nPlease enter N(intervals): ");
scanf("%d", &N);
printf("\nPlease enter initial condition (alpha): ");
scanf("%lf", &alpha);
/* Initial Step */
h=(b-a)/N;
t=a;
w=alpha;
/* Table Headings */
printf("\n*************************\n");
printf("t_i\t\tw_i");
printf("\n*************************");
/* Iterations/Loop */
for (i=1;i<=N;i++)
{
w=w+h*F(t,w);
t=a+i*h;
printf("\n%lf\t%lf", t, w);
}
getchar();
getchar();
return 0;
}
/* Function definition and evaluation */
double F(double t, double y)
{
double f;
f=y-t*t+1.0;
return f;
}
A sample output of the program
No comments:
Post a Comment