Académique Documents
Professionnel Documents
Culture Documents
C++ is a statically typed, free-form, multi-paradigm, compiled, general purpose programming language. It was developed by Bjarne Stroustrup. C++ is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. C++ is one of the most popular programming languages and is implemented on a wide variety of hardware and operating system platforms. C++ is used for many purpose like writing operating systems and scientific application. It is also used in field of Numerical Analysis. In the later part, various programs are shown in this file which performs various operations used in numerical analysis.
Fundamental components that every C++ program has can be divided into four basic parts:
a) Header Files: Syntax: #include<header_file_name.h> Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. Here .h extension shows that above file is a header file. Commonly used Header Files:#include<iostream.h> #include<conio.h> #include<math.h>
b) Main
Syntax: int main ( ); This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It is essential that all C++ programs have a main function.
c) Cout
Syntax: cout<<data to be printed on screen; cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (cout, which usually corresponds to the screen).
d) Return
Syntax: return 0; The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.
Page 1
Control Structures:
A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, C++ provides control structures that serve to specify what has to be done by our program, when and under which circumstances.
If and else:
The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is: Syntax: if (condition) statement1 else statement2 Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the else condition of the program continues right after this conditional structure.
Page 2
TASK 1: Write a program to for finding a real root of an equation using bisection method. SOURCE CODE:
#include<iostream.h> #include<conio.h> #include<math.h> #include <iomanip.h> #include <stdlib.h> float funct(float); int main() { double a,b,c,x1,x2; int n; clrscr(); cout<<endl; cout<<"Please enter your interval for EQN:f(x)=(x^3-x-4): "; cin>>x1>>x2; cout<<endl; while(x1<=x2) { if(funct(x1)*funct(x1+0.5)<0) { n=0; a=x1; b=x1+0.5; do { c=(a+b)/2; if(funct(a)*funct(c)<0) b=c; else a=c; n++; if((n>0)&&(n<=9)) { cout<<n<<" iteration at "<<c<<" f(x): "<<funct(c)<<endl; } else { cout<<n<<" iteration at "<<c<<" f(x): "<<funct(c)<<endl; } }while(fabs(b-a)>0.000001); // threshold cout<<endl<<"The root is: "<<c; cout<<endl<<endl<<"f(c) is: "<<funct(c);
Page 3
Page 4
OUTPUT:
Page 5
TASK 2: Write a program to for finding a real root of an equation using Newton Raphson method. SOURCE CODE:
#include<iostream.h> #include<conio.h> #include<math.h> #include <iomanip.h> #include <stdlib.h> double funct(double); double derv(double); int main() { double x0,x1,temp; int n=0,flag=0; clrscr(); cout<<endl<<"Enter X0 for EQN: f(x)= x^3-5*x+3 : "; cin>>x0; do{ if(fabs(derv(x0))<0.00001) { cout<<endl<<"Error: CANNOT DIVIDE BY ZERO!!!"; flag=1; break; } temp=x0; x1=x0-funct(x0)/derv(x0); n++; if(n>200) { cout<<endl<<"Error: TO LARGE TO BE CALCULATED!!!"; flag=1; break; } x0=x1; if(flag==0) { cout<<endl<<"x: "<<x0; cout<<"\nf(x): "<<funct(x0)<<"\nIteration No: "<<n<<endl; } }while(fabs(funct(x0))>0.00001); cout<<endl<<"Root for the above Eqn: "<<x0; cout<<endl<<endl<<"Value of Eqn at X0 ie f(x): "<<funct(x0); cout<<endl<<endl<<"No of Iterations done: "<<n; getch(); return 0; }
Page 6
Page 7
OUTPUT:
Page 8
TASK 3: Write a program to for finding a real root of an equation using Iteration method. SOURCE CODE:
#include<stdio.h> #include<conio.h> #include<math.h> #include<iostream.h> #define EPS 0.00005 #define F(x) (x*x*x + 1)/2 #define f(x) x*x*x - 2*x + 1 int n; void iter(); int main() { clrscr(); cout<<"\n Enter the no. of iterations for x*x*x - 2*x + 1 = 0 equation: "; cin>>n; iter(); getch(); } void iter() { int i=0; float x1,x2,x0; float f1,f2,f0,error; for(x1=1; ;x1++) { f1=f(x1); if(f1>0) break; } for(x0=x1-1; ;x0--) { f0=f(x0); if(f0<0) break; } x2=(x0+x1)/2; cout<<"\n\n\t\t The 1 approximatrion to the root is : "<<x2; for(;i<n-1;i++) { f2=F(x2); cout<<"\n\n\t\t The "<<i+2<<" approximatrion to the root is : "<<f2;
Page 9
x2=F(x2); error=fabs(f2-f1); if(error<EPS) break; f1=f2; } if(error>EPS) { cout<<"\n\t\t The no. of iterations are not sufficient."; } cout<<endl<<"\t\t ROOT for the equation x*x*x - 2*x + 1: "<<f2; }
Page 10
OUTPUT:
Page 11
TASK 4: Write a program for solving simultaneous linear algebraic equations using Gauss elimination method. SOURCE CODE:
#include <iostream.h> #include <conio.h> #include <iomanip.h> #include <stdlib.h> class elimination { public: void doCalculate(float A[10][10],int NEQ) { int m,i,j,k; double temp,D[10]; m=NEQ; cout<<endl<<"INPUT MATRIX: "<<endl<<endl; for(i=0;i<m;i++) { for(j=0;j<m+1;j++) { cout<<setw(5)<<setprecision(2)<<A[i][j]; if(j==2) { cout<<" :"; } } cout<<endl; } cout<<endl<<"PARTIALLY PIOVATING THE MATRIX: "<<endl<<endl; for(i=m-1;i>0;i--) // partial pivoting { if(A[i-1][0]<A[i][0]) for(j=0;j<=m;j++) { float temp = A[i][j]; A[i][j] = A[i-1][j]; A[i-1][j] = temp; } } for(i=0;i<m;i++) { for(j=0;j<m+1;j++) { cout<<setw(5)<<setprecision(2)<<A[i][j]; if(j==2) { cout<<" :"; }} cout<<endl; }
Page 12
for(k=0;k<m-1;k++) // Forward elimination process for(i=k;i<m-1;i++) { temp= (A[i+1][k]/A[k][k]) ; for(j=0;j<=m;j++) A[i+1][j]-=temp*A[k][j]; } cout<<endl<<"OUTPUT MATRIX: "<<endl<<endl; for(i=0;i<m;i++) { for(j=0;j<=m;j++) { cout<<setw(5)<<setprecision(2)<<A[i][j]; if(j==2) { cout<<" :"; } } cout<<endl; } for(i=m-1;i>=0;i--) { temp=0; for(j=i;j<=m-1;j++) temp=temp+A[i][j]*D[j]; D[i]=(A[i][m]-temp)/A[i][i]; } cout<<endl<<"SOLUTION SATISFYING THE EQUATIONS: "<<endl<<endl; for(i=0;i<m;i++) { cout<<" X"<<i+1<<" = "<<D[i]<<endl; }} }; int main() { float A[10][10]; int n,i,j,N1; elimination el; clrscr(); cout<<"Enter number of Equations : "; cin>>n; cout<<"Enter the Input Matrix : "<<endl; for(i=0;i<n;i++) { for(j=0;j<=n;j++) { cin>>A[i][j]; } } el.doCalculate(A,n); getch();}
Page 13
OUTPUT:
Page 14
TASK 5: Write a program for solving simultaneous linear algebraic equations using Gauss Jordan method. SOURCE CODE:
#include <iostream.h> #include <conio.h> #include <iomanip.h> #include <stdlib.h> class Jordan { public: void doCalculate(float A[50][50],int NEQ, int N1) { int N,j,i; float D,D1; for(N=1;N<=NEQ;N++) { D = 1 / A[N][N]; for (j=1; j<=N1;j++) A[N][j] = D * A[N][j]; for (i=1;i<=NEQ;i++) { if (i==N) i=i+1; if (i>NEQ) return; D1=A[i][N]; for(j=1;j<=N1;j++) A[i][j]=A[i][j]-D1*A[N][j]; } A[N][N]=1; } } }; int main() { float A[50][50],B[50],temp; int NEQ,i,j,N1; Jordan jo; //clrscr(); cout<<"Enter number of Equations : "; cin>>NEQ; N1 = NEQ+1; cout<<"Enter the Input Matrix : "<<endl; for(i=1;i<=NEQ;i++) { for(j=1;j<=N1;j++) {
Page 15
cin>>A[i][j]; } } cout<<endl<<"INPUT MATRIX: "<<endl; for(i=1;i<=NEQ;i++) { for(j=1;j<=N1;j++) { cout<<setw(5)<<A[i][j]; if(j==3) { cout<<" :"; } } cout<<endl; } jo.doCalculate(A,NEQ,N1); cout<<endl<<"OUTPUT MATRIX: "<<endl; for(i=1;i<=NEQ;i++) { for(j=1;j<=N1;j++) { cout<<setprecision(2)<<setw(5)<<A[i][j]; if(j==3) { cout<<" :"; } } cout<<endl; } getch(); }
Page 16
OUTPUT:
Page 17
TASK 6: Write a program for solving equation using Simpson's 1/3rd rule for numerical integration. SOURCE CODE:
#include<iostream.h> #include<conio.h> #include<math.h> float funct(float a); int main() { float f,x,h,a,b,sum; clrscr(); cout<<"Simpson 1/3rd rule for Numerical integration of 1/(1+(x^2)"; cout<<endl<<"\nInter the value of lower and upper intrgral: "; cin>>a>>b; sum=0; x=a; cout<<endl<<"Enter value of h ? "; cin>>h; while(x<b) { sum+=(funct(x)+4*funct(x+h)+funct(x+2*h)); x=x+2*h; } cout<<endl<<"The integration is: "<<sum*h/3<<endl; getch(); return 0; } float funct(float x) { return 1/(1+x*x); }
Page 18
OUTPUT:
Page 19
TASK 7: Write a program for implementing Newton's forward interpolation formula. SOURCE CODE:
#include<iostream.h> #include<conio.h> int main() { float x[30],y[30],p,h,a,yp,d[30][30],nr=1,dr=1; int i,j,n; cout<<"Enter the value of N: "; cin>>n; for(i=0;i<n;i++) { cout<<endl<<"Enter the element of X"<<i+1<<" : "; cin>>x[i]; cout<<endl<<"Enter the element of Y"<<i+1<<" : "; cin>>y[i]; } h=x[1]-x[0]; cout<<endl<<"Enter X corresponding to which we calculate for Y: "; cin>>a; p=(a-x[0])/h; for(j=1;j<n;j++) { for(i=0;i<n-j;i++) { if(j==1) d[i][j]=y[i+1]-y[i]; else d[i][j]=d[i+1][j-1]-d[i][j-1]; } } yp=y[0]; for(i=1;i<n;i++) { nr=nr*(p-i+1); dr=dr*i; yp=yp+(nr/dr)*d[0][i]; } cout<<endl<<"At X = "<<a<<" Y = "<<yp; getch(); return 0; }
Page 20
OUTPUT:
Page 21
TASK 8: Write a program for implementing Lagrange's method for interpolation. SOURCE CODE:
#include<iostream.h> #include<conio.h> int main() { float x[30],y[30],p,h,a,yp,d[30][30],nr=1,dr=1; int i,j,n; cout<<"Enter the value of N: "; cin>>n; for(i=0;i<n;i++) { cout<<endl<<"Enter the element of X"<<i+1<<" : "; cin>>x[i]; cout<<endl<<"Enter the element of Y"<<i+1<<" : "; cin>>y[i]; } h=x[1]-x[0]; cout<<endl<<"Enter X corresponding to which we calculate for Y: "; cin>>a; for(i=0;i<n;i++) { nr=1; dr=1; for(j=0;j<n;j++) { if(i!=j) { nr=nr*(a-x[j]); dr=dr*(x[i]-x[j]); } yp=yp+(nr/dr)*y[i]; } } cout<<endl<<"At X = "<<a<<" Y = "<<yp; getch(); return 0; }
Page 22
OUTPUT:
Page 23
TASK 9: Write a program for solving ordinary differential equations using Euler's method. SOURCE CODE:
#include<stdio.h> #include<conio.h> #include<iostream.h> int main() { int c=0; float x,y,xp,h,dy,i,n; float f(float,float); cout<<endl<<"Euler's method for solving (y-x)/(y+x)"; cout<<endl<<endl<<"Enter initial Boundry condition x: "; cin>>x; cout<<endl<<"Enter initial Boundry condition y: "; cin>>y; cout<<endl<<"Enter Value of X at which Y is required: "; cin>>xp; cout<<endl<<"Enter Interval h: "; cin>>h; cout<<endl<<endl; cout<<"\t\tNo. \t X\t f(x,y) \t Y\n"; for(i=x;i<=xp;i=i+h) { c++; n=y+h*f(i,y); cout<<endl<<"\t\t"; printf("%2d\t %2.3f\t %5.5f\t %5.6f\n",c,i,f(i,y),n); y=n; } cout<<endl; cout<<"Value of Y: "<<n<<endl<<endl<<"No. of iteration: "<<c; getch(); } float f(float x,float y) { return (y-x)/(y+x); }
Page 24
OUTPUT:
Page 25
TASK 10: Write a program for solving ordinary differential equations using R-K method. SOURCE CODE:
#include<stdio.h> #include<conio.h> #include<math.h> #include<iostream.h> #include<conio.h> float math(float x, float y) { float f; f=sqrt(x+y); return(f); } int main() { int i,n; float x0,y0,x,h,k1,k2,k3,k4; cout<<endl<<"R-K method for solving sqrt(x+y) "; cout<<endl<<endl<<"Type the value of the point x where value of y to be found: "; cin>>x; cout<<endl<<"Type the initial value of x: "; cin>>x0; cout<<endl<<"Type the initial value of y: "; cin>>y0; cout<<endl<<"Type the number of step length h: "; cin>>n; h=(x-x0)/n; for(i=1;i<=n;i++) { k1=h*math(x0,y0); k2=h*math(x0+h/2,y0+k1/2); k3=h*math(x0+h/2,y0+k2/2); k4=h*math(x0+h,y0+k3); y0+=(k1+2*k2+2*k3+k4)/6; x0+=h; } cout<<endl<<"Value of X= "<<x; cout<<endl<<endl<<"Value of Y= "<<y0; getch(); }
Page 26
OUTPUT:
Page 27