#include <stdio.
h>
int fact(int n){
int deno=1;
for(int i=n;i>=1;i--) deno*=i;
return deno;
}
int main(){
int n;
printf("Enter number of data values: ");
scanf("%d",&n);
if(n<=0){
printf("Invalid number of data points!");
return 1;
}
if(n==1){
printf("Atleast 2 data points required!");
return 1;
}
float x[n],di [n][n];
printf("Enter data values:\n");
for(int i=0;i<n;i++){
printf("x[%d]=",i);
scanf("%f",&x[i]);
printf("y[%d]=",i);
scanf("%f",&di [0][i]);
}
float h = x[1]-x[0];
for(int i=2;i<n;i++){
if((x[i]-x[i-1])!=h){
printf("Intervals are not equal, Interpolation cannot be calculated!");
return 1;
}
}
for(int i=1;i<n;i++){
for(int j=0;j<n-i;j++){
di [i][j]=di [i-1][j+1]-di [i-1][j];
}
}
printf("The di erence table:\n");
printf("+-------------------------------------------------------------------------+\n");
printf("| \t x \t | \t y \t |");
for(int i=1;i<n;i++) printf("\t D[%d] \t |",i);
printf("\n");
printf("+-------------------------------------------------------------------------+\n");
for(int i=0;i<n;i++){
printf(" \t %0.2f \t",x[i]);
for(int j=0;j<n-i;j++){
printf("\t %0.2f \t",di [j][i]);
}
printf("\n");
}
float data;
printf("Enter data point to be interpolated: ");
scanf("%f",&data);
## Newton Forward Interpolation
float u = (float)(data-x[0])/h;
float ans=di [0][0];
float term=1;
for(int i=1;i<n;i++){
term*=(u-(i-1));
ans+= (term * di [i][0]) / fact(i);
}
printf("Interpolated Data: %0.2f ",ans);
return 0;
}
##Newton Backward Interpolation
float u= (data-x[n-1])/h;
float ans=di [0][n-1];
float term=1;
for(int i=1;i<n;i++){
term*=(u+(i-1));
ans+=(term*di [i][n-i-1])/fact(i);
}
printf("The interpolated value is: %f",ans);
return 0;
##Lagrange
#include <stdio.h>
int main(){
int n;
printf("Enter number of data points: ");
scanf("%d",&n);
float x[n],y[n];
printf("Enter data points: ");
for(int i=0;i<n;i++){
printf("x[%d]=",i);
scanf("%f",&x[i]);
printf("y[%d]=",i);
scanf("%f",&y[i]);
}
float data;
printf("Enter data: ");
scanf("%f",&data);
float store;
float Sum=0;
for(int i=0;i<n;i++){
float num=1;
float den=1;
for(int j=0;j<n;j++){
if(j!=i){
num*=(data-x[j]);
den*=(x[i]-x[j]);
}
}
Sum+=(num/den)*y[i];
}
printf("The value is: %0.3f",Sum);
return 0;
}
✔ Check that the diagonal elements are non-zero.
✔ Check whether the matrix is diagonally dominant.
##Gauss Siedel Method
#include <stdio.h>
int main(){
float A[3][3];
printf("Enter the values in the matrix:\n");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("A[%d][%d]=",i,j);
scanf("%f",&A[i][j]);
}
}
float B[3];
printf("Enter solution set:\n");
for(int i=0;i<3;i++){
printf("B[%d]=",i);
scanf("%f",&B[i]);
}
printf("Augmented Matrix:\n");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("%0.2f \t",A[i][j]);
}
printf("| %0.2f",B[i]);
printf("\n");
}
int iter;
printf("Enter number of iterations: ");
scanf("%d",&iter);
float x=0;
float y=0;
float z=0;
for(int i=0;i<iter;i++){
x=(B[0]-A[0][1]*y-A[0][2]*z) / A[0][0];
y=(B[1]-A[1][0]*x-A[1][2]*z) / A[1][1];
z=(B[2]-A[2][0]*x-A[2][1]*y) / A[2][2];
}
printf("The solution is x=%0.2f, y=%0.2f, z=%0.2f: ",x,y,z);
return 0;
}
##Guass Jacobi Method
#include <stdio.h>
int main(){
float A[3][3];
printf("Enter the values in the matrix:\n");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("A[%d][%d]=",i,j);
scanf("%f",&A[i][j]);
}
}
float B[3];
printf("Enter solution set:\n");
for(int i=0;i<3;i++){
printf("B[%d]=",i);
scanf("%f",&B[i]);
}
printf("Augmented Matrix:\n");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("%0.2f \t",A[i][j]);
}
printf("| %0.2f",B[i]);
printf("\n");
}
int iter;
printf("Enter number of iterations: ");
scanf("%d",&iter);
float x=0;
float y=0;
float z=0;
float newx,newy,newz;
for(int i=0;i<iter;i++){
newx=(B[0]-A[0][1]*y-A[0][2]*z) / A[0][0];
newy=(B[1]-A[1][0]*x-A[1][2]*z) / A[1][1];
newz=(B[2]-A[2][0]*x-A[2][1]*y) / A[2][2];
x=newx;
y=newy;
z=newz;
printf("The solution is x=%0.5f, y=%0.5f, z=%0.5f: ",x,y,z);
return 0;
}
##Bisection
#include <stdio.h>
float f(float x){
float answer = (x*x*x) - (2*x) - 5;
return answer;
}
int main() {
float a,b;
printf("Enter Lower Interval: ");
scanf("%f",&a);
printf("Enter Upper Interval: ");
scanf("%f",&b);
if((f(a)*f(b))>=0){
printf("Bisection not possible!");
return 1;
}
float c;
int iter;
printf("Enter Number of Iterations: ");
scanf("%d",&iter);
printf("+--------------------------------------------------------------------------------------------
+\n");
printf("Iter no. \t \t a \t \t b \t \t c \t \t f(c) \t \t New Interval\n");
printf("+--------------------------------------------------------------------------------------------
+\n");
float olda,oldb;
for(int i=0;i<iter;i++){
olda=a;
oldb=b;
c= (a+b)/2;
if((f(a)*f(c))<0) b=c;
else a=c;
printf("%d \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t \t
[%0.3f,%0.3f]",(i+1),olda,oldb,c,f(c),a,b);
printf("\n");
}
printf("Approximate root after iteration ends: %0.3f",c);
return 0;
}
##Newton Rapson
#include <stdio.h>
float f(float x){
float answer = (x*x*x) - (x) - 2;
return answer;
}
float df(float dx){
float derivative = (3*dx*dx) - 1;
return derivative;
}
int main() {
float initial_approximation;
printf("Enter Initial Approximation: ");
scanf("%f",&initial_approximation);
int iter;
printf("Enter Number of Iterations: ");
scanf("%d",&iter);
printf("+--------------------------------------------------------------------------------------------
+\n");
printf("Iter no. \t \t f(x) \t \t f'(x) \t\t X_old \t \t Result\n");
printf("+--------------------------------------------------------------------------------------------
+\n");
float X_new,X_old;
X_old=initial_approximation;
for(int i=0;i<iter;i++){
X_new=X_old-(f(X_old)/df(X_old));
printf("%d \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t %0.3f\n",
(i+1),f(X_old),df(X_old),X_old,X_new);
X_old=X_new;
}
return 0;
}
##Regula Falsi
#include <stdio.h>
float f(float x){
float answer = (x*x*x) - (x) - 2;
return answer;
}
int main() {
float a,b;
printf("Enter Lower Interval: ");
scanf("%f",&a);
printf("Enter Upper Interval: ");
scanf("%f",&b);
int iter;
printf("Enter Number of Iterations: ");
scanf("%d",&iter);
if(f(a)*f(b)>=0){
printf("Regula Falsi Not Possible!");
return 1;
}
printf("+--------------------------------------------------------------------------------------------
+\n");
printf("Iter no. \t \t a \t \t f(a) \t \t b \t \t f(b) \t \t c \t \t f(c) \n");
printf("+--------------------------------------------------------------------------------------------
+\n");
float c;
for(int i=0;i<iter;i++){
c= (a*f(b)-b*f(a))/(f(b)-f(a));
printf("%d \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t %0.3f
\n",(i+1),a,f(a),b,f(b),c,f(c));
if(f(a)*f(c)<0) b=c;
else a=c;
printf("Approximate Solution is: %0.3f",c);
return 0;
}
##Euler
#include <stdio.h>
float f(float x, float y){
float answer = (x*x) + y;
return answer;
}
int main() {
float x,y;
printf("Enter the initial value of x:");
scanf("%f",&x);
printf("Enter the initial value of y:");
scanf("%f",&y);
float h;
printf("Enter the step size:");
scanf("%f",&h);
int iter;
printf("Enter number of iterations:");
scanf("%d",&iter);
float ynew,slope;
printf("Iter no. \t \t x \t \t y \t \t f(x,y) \t \t Modified y \n");
for(int i=0;i<iter;i++){
slope=f(x,y);
ynew=y+(h*slope);
printf("%d \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t %0.3f",(i+1),x,y,slope,ynew);
printf("\n");
x=x+h;
y=ynew;
printf("Final approximated root= %0.3f",ynew);
return 0;
}
##Trapezoidal for integration
#include <stdio.h>
float f(float x){
float answer = (x*x)+1;
return answer;
}
int main() {
float x,y;
printf("Enter lower interval:");
scanf("%f",&x);
printf("Enter upper interval:");
scanf("%f",&y);
int n;
printf("Enter number of sub-intervals:");
scanf("%d",&n);
float h=(y-x)/n;
float I=1;
float Sum=0;
for(int i=0;i<=n;i++){
if(i==0) Sum+=f(x);
else {
if(i==n) Sum+=f(y);
else Sum+=2*f(x+(i*h));
}
}
I=(h*Sum)/2;
printf("Final answer= %0.3f",I);
return 0;
}
##Simpson 1/3 Integration
#include <stdio.h>
float f(float x){
float answer = (x*x)+1;
return answer;
}
int main() {
float x,y;
printf("Enter lower interval:");
scanf("%f",&x);
printf("Enter upper interval:");
scanf("%f",&y);
int n;
printf("Enter number of sub-intervals (even):");
scanf("%d",&n);
float h=(y-x)/n;
float I=0;
float Sum=0;
for(int i=0;i<=n;i++){
if(i==0) Sum+=f(x);
else {
if(i==n) Sum+=f(y);
else {
if(i%2!=0) Sum+=4*f(x+(i*h));
else Sum+=2*f(x+(i*h));
}
}
}
I=(h*Sum)/3;
printf("Final answer= %0.3f",I);
return 0;
}
Rk2
#include <stdio.h>
float f(float x, float y){
float answer = (x*x) + y;
return answer;
}
int main() {
float x,y;
printf("Enter the initial value of x:");
scanf("%f",&x);
printf("Enter the initial value of y:");
scanf("%f",&y);
float h;
printf("Enter the step size:");
scanf("%f",&h);
int iter;
printf("Enter number of iterations:");
scanf("%d",&iter);
float ynew,k1,k2;
printf("Iter no. \t \t x \t \t y \t \t k1 \t \t k2 \t \t result \n");
for(int i=0;i<iter;i++){
k1=h*f(x,y);
k2=h*f(x+h,y+k1);
ynew=y+((k1+k2)/2);
printf("%d \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t %0.3f",(i+1),x,y,k1,k2,ynew);
printf("\n");
x=x+h;
y=ynew;
printf("Final approximated root= %0.3f",y);
return 0;
}
Rk4
#include <stdio.h>
float f(float x, float y){
float answer = (x*x) + y;
return answer;
}
int main() {
float x,y;
printf("Enter the initial value of x:");
scanf("%f",&x);
printf("Enter the initial value of y:");
scanf("%f",&y);
float h;
printf("Enter the step size:");
scanf("%f",&h);
int iter;
printf("Enter number of iterations:");
scanf("%d",&iter);
float ynew,k1,k2,k3,k4;
printf("Iter no. \t \t x \t \t y \t \t k1 \t \t k2 \t \t k3 \t \t k4 \t \t result \n");
for(int i=0;i<iter;i++){
k1=h*f(x,y);
k2=h*f(x+(h/2),y+(k1/2));
k3=h*f(x+(h/2),y+(k1/2));
k4=h*f(x+h,y+k3);
ynew=y+((k1+2*k2+2*k3+k4)/6);
printf("%d \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t %0.3f \t \t
%0.3f",(i+1),x,y,k1,k2,k3,k4,ynew);
printf("\n");
x=x+h;
y=ynew;
printf("Final approximated root= %0.3f",y);
return 0;
}
Gauss Eli
#include <stdio.h>
int main() {
float A[3][3], b[3];
float factor;
float x, y, z;
// Input coe icient matrix
printf("Enter the coe icients of the matrix:\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
printf("A[%d][%d] = ", i, j);
scanf("%f", &A[i][j]);
}
}
// Input constants
printf("\nEnter the constants:\n");
for(int i = 0; i < 3; i++) {
printf("b[%d] = ", i);
scanf("%f", &b[i]);
}
// -------- Forward Elimination --------
// Eliminate A[1][0]
factor = A[1][0] / A[0][0];
for(int j = 0; j < 3; j++) {
A[1][j] = A[1][j] - factor * A[0][j];
}
b[1] = b[1] - factor * b[0];
// Eliminate A[2][0]
factor = A[2][0] / A[0][0];
for(int j = 0; j < 3; j++) {
A[2][j] = A[2][j] - factor * A[0][j];
}
b[2] = b[2] - factor * b[0];
// Eliminate A[2][1]
factor = A[2][1] / A[1][1];
for(int j = 1; j < 3; j++) {
A[2][j] = A[2][j] - factor * A[1][j];
}
b[2] = b[2] - factor * b[1];
// -------- Back Substitution --------
z = b[2] / A[2][2];
y = (b[1] - A[1][2] * z) / A[1][1];
x = (b[0] - A[0][1] * y - A[0][2] * z) / A[0][0];
// Display Upper Triangular Matrix
printf("\nUpper Triangular Matrix:\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
printf("%8.2f", A[i][j]);
}
printf(" | %8.2f\n", b[i]);
}
// Display Solution
printf("\nSolution:\n");
printf("x = %.2f\n", x);
printf("y = %.2f\n", y);
printf("z = %.2f\n", z);
return 0;
}
#gass jordan
#include <stdio.h>
int main() {
int n;
printf("Enter the number of variables: ");
scanf("%d", &n);
float a[n][n + 1];
printf("Enter the augmented matrix:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j <= n; j++) {
scanf("%f", &a[i][j]);
}
}
// Gauss-Jordan Elimination
for(int i = 0; i < n; i++) {
// Make pivot element 1
float pivot = a[i][i];
for(int j = 0; j <= n; j++) {
a[i][j] /= pivot;
}
// Make all other elements in the pivot column 0
for(int k = 0; k < n; k++) {
if(k != i) {
float factor = a[k][i];
for(int j = 0; j <= n; j++) {
a[k][j] -= factor * a[i][j];
}
}
}
}
printf("\nReduced Row Echelon Form:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j <= n; j++) {
printf("%8.2f ", a[i][j]);
}
printf("\n");
}
printf("\nSolution:\n");
for(int i = 0; i < n; i++) {
printf("x%d = %.2f\n", i + 1, a[i][n]);
}
return 0;
}