0% found this document useful (0 votes)
56 views

function F For Dy/dx F (X, Y) : C++ Code For Euler's Method

The document contains C++ code implementing four different numerical methods - Euler's method, Modified Euler's method, Range-Kutta method, and AM-4 method - to solve ordinary differential equations. Each method is presented in a separate code block that defines functions, variables, loops to calculate values, and outputs results to a text file. The codes demonstrate how to discretize the independent and dependent variables and iteratively calculate new dependent variable values based on the specific update rules for each numerical integration technique.

Uploaded by

Harsh Agarwal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

function F For Dy/dx F (X, Y) : C++ Code For Euler's Method

The document contains C++ code implementing four different numerical methods - Euler's method, Modified Euler's method, Range-Kutta method, and AM-4 method - to solve ordinary differential equations. Each method is presented in a separate code block that defines functions, variables, loops to calculate values, and outputs results to a text file. The codes demonstrate how to discretize the independent and dependent variables and iteratively calculate new dependent variable values based on the specific update rules for each numerical integration technique.

Uploaded by

Harsh Agarwal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

C++ Code for Euler’s Method

#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 Euler 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;

float X[n],Y[n];

X[0]=0; Y[0]=0;

for(i=0; i<n; i++){

X[i]=X[0]+i*h; //Calculating X

for(i=1; i<n; i++){ //Calculating Yn+1 = Yn + h*fn

Y[i]=Y[i-1]+ h*f(X[i-1], Y[i-1]);

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 f(float x, float y){


return x-y;
}
int main(){
ofstream fout;
fout.open("6.1 Euler mod 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;
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 f(float x, float y){


return x-y;
}
int main(){
ofstream fout;
fout.open("6.1 RK out.txt");

float Xo = 0; float Xn = 12;


float h,k1,k2,k3,k4;
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++){
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 f(float x, float y){


return -3*y;
}
int main(){
ofstream fout;
fout.open("6.1 AM 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-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;
}

You might also like