Program: 1Aim: To find roots of a polynomial using Bisection method.
#include<iostream>#include<cmath>using namespace std;
float bisect(float a, float b){ return (a+b)/2;}float f(float x){ return (x*x*x - 4*x - 9);}
int main(){ int max_itr, i, j; float a, b, error; cout << "Enter values of a and b : "; cin >> a >> b; cout << "Enter Allowed Error : "; cin >> error; cout << "Enter Maximum Iterations : "; cin >> max_itr;
if(f(a)*f(b) > 0) cout << "NO ROOT LIES BETWEEN " << a << " and " << b; else { bool ans = false; i = 0; float prev_x = 1000.0; while(i < max_itr) { float x = bisect(a,b); if(fabs(x-prev_x) <= error) { cout << "\n\nRoot found After (" << i+1 <<") Iterations\n"; cout << "ROOT = " << x; ans = true; break; } prev_x = x;
if(f(a)*f(x) < 0) b = x; else a = x; cout << "\n\nAfter Iteration (" << i+1 << "), a, b, x, are:\t\t" << a << ' ' << b << ' ' << x; i++; } if(!ans) cout << "\n\n\nROOT NOT FOUND\nNumber of Iterations Entered is less than required"; } return 0;}
OUTPUTSCase: 1
Case: 2
OUTPUTSCase: 3
Program: 2Aim: To find roots of a polynomial using Regula Falsi method.
#include<iostream>#include<cmath>using namespace std;
float regulaFalsi(float a, float b, float fa, float fb){ return (a*fb - b*fa)/(fb - fa);}float f(float x){ return (cos(x) - x*exp(x));}
int main(){ int max_itr, i, j; float a, b, error; cout << "Enter values of a and b : "; cin >> a >> b; cout << "Enter Allowed Error : "; cin >> error; cout << "Enter Maximum Iterations : "; cin >> max_itr;
if(f(a)*f(b) > 0) cout << "NO ROOT LIES BETWEEN " << a << " and " << b; else { bool ans = false; i = 0; float x; float prev_x = 1000.0; while(i < max_itr) { x = regulaFalsi(a,b,f(a),f(b)); if(fabs(x-prev_x) <= error) { cout << "\n\nRoot found After (" << i+1 <<") Iterations\n"; cout << "ROOT = " << x; ans = true; break; }
prev_x = x; if(f(a)*f(x) < 0) b = x; else a = x; cout << "\nAfter Iteration (" << i+1 << "), a, b, x, are:\t" << a << ' ' << b << ' ' << x; i++; } if(!ans) cout << "\n\nROOT NOT FOUND\nNumber of Iterations Entered is less than required"; } return 0;}
OUTPUTS
Case: 1
Case: 2
OUTPUTS
Case: 3
Program: 3
Aim: To find roots of a polynomial using Newton-Raphson method.
#include<iostream>
#include<cmath>
using namespace std;
float f(float x)
{
return (x*log10(x) - 1.2);
}
float df(float x)
{
return (log10(x) + 0.42439);
}
int main()
{
int max_itr;
float a, b, x, error, h;
cout << "Enter values of a and b : ";
cin >> a >> b;
if(f(a)*f(b) > 0)
cout << "Either No Root Exists OR Even Number of Roots Exist";
else
{
cout << "Enter allowed error : ";
cin >> error;
cout << "Enter Maximum Number of iterations : ";
cin >> max_itr;
x = (a + b)/2;
int i = 0;
bool ans = false;
while(i < max_itr)
{
h = f(x)/df(x);
if(fabs(h) <= error)
{
cout << "\nRoot " << x << " Found After " << i + 1 << " Iterations";
ans = true;
break;
}
x = x - h;
cout << "\nAfter Iteration " << i + 1 << " x = " << x;
i++;
}
if(!ans)
cout << "\n\n Maximum Iteration is Not Sufficient";
}
}
OUTPUTSCase: 1
Case: 2
OUTPUTSCase: 3
Program: 4Aim: To perform matrix addition, subtraction, multiplication and transpose.
#include<iostream>using namespace std;void add(int a[][10],int b[][10],int ,int);void sub(int a[][10],int b[][10],int ,int);void multiply(int a[][10],int b[][10],int ,int,int ,int);void transpose(int a[][10],int ,int);int main(){ int m, n, p, q, a[10][10], b[10][10]; cout << "Enter order of first matrix:"; cin >> m >> n; cout << "Enter first matrix:\n"; for(int i = 0; i < m; i++) for(int j = 0;j < n; j++) cin >> a[i][j]; cout << "Enter order of second matrix:"; cin >> p >> q; cout << "Enter second matrix:\n"; for(int i = 0; i < p; i++) for(int j = 0; j < q; j++) cin >> b[i][j]; if(m != p || q != n) cout << "\nAddition and subtraction is not possible!!!"; else { add(a,b,m,n); sub(a,b,m,n); } if(n != p) cout << "\nMultiplication not possible!!!"; else multiply(a,b,m,n,p,q); transpose(a,m,n); return 0;}void add(int a[][10],int b[][10],int m, int n){ int c[10][10]; cout << "\nAddition is:\n"; for(int i=0;i<m;i++) { for(int j=0;j<n;j++)
{ c[i][j]=a[i][j]+b[i][j]; cout << "\t" << c[i][j]; } cout << endl; }}void sub(int a[][10],int b[][10],int m, int n){ cout << "\nSubtraction is:\n"; int c[10][10]; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { c[i][j]=a[i][j]-b[i][j]; cout << "\t" << c[i][j]; } cout << endl; }
}void transpose(int a[][10],int m,int n){ cout << "\nTranspose is:\n"; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) cout << "\t" << a[j][i]; cout << endl; }}void multiply(int a[][10],int b[][10],int m,int n,int p,int q){ cout << "\nMultiplication is:\n"; int c[10][10];
OUTPUTSCase: 1
Case: 2
OUTPUTSCase: 3
Program: 5
Aim: To find solution of given equations10x - 7y + 3z + 5w = 6-6x + 8y - z - 4w = 53x + y + 4z + 11w = 25x - 9y - 2z + 4w = 7
by Gauss Elimination method.
#include<iostream>#include<iomanip>using namespace std;#define N 4int main(){float a[N][N+1], x[N], t, s;cout << "\nEnter Cofficient of x,y,z,w and constant term for:\n";for(int i = 0; i < N; i++){cout << "Equation " << i+1 << " : ";for(int j = 0; j < N+1; j++)cin >> a[i][j];}for(int j = 0; j < N-1; j++){for(int i = j+1; i < N; i++){t = a[i][j]/a[j][j];for(int k = 0; k < N+1; k++)a[i][k] = a[i][k] - t*a[j][k];}}cout << "\nUpper triangular matrix is:\n";for(int i = 0; i < N; i++){for(int j = 0; j < N+1; j++)cout <<setw(8)<< fixed << setprecision(5) << a[i][j] << "\t";cout << endl;}for(int i = N-1; i >= 0; i--){s = 0;for(int j = i+1; j < N; j++)s += a[i][j]*x[j];x[i] = (a[i][N] - s)/a[i][i];}cout << "\nSolutions of Given System of Equations are:\n";for(int i = 0; i < N; i++){cout <<setw(8)<< fixed << setprecision(5) << x[i] << endl;}return 0;}
OUTPUTS
Program: 6
Aim: To find solution of given equations
10x - 7y + 3z + 5w = 6-6x + 8y - z - 4w = 53x + y + 4z + 11w = 25x - 9y - 2z + 4w = 7
by Gauss Jordan method.
#include<iostream>#include<iomanip>using namespace std;#define N 4int main(){ float a[N][N+1], x[N], t, s; cout << "\nEnter cofficient of x,y,z,w and constant term for:\n"; for(int i = 0; i < N; i++) { cout << "Equation " << i+1 << " : "; for(int j = 0; j < N+1; j++) cin >> a[i][j]; } for(int j = 0; j < N; j++) for(int i = 0; i < N; i++) { if(i != j) { t = a[i][j]/a[j][j]; for(int k = 0; k < N+1; k++) a[i][k] = a[i][k] - t*a[j][k]; } } cout << "\nDiagonal matrix is:\n"; for(int i = 0; i < N; i++) { for(int j = 0; j < N+1; j++) cout << setw(8) << fixed << setprecision(3) << a[i][j] << "\t"; cout << endl; } for(int i = 0; i < N; i++) x[i] = a[i][N]/a[i][i]; cout << "\nSolutions of Given System of Equations are:\n"; for(int i = 0; i < N; i++) cout << setw(8) << x[i] << endl; return 0;
}for(int i = 0; i < m; i++) { for(int j=0;j<q;j++) { c[i][j]=0; for(int k = 0; k < n; k++) c[i][j] += a[i][k]*b[k][j]; cout << "\t" << c[i][j]; } cout << endl; }}
OUTPUTS
Program: 7
Aim: To find inverse of given matrix using Gauss Jordan method.
2 2 32 1 11 3 5
#include<iostream>#include<iomanip>using namespace std;#define n 3int main(){
float a[n][n],t,d,s,i1[n][n]={1,0,0,0,1,0,0,0,1};cout<<"\n \t Enter the matrix:";for(int i=0;i<n;i++){
for(int j=0;j<n;j++)cin>>a[i][j];
}for(int j=0;j<n;j++){
for(int i=0;i<n;i++){
if(i!=j){t=a[i][j]/a[j][j]; for(int k=0;k<n;k++){a[i][k]=a[i][k]-t*a[j][k];i1[i][k]=i1[i][k]-t*i1[j][k];
}}}
}cout<<"\n \t Diagonal matrix is:\n";for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<fixed<<setprecision(3)<<a[i][j]<<"\t";}cout<<endl;
}
cout<<"\n\t Inverse of given matrix is :\n";for(int i=0;i<n;i++){ for(int j=0;j<n;j++)
i1[i][j]=i1[i][j]/a[i][i];}for(int i=0;i<n;i++){
for(int j=0;j<n;j++)cout<<i1[i][j]<<" \t";cout<<endl;
}return 0;
}
OUTPUT
Program: 8
Aim: Write a program to find ∫0
6 11+x2
using Trapezoidal Rule.
#include<iostream>using namespace std;float f(float x){
return 1/(1+x*x);}int main(){
float a,b,s=0,h,n;cout<<"\n Enter a,b and n:";cin>>a>>b>>n;s=f(a)+f(b);h=(b-a)/n;for(int i=1;i<n;i++){
s=s+2*f(a+i*h);}s=s*h/2;cout<<"The value of given integral using Trapezoidal rule is:"<<s;return 0;
}
OUTPUT
Program: 9Aim: Write a program to find ∫
0
6 11+x2
using Simpsons one third Rule.
#include<iostream>using namespace std;float f(float x){
return 1/(1+x*x);}int main(){
float a,b,s=0,h,n;cout<<"\n Enter a,b and n:";cin>>a>>b>>n;s=f(a)+f(b);h=(b-a)/n;for(int i=1;i<n;i=i+2){
s=s+4*f(a+i*h);}for(int i=2;i<n-1;i=i+2){
s=s+2*f(a+i*h);}s=s*h/3;cout<<"The value of given integral using Simpsons one third rule is:"<<s;return 0;
}
OUTPUT
Program: 10
Aim: Write a program to solve the differential equation using Runge Kutta Method of order 4dydx
=x+ y2 , y(0) = 1 for x = 0, 0.2, 0.6.
#include<iostream>#include<math.h>using namespace std;double f(double x,double y){
return(pow(y,2)+x);}int main(){
double xo,xn,yo,x,y,i,j,k,k1,k2,k3,k4,h;cout<<"Enter the Values of xo,yo,xn,h :\n";cin>>xo>>yo>>xn>>h;x=xo;y=yo;for(i=xo;i<xn;i+=h){
k1=h*f(x,y);k2=h*f(x+(h/2),y+(k1/2));k3=h*f(x+(h/2),y+(k2/2));k4=h*f(x+h,y+k3);k=(k1+2*k2+2*k3+k4)/6;x=x+h;y=y+k;cout<<"\nThe value of y at x = "<<x<<" is : "<<y<<"\n";
}return 0;
}
OUTPUT
START
Function f(x)= x+y2
Input xo,yo,h,xn
n=(xn-xo)/h , x=xo , y=yo
For i=0,n
Print x,y
x=x+h
y=y+k
STOP
k1=h*f(x,y)
k2=h*f(x+h/2,y+k1/2)
k3=h*f(x+h/2,y+k2/2)
k4=h*f(x+h,y+k3)
k=(k1+(k2+k3)*2+k4)/6
Program: 11
Aim: Write a program to find solution of given equations by Gauss Siedel method.
20x + y - 2z = 173x + 20y - z = -182x - 3y + 20z = 25
#include<bits/stdc++.h>using namespace std;#define n 3int main(){
int maxitr;float aerr,a[n][n+1],x[n]={0},maxerr=0,prev;cout<<"\nEnter maximum iteration & allowed error : \n";cin>>maxitr>>aerr;cout<<"\nEnter the augmented matrix :\n";for(int i=0;i<n;i++)
for(int j=0;j<n+1;j++)cin>>a[i][j];
for(int i=0;i<maxitr;i++){
maxerr=0;for(int j=0;j<n;j++){
float s=0;for(int k=0;k<n;k++){if(j!=k){
s+=a[j][k]*x[k];}}if(i!=0)
prev=x[j];x[j]=(a[j][n]-s)/a[j][j];maxerr=max(maxerr,fabs(x[j]-prev));
} cout<<"\nIteration "<<i+1<<" Values of x,y,z are : ";
for(int q=0;q<n;q++)cout<<x[q]<<" ";
cout<<endl;if(maxerr<aerr)
break;}
cout<<"\n Final result is : \n";for(int i=0;i<n;i++)cout<<x[i]<<"\n";
return 0;}
OUTPUT
Program: 12
Aim: Write a program to find largest Eigen value and corresponding Eigen vector of the given matrix using Power method.
A = [ 2 −1 0−1 2 −10 −1 2 ]
#include<bits/stdc++.h>using namespace std;#define n 3int main(){
int maxitr;float aerr,a[n][n],x[n],maxerr=0,max1=0,l=0,newx[n]={0},prevl=0;cout<<"Enter maximum iteration and allowed error : \n";cin>>maxitr>>aerr;cout<<"\nEnter augmented matrix :\n";for(int i=0;i<n;i++)
{ for(int j=0;j<n;j++)cin>>a[i][j]; }
cout<<"\nEnter initial approximation of eigen vector : \n";for(int i=0;i<n;i++)
cin>>x[i];cout<<"\nIteration\t Eigen value \t\t\t\tEigen vectors \n";for(int i=0;i<maxitr;i++){
maxerr=0;max1=0;for(int j=0;j<n;j++){
newx[j]=0;for(int k=0;k<n;k++)
newx[j]=newx[j]+a[j][k]*x[k];max1=max(max1,fabs(newx[j]));
} l=max1; maxerr=max(maxerr,fabs(prevl-l)); prevl=l; for(int u=0;u<n;u++)
newx[u]=newx[u]/l; for(int p=0;p<n;p++)
{maxerr=max(maxerr,fabs(x[p]-newx[p]));
x[p]=newx[p];}cout<<setw(14)<<left<<i<<setw(20)<<left<<l;for(int i=0;i<n;i++)
cout<<setw(12)<<left<<x[i];cout<<endl;if(maxerr<aerr) break;
}cout<<"\nThe required eigen value is : "<<l;cout<<"\nThe required eigen vector is :\n";for(int i=0;i<n;i++)
cout<<x[i]<<"\t";return 0;
}
OUTPUT
Program: 13
Aim: Write a program to fit a parabola a+bx+c x2 using least squares method.
#include<iostream>#include<iomanip>using namespace std;int main(){ float augm[3][4], x, y, xsq, t, X[3]; int i,j,n,k; for(i = 0; i < 3; i++) for(j = 0; j < 4; j++) augm[i][j] = 0; cout << "Enter the no. of pairs of observed values : "; cin >> n; augm[0][0] = n; for(i = 0; i < n; i++) { cout << "Enter pair no. "<< i+1 << " : "; cin >> x >> y; xsq = x*x; augm[0][1] += x; augm[0][2] += xsq; augm[1][2] += xsq*x; augm[0][3] += y; augm[1][3] += x*y; augm[2][2] += xsq*xsq; augm[2][3] += xsq*y; } augm[1][0] = augm[0][1]; augm[1][1] = augm[0][2]; augm[2][0] = augm[0][2]; augm[2][1] = augm[1][2]; cout << "\nAugmented matrix is : \n"; for(i = 0; i < 3; i++) { cout << '\n'; for(j = 0; j < 4; j++) cout << fixed << setprecision(6) << augm[i][j] << '\t'; } for(j = 0; j < 3; j++) for(i = 0; i < 3; i++) if(i != j) {
t = augm[i][j]/augm[j][j]; for(k = 0; k < 4; k++) augm[i][k] -= augm[j][k]*t; } cout << "\n\nThe Upper Triangular Matrix is:\n"; for(i = 0; i < 3; i++) { for(j = 0; j < 4; j++) cout << fixed << setprecision(6) << ' ' << augm[i][j] << ' '; cout << '\n'; } for(i = 0; i < 3; i++) X[i] = augm[i][3]/augm[i][i]; cout << "\nSolution of the given system of linear equation is:\n"; cout << "a = " << X[0] << "\nb = " << X[1] << "\nc = " << X[2]; cout << "The Required equation is : \n y = " << X[0] << " + " << X[1] << "x + " << X[2] << "x^2"; return 0;}
OUTPUT
Top Related