Menu Close

Milne’s Method C++

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

float x[5],y[5],h;

float f(int i)
{
return x[i]-y[i]*y[i];
}

void corect()
{
y[4]=y[2]+(h/3)*(f(2)+4*f(3)+f(4));
cout<<setw(23)<<" ";
cout<<setprecision(4)<<setw(8)<<y[4];
cout<<setw(8)<<f(4)<<endl;
cout<<" "<<y[4]<<" "<<f(4);
}

int main()
{
float xr,aerr,yc;
int i;

cout<<"Enter the value of x0,xr,h allowed error"<<endl;
cin>>x[0]>>xr>>h>>aerr;

cout<<"enter the value of y[i],i=0,3"<<endl;
for(i=0;i<=3;i++)
cin>>y[i];
cout<<fixed;

for(i=1;i<=3;i++)
x[i]=x[0]+i*h;

cout<<setw(5)<<"x"<<setw(15)<<"Predicted";
cout<<setw(17)<<"Corrected"<<endl;

cout<<setw(11)<<"y"<<setw(10)<<"f";
cout<<setw(7)<<"y"<<setw(10)<<"f"<<endl;

while(1)
{
if(x[3] >= xr)
return 0;
x[4] = x[3]+h;
y[4] = y[0]+(4*h/3)*(2*(f(1)+f(3)-f(2)));

cout<<setw(6)<<setprecision(2)<<x[4];
cout<<setprecision(4)<<setw(8)<<y[4];
cout<<setw(4)<<f(4)<<endl;

corect();

while(1)
{
yc = y[4];
corect();

if(fabs(yc-y[4]) <= aerr)
break;
}

for(i=0;i<=3;i++)
{
x[i] = x[i+1];
y[i] = y[i+1];
}
}
}

Output

Enter the value of x0,xr,h allowed error
0 1 .2 .0001
enter the value of y[i],i=0,3
0 .02 .0795 .1762
x Predicted Corrected
y f y f
0.80 0.19990.7600
0.3081 0.7051
0.3081 0.7051 0.3045 0.7073
0.3045 0.7073 0.3046 0.7072
0.3046 0.7072 0.3046 0.7072
0.3046 0.7072 1.00 0.30370.9078
0.4632 0.7854
0.4632 0.7854 0.4551 0.7929
0.4551 0.7929 0.4556 0.7924
0.4556 0.7924 0.4555 0.7925
0.4555 0.7925

More Related Stuff