Professional Documents
Culture Documents
function F For Dy/dx F (X, Y) : C++ Code For Euler's Method
function F For Dy/dx F (X, Y) : C++ Code For Euler's Method
#include<iostream.h>
#include<math.h>
#include<fstream.h>
return x-y;
int main(){
ofstream fout;
float Xo = 0;
float Xn = 12;
float h;
int i,n;
cin>>h;
float X[n],Y[n];
X[0]=0; Y[0]=0;
X[i]=X[0]+i*h; //Calculating X
fout<<"\n"<<i<<"\t"<<X[i]<<"\t"<<Y[i]<<"\t"<<X[i]+exp(-1*X[i])-1;
fout<<"\n"<<"error:"<<"\t"<<fabs(Y[n-1]-(X[n-1]+exp(-1*X[n-1])-1));
return 0;}
C++ Code for Modified Euler’s Method
#include<iostream.h>
#include<math.h>
#include<fstream.h>
using namespace std;
//function f for dy/dx = f(x,y)
float Xo = 0;
float Xn = 12;
float h;
int i,n;
cout<<"enter step size";
cin>>h;
float N=(Xn-Xo)/h+1;
n=N;
float X[n],Y[n];
X[0]=0; Y[0]=0;
for(i=0; i<n; i++){
X[i]=X[0]+i*h;
}
for(i=1; i<n; i++){
Y[i]=Y[i-1]+ h*f(X[i-1], Y[i-1]); //Calculating Yp
Y[i]=Y[i-1]+ h*(f(X[i-1], Y[i-1])+f(X[i], Y[i]))/2; //Calculating Yc
fout<<"\n"<<i<<"\t"<<X[i]<<"\t"<<Y[i]<<"\t"<<X[i]+exp(-1*X[i])-1;
}
fout<<"\n"<<"error:"<<"\t"<<fabs(Y[n-1]-(X[n-1]+exp(-1*X[n-1])-1));
return 0;}
C++ Code for Range Kutta’s Method
#include<iostream.h>
#include<math.h>
#include<fstream.h>
using namespace std;
//function f for dy/dx = f(x,y)
float X[n],Y[n];
X[0]=0; Y[0]=0;
for(i=0; i<n; i++){
X[i]=X[0]+i*h;
}
for(i=1; i<n; i++){
k1=f(X[i-1],Y[i-1]);
k2=f((X[i-1]+h/2),(Y[i-1]+h*k1/2));
k3=f((X[i-1]+h/2),(Y[i-1]+h*k2/2));
k4=f((X[i-1]+h),(Y[i-1]+h*k3));
Y[i]=Y[i-1]+(k1+2*k2+2*k3+k4)*(h/6);
fout<<"\n"<<i<<"\t"<<X[i]<<"\t"<<Y[i]<<"\t"<<X[i]+exp(-1*X[i])-1;
} fout<<"\n"<<"error:"<<"\t"<<fabs(Y[n-1]-(X[n-1]+exp(-1*X[n-1])-1));
return 0;}
C++ Code for AM-4 Method
#include<iostream.h>
#include<math.h>
#include<fstream.h>
using namespace std;
//function f for dy/dx = f(x,y)
float Xo = 0;
float Xn = 12;
float h;
int i,n;
cout<<"enter step size";
cin>>h;
float N=(Xn-Xo)/h+1;
n=N+3;
float X[n],Y[n];
X[3]=0; Y[3]=0;
for(i=0; i<n; i++){
X[i]=X[3]+(i-3)*h;
}
for(i=0; i<3; i++){
Y[i]=X[i]+exp(-1*X[i])-1;
}
for(i=4; i<n; i++){
Y[i]=Y[i-1]+(h/24)*(55*f(X[i-1],Y[i-1])-59*f(X[i-2],Y[i-2])
+37*f(X[i-3],Y[i-3])-9*f(X[i-4],Y[i-4]));
Y[i]=Y[i-1]+(h/24)*(9*f(X[i],Y[i])+19*f(X[i-1],Y[i-1])
-5*f(X[i-2],Y[i-2])+f(X[i-3],Y[i-3]));
fout<<"\n"<<i-3<<"\t"<<X[i]<<"\t"<<Y[i]<<"\t"<<X[i]+exp(-1*X[i])-1;
}
fout<<"\n"<<"error:"<<"\t"<<fabs(Y[n-1]-(X[n-1]+exp(-1*X[n-1])-1));
return 0;
}
C++ Code for Milne’s Method
//6.1 Milne
#include<iostream.h>
#include<math.h>
#include<fstream.h>
using namespace std;
//function f for dy/dx = f(x,y)
float f(float x, float y){
return x-y;
}
int main(){
ofstream fout;
fout.open("6.1 Milne out.txt");
float Xo = 0;
float Xn = 12;
float h;
int i,n;
cout<<"enter step size";
cin>>h;
float N=(Xn-Xo)/h+1;
n=N+3;
float X[n],Y[n];
X[3]=0; Y[3]=0;
for(i=0; i<n; i++){
X[i]=X[3]+(i-3)*h;
}
for(i=0; i<3; i++){
Y[i]=X[i]+exp(-1*X[i])-1;
}
for(i=4; i<n; i++){
Y[i]=Y[i-4]+(4*h/3)*(2*f(X[i-1],Y[i-1])-f(X[i-2],Y[i-2])+2*f(X[i-3],Y[i-3]));
Y[i]=Y[i-2]+(h/3)*(f(X[i],Y[i])+4*f(X[i-1],Y[i-1])+f(X[i-2],Y[i-2]));
}
for(i=0; i<n; i++){
fout<<"\n"<<i-3<<"\t"<<X[i]<<"\t"<<Y[i]<<"\t \t"<<X[i]+exp(-1*X[i])-1;
}
fout<<"\n"<<"error:"<<"\t"<<fabs(Y[n-1]-(X[n-1]+exp(-1*X[n-1])-1));
return 0;
}