/* Eulers method implementation in CPP Language----- Md. Alamgir Hossain Department of Computer Science & Engineering. Jessore University of Science & Technology, Bangladesh. Email : alamgir.cse14.just@gmail.com */ #include<bits/stdc++.h> using namespace std; float equation(float x, float y) { ///Here enter the right side of your differential equation--- ///Some example of equation in the res variable-- ///You can check with your desired equation ///From here remove comment and check for another equation... float res; ///Equation start // res = (3*x*x)+1; res = (x*x)+(y*y); // res = y+(x*x); // res = ((2*y)/x)+(x*x*x); //res = y-((2*x)/y); return res; } ///Main function start from here---- int main() { float x0; cout<<"Enter the value of X0 : "; cin>>x0; float y0; cout<<"Enter the value of Y0 : "; cin>>y0; float xn; cout<<"Enter the value of Xn or x1 : "; cin>>xn; char pr; cout<<"If you want to enter the value of h press 1 else other : "; cin>>pr; float x1 = 0.0, h, h2; if(pr == '1'){ cout<<"Enter the value of h : "; cin>>h2; h = h2; }else{ h = xn - x0; } float y1,y2; x1 = h + x0; y1 = y0 + (h * equation(x0, y0)); y2 = y1 + (h * equation(x1, y1)); ///Now we want to check Eulers/Eulers Modified/Eulers Improved method-- cout<<"Press 1 for Eulers method, 2 for Modified and 3 for Improved method--"; char p; cin>>p; ///Machanism start for Eulers Method---------- if(p == '1'){ cout<<"Press 2 for two values 1 for one values"; char pp; if(pp == '2'){ cout<<"Enter the second value of x : "; float x2; cin>>x2; cout<<"\nThe Result for Y1("<<xn<<") using Eulers method is : "<<y1<<"\n"; cout<<"\nThe Result for Y2("<<x2<<") using Eulers method is : "<<y2<<"\n"; } else if(pp == '1'){ cout<<"\nThe Result for Y1("<<xn<<") using Eulers method is : "<<y1<<"\n"; } else{ cout<<"Please enter valid value---\n"; return 0; } } ///Eulers modified method start---------- else if(p == '2') { float v,v1,v2,v11,v21,vv; v1 = x0 + (h/2); v2 = y0 + ((h/2)+equation(x0,y0)); v = (h*(equation(v1,v2))+y0); v11 = x1 + (h/2); v21 = v + ((h/2)*equation(x1,v)); vv = (h * (equation(v11,v21))) + v; cout<<"Press 2 for two values 1 for one values"; char pp; cin>>pp; if(pp=='2'){ cout<<"Enter the second value of x : "; float x2; cin>>x2; cout<<"\nThe Result for Y1("<<xn<<") using Eulers modified method is : "<<v<<"\n"; cout<<"\nThe Result for Y2("<<x2<<") using Eulers modified method is : "<<vv<<"\n"; } else if(pp=='1'){ cout<<"\nThe Result for Y1("<<xn<<") using Eulers Modified method is : "<<v<<"\n"; } else{ cout<<"\nPlease enter valid value---\n"; return 0; } } ///Mechanism for Improved Eulers Method is------ else if(p == '3'){ float y1,y2; y1 = y0 + (h/2) * ((equation(x0,y0)) + (equation((x0+h),(y0+(h*(equation(x0,y0))))))); y2 = y1 + (h/2) * ((equation(x1,y1)) + (equation((x1+h),(y1+(h*(equation(x1,y1))))))); cout<<"Press 2 for two values 1 for one values"; char pp; cin>>pp; if(pp=='2'){ cout<<"Enter the second value of x : "; float x2; cin>>x2; cout<<"\nThe Result for Y1("<<xn<<") using Improved Eulers method is : "<<y1<<"\n"; cout<<"\nThe Result for Y2("<<x2<<") using Improved Eulers method is : "<<y2<<"\n"; } else if(pp=='1'){ cout<<"\nThe Result for Y1("<<xn<<") using Improved Eulers Modified is : "<<y1<<"\n"; } else{ cout<<"Please enter valid value---\n"; return 0; } } cout<<"\n\nThank You!!!!!\n\n"; return 0; } ///Input/Output Example----------------- /* Enter the value of X0 : 0 Enter the value of Y0 : 1 Enter the value of Xn : .1 If you want to enter the value of h press 1 else other : 1 Enter the value of h : .1 Press 1 for Eulers method, 2 for Modified and 3 for Improved method--2 Press 2 for two values 1 for one values2 Enter the second value of x : .2 The Result for Y1(0.1) using Eulers modified method is : 1.4205 The Result for Y2(0.2) using Eulers modified method is : 1.65437 Thank You!!!!! */
No comments:
Post a Comment