Escolar Documentos
Profissional Documentos
Cultura Documentos
#include<iostream>
#include<math.h>
int main()
double abs_err,rel_err,p_rel_err,t_val,a_val;
cin>>t_val;
cin>>a_val;
abs_err=fabs(t_val-a_val);
rel_err=abs_err/t_val;
p_rel_err=rel_err*100;
}
OUTPUT:
2) To find the roots of non-linear equation using Bisection method.
#include<bits/stdc++.h>
#define e 2.71828
double func(double x)
return x*pow(e,x)-1;
return;
double c = a;
c = (a+b)/2;
// Check if middle point is root
if (func(c) == 0.0)
break;
b = c;
else
a = c;
int main()
double a,b;
cin>>a>>b;
bisection(a, b);
return 0;
}
OUTPUT:
3) To find the roots of non-linear equation using Secant Method.
#include <bits/stdc++.h>
float f(float x)
return f;
do {
c = f(x1) * f(x0);
x1 = x2;
x2 = x0;
n++;
if (c == 0)
break;
} else
// Driver code
int main()
float x1 , x2 , E = 0.0001;
cin>>x1>>x2;
return 0;
}
OUTPUT:
4) To find the roots of non-linear equation using Newtons Method.
#include<bits/stdc++.h>
double func(double x)
double derivFunc(double x)
void newtonRaphson(double x)
h = func(x)/derivFunc(x);
// x(i+1) = x(i) - f(x) / f'(x)
x = x - h;
int main()
double x0;
cin>>x0;
newtonRaphson(x0);
return 0;
}
OUTPUT:
5) To implement numerical integration using Trapezoidal rule.
#include<iostream>
#include<math.h>
float y(float x)
// Grid spacing
float h = (b-a)/n;
float s = y(a)+y(b);
s += 2*y(a+i*h);
int main()
float x0,xn;
cout<<"\nEnter range";
cin>>x0>>xn;
int n;
cin>>n;
return 0;
}
OUTPUT:
6) To implement numerical integration using Simpson 1/3 rule.
#include<iostream>
float f(float x)
return(1/(1+x));
int main()
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
cin>>x0>>xn>>h;
n=(xn-x0)/h;
if(n%2==1)
n=n+1;
h=(xn-x0)/n;
x[i]=x0+i*h;
y[i]=f(x[i]);
cout<<" "<<y[i]<<"\n";
so=0;
se=0;
if(i%2==1)
so=so+y[i];
else
se=se+y[i];
ans=h/3*(y[0]+y[n]+4*so+2*se);
}
OUTPUT:
7) To implement numerical integration using Simpson 3/8 rule.
#include<iostream>
#include<math.h>
return x*x;
int main(){
int n,i;
double a,b,h,x,sum=0,integral;
cin>>n;
cin>>a;
cin>>b;
h=fabs(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
if(i%3==0){
sum=sum+2*f(x);
else{
sum=sum+3*f(x);
integral=(3*h/8)*(f(a)+f(b)+sum);
OUTPUT:
8) To solve the system of linear equations using Gauss-elimination method.
#include<bits/stdc++.h>
/* if matrix is singular */
if (singular_flag != -1)
cout<<"Singular Matrix\n";
if (mat[singular_flag][N])
cout<<"Inconsistent System";
else
return;
backSub(mat);
mat[i][k] = mat[j][k];
mat[j][k] = temp;
if (!mat[k][i_max])
if (i_max != k)
swap_row(mat, k, i_max);
double f = mat[i][k]/mat[k][k];
mat[i][j] -= mat[k][j]*f;
/* filling lower triangular matrix with zeros*/
mat[i][k] = 0;
return -1;
x[i] = mat[i][N];
/* subtract all the lhs values except the coefficient of the variable
x[i] -= mat[i][j]*x[j];
}
/* divide the RHS by the coefficient of the unknown being calculated */
x[i] = x[i]/mat[i][i];
cout<<"\n"<<x[i];
// Driver program
int main()
double mat[N][N+1];
for(int i=0;i<N;i++)
for(int j=0;j<N+1;j++)
cin>>mat[i][j];
gaussianElimination(mat);
return 0;
}
OUTPUT:
9) To implement Lagranges interpolation formula.
#include<bits/stdc++.h>
struct Data
double x, y;
};
if (j!=i)
result += term;
return result;
int main()
int x;
cin>>x;
return 0;
}
OUTPUT:
10) To find numerical solution of ordinary differential equations by Eulers method.
#include<iostream>
#include <math.h>
#include<iomanip>
int main()
double y1,y2,x1,a,n,h;
int j;
cin>>a>>n;
cin>>y1;
cin>>h;
cout<<"\n\n y1 = "<<y1;
y2= y1 + h * F(x1,y1);
y1=y2;
}
OUTPUT:
11) To find numerical solution of ordinary differential equations by Runge Kutta
method.
#include<iostream>
return((x - y)/2);
// step height h
float y = y0;
k1 = h*dydx(x0, y);
k4 = h*dydx(x0 + h, y + k3);
x0 = x0 + h;
return y;
// Driver method
int main()
float x0,y,x,h;
cin>>x0>>y>>x>>h;
return 0;
}
OUTPUT: