15. Write a program to demonstrate Milne Simpson Method.
/*Program to execute Milne Simpson Method*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
float dif(float x, float y)
{
return x + y;
}
float pred(float y0, float h, float f1, float f2, float f3)
{
return y0 + 4 * h * (2 * f1 - f2 + 2 * f3) / 3;
}
float corr(float y2, float h, float f2, float f3, float f4)
{
return y2 + h * (f2 + 4 * f3 + f4) / 3;
}
void main()
{
float x, h, x0, x1, y0, y1, y2, y3, y4, x4, f1, f2, f3, f4;
float aerr, yc, yc1;
printf("Enter the values of x, h, x0, aerr: ");
scanf("%f%f%f%f", &x, &h, &x0, &aerr);
printf("Enter the values of y0, y1, y2, y3: ");
scanf("%f%f%f%f", &y0, &y1, &y2, &y3);
while (x4 < x)
{
f1 = dif(x0 + h, y1);
f2 = dif(x0 + 2 * h, y2);
f3 = dif(x0 + 3 * h, y3);
printf("\n\nf1=%f\tf2=%f\tf3=%f\n", f1, f2, f3);
y4 = pred(y0, h, f1, f2, f3);
x4 = x0 + 4 * h;
f4 = dif(x4, y4);
printf("\nPredicator values:");
printf("\n\ty=%f\tf4=%f", y4, f4);
y4 = corr(y2, h, f2, f3, f4);
f4 = dif(x4, y4);
printf("\nCorrector values:");
printf("\n\ty=%f\tf4=%f", y4, f4);
yc = corr(y2, h, f2, f3, f4);
f4 = dif(x4, yc);
printf("\n\ty=%f\tf4=%f", yc, f4);
while (fabs(yc - y4) > aerr)
{
y4 = yc;
yc = corr(y2, h, f2, f3, f4);
f4 = dif(x4, yc);
printf("/n/ty=%f\tf4=%f", yc, f4);
}
x0 += h;
printf("\nValues of y=%f, when x=%f", yc, x4);
y0 = y1;
y1 = y2;
y2 = y3;
y3 = y4;
}
getch();
}
Output
Enter the values of x, h, x0, aerr: 0.3 0.05 0 0.0001
Enter the values of y0, y1, y2, y3: 1 1.0525 1.1103 1.1736
f1 = 1.102500 f2 = 1.210300 f3 = 1.323600
Predictor Values:
y = 1.242793 f4 = 1.442793
Corrector Values:
y = 1.242758 f4 = 1.442758
y = 1.242758 f4 = 1.442758
Value of y = 1.242758, when x = 0.200000
f1 = 1.210300 f2 = 1.323600 f3 = 1.442758
Predictor Values:
y = 1.318001 f4 = 1.568001
Corrector Values:
y = 1.317977 f4 = 1.567977
y = 1.317977 f4 = 1.567977
Value of y = 1.317977, when x = 0.250000
f1 = 1.323600 f2 = 1.442758 f3 = 1.567977
Predictor Values:
y = 1.3399660 f4 = 1.699660
Corrector Values:
y = 1.399664 f4 = 1.699664
y = 1.399664 f4 = 1.699664
Value of y = 1.399664, when x = 0.300000