#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
float f(float x, float y)
{
return x+y*y;
}
int main()
{
float x0,y0,h,xn,x,y,k1,k2,k3,k4,k;
cout<<"Enter the values of x0,y0,h,xn"<<endl;
cin>>x0>>y0>>h>>xn;
x=x0;
y=y0;
cout<<fixed;
while(1)
{
if(x==xn)
break;
k1=h*f(x,y);
k2=h*f(x+h/2,y+k1/2);
k3=h*f(x+h/2,y+k2/2);
k4=h*f(x+h,y+k3);
k=(k1+(k2+k3)*2+k4)/6;
x += h;
y += k;
cout<<"when x="<<setprecision(4)<<setw(8)<<x;
cout<<endl<<"y="<<setw(8)<<y;
}
return 0;
}
Output
Enter the values of x0,y0,h,xn
0.0 1.0 0.2 0.2
when x= 0.2000
y= 1.2735
