Euler’s method is one of the methods to solve the ordinary
differential equation for first order equation.
The formula for Eulers method is given by:
yn+1=yn+
h * f((xn, yn)
where,
i=
1,2,3,4,……….
h =
step size
Source code for Euler’s method
#include<iostream>
#include<cmath>
using namespace std;
int main(){
float x,
X, y, n, h, m;
cout<<"Enter initial value of x & y, final value of x and
number of steps : "<<endl;
cin>>x>>y>>X>>n;
h = ( X -
x ) / n;
do{
cout<<"f("<<x<<") =
"<<y<<endl;
m = (
y - x ) / ( y + x );
y += m
* h;
x = x
+ h;
}while(x<=X);
return 0;
}
Source code for Modified Euler’s method
#include<iostream>
#include<cmath>
using namespace std;
int main(){
float x,
X, y, y1, Y, h, m, M;
cout<<"Enter
initial value of x & y, final value of x and stepping value :\n";
cin>>x>>y>>X>>h;
do{
cout<<"f("<<x<<") =
"<<y<<endl;
M = (
y - x ) / ( y + x ); m = M;
x = x
+ h;
Y = y;
do{
y1
= y;
y
= Y + m * h;
cout<<"f("<<x<<") =
"<<y<<endl;
m
= ( y - x ) / ( y + x );
m
= ( M + m ) / 2;
}while((abs(y1-y))>0.00004);
}while(x<X);
return 0;
}
No comments:
Post a Comment