0% found this document useful (0 votes)
191 views11 pages

CBNST 1

The document contains code implementations of several numerical analysis and interpolation techniques, including: 1) Gauss forward and backward interpolation algorithms using difference tables. 2) Newton forward and backward interpolation algorithms. 3) Bisection method for solving equations. 4) Calculation of relative, absolute, and percentage errors. 5) Langrange interpolation method. 6) Simpson's 1/3 and 3/8 integration rules. 7) Trapezoidal integration rule. 8) Second and fourth order Runge-Kutta methods for solving differential equations. 9) Newton divided difference formula. The code shows the implementation of these techniques through functions, loops, and calculations on arrays of

Uploaded by

akshat goel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views11 pages

CBNST 1

The document contains code implementations of several numerical analysis and interpolation techniques, including: 1) Gauss forward and backward interpolation algorithms using difference tables. 2) Newton forward and backward interpolation algorithms. 3) Bisection method for solving equations. 4) Calculation of relative, absolute, and percentage errors. 5) Langrange interpolation method. 6) Simpson's 1/3 and 3/8 integration rules. 7) Trapezoidal integration rule. 8) Second and fourth order Runge-Kutta methods for solving differential equations. 9) Newton divided difference formula. The code shows the implementation of these techniques through functions, loops, and calculations on arrays of

Uploaded by

akshat goel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Gauss Forward Interpolation implementation Code

#include <stdio.h>
#include <math.h>
int main()
{
float a[10][10],x,u1,u,y;
int i,j,n,n1,fact=1;
printf("enter the n\n");
scanf("%d",&n);
printf("enter the x: ");
for(i=0;i<n;i++)
scanf("%f",&a[i][0]);
printf("enter the y: ");
for(i=0;i<n;i++)
scanf("%f",&a[i][1]);
printf("enter the value to predict\n");
scanf("%f",&x);
for(j=2;j<n+1;j++)
{
for(i=0;i<n-j+1;i++)
a[i][j]=a[i+1][j-1]-a[i][j-1];
}
printf("the differnce table is \n");
for(i=0;i<n;i++)
{
for(j=0;j<=n-i;j++)
printf("%0.2f\t",a[i][j]);
printf("\n");
}
y=a[n/2][1];
printf("\n y: %f",y);
u=(x-a[n/2][0])/(a[1][0]-a[0][0]);
u1=u;
printf("\n%f",u1);
for(i=2;i<=n;i++)
{
y=y+(u1/fact)*a[(n-1)/i][i];
printf("%f\t",a[(n-1)/i][i]);
fact=fact*i;
if(i%2==0){
u1=u1*(u-(i/2));
printf("\n %f",u1);
}
else
{
u1=u1*(u+(i/2));
printf("\n %f",u1);

}
}
printf("\n\nthe desired value is %f",y);
return 0;
}

Gauss Bwd Interpolation Implementation Code


#include <stdio.h>
#include <math.h>
int main()
{
float a[10][10],x,u1,u,y;
int i,j,n,n1,fact=1;
printf("enter the n\n");
scanf("%d",&n);
printf("enter the x: ");
for(i=0;i<n;i++)
scanf("%f",&a[i][0]);
printf("enter the y: ");
for(i=0;i<n;i++)
scanf("%f",&a[i][1]);
printf("enter the value to predict\n");
scanf("%f",&x);
for(j=2;j<n+1;j++)
{
for(i=0;i<n-j+1;i++)
a[i][j]=a[i+1][j-1]-a[i][j-1];
}
printf("the differnce table is \n");
for(i=0;i<n;i++)
{
for(j=0;j<=n-i;j++)
printf("%0.2f\t",a[i][j]);
printf("\n");
}
y=a[n/2][1];
printf("\n y: %f",y);
u=(x-a[n/2][0])/(a[1][0]-a[0][0]);
u1=u;
printf("\n%f",u1);
for(i=2;i<=n;i++)
{
y=y+(u1/fact)*a[(n-2)/i][i];
printf("%f\t",a[(n-1)/i][i]);
fact=fact*i;
if(i%2==0){
u1=u1*(u+(i/2));
printf("\n %f",u1);
}
else
{
u1=u1*(u-(i/2));
printf("\n %f",u1);

}
}
printf("\n\nthe desired value is %f",y);
return 0;
}
#include<iostream>

using namespace std;

int main()
{
int n,i,j,p;
cout<<"Enter the value of n : ";
cin>>n;

float arr[n][n+1];
float x,y,u;

cout<<"Enter x : ";
for(i=0;i<n;i++)
{
cin>>x;
arr[i][0] = x;
}

cout<<"Enter y : ";
for(i=0;i<n;i++)
{
cin>>y;
arr[i][1] = y;
}

for(i=2;i<=n;i++)
{
for(j=0;j<n-i+1;j++)
{
arr[j][i] = arr[j+1][i-1] - arr[j][i-1];
}
}

cout<<"Difference Table \n";


for(i=0;i<n;i++)
{
for(j=0;j<=n-i;j++)
{
cout<<arr[i][j]<<" ";
}
cout<<endl;
}

/*Newton Backward Interpolation


------------------------------*/

float y1 = arr[n-1][1];
float x1;
cout<<"Enter x1 for backward interpolation : ";
cin>>x1;

u = (x1 - arr[n-1][0])/(arr[1][0]-arr[0][0]);
cout<<"u = "<<u<<endl;
float u1 = u;
int fact = 1;

for(i=2;i<=n;i++)
{
y1 = y1 + (u1*arr[n-i][i])/fact;

u1 = u1*(u+(i-1));
fact = fact*i;
}

cout<<"Backward Interpolation Answer = "<<y1<<endl;

/*Newton Forward Interpolation


------------------------------*/

y1 = arr[0][1];

float x2;
cout<<"Enter x2 for forward interpolation : ";
cin>>x2;
u = (x2 - arr[0][0])/(arr[1][0]-arr[0][0]);
cout<<"u = "<<u<<endl;
u1 = u;

for(i=2;i<=n;i++)
{
y1 = y1 + (u1*arr[0][i])/fact;
u1 = u1 * (u1-(i-1));
fact =fact*i;
}
cout<<"Forward Interpolation Answer = "<<y1<<endl;

return 0;

// HERE WE HAVE TO SOLVE EQUATION USING BISECTION METHOD


// THIS IS THE IMPLEMENTATION OF BISECTION METHOD

#include<bits/stdc++.h>
using namespace std;
#define EPSILON 0.01

// An example function whose solution is determined using


// Bisection Method. The function is x^3 - x^2 + 2
double func(double x)
{
return x*x*x - x*x + 2;
}
void bisection(double a, double b)
{
if (func(a) * func(b) >= 0)
{
cout << "You have not assumed right a and b\n";
return;
}

double c = a;
while ((b-a) >= EPSILON)
{
// Find middle point
c = (a+b)/2;

// Check if middle point is root


if (func(c) == 0.0)
break;

// Decide the side to repeat the steps


else if (func(c) < 0)
a = c;
else
b = c;
}
cout << "The value of root is : " << c;
}

int main()
{

double a =-200, b = 300;


cout<<"Value of f(-200) = "<<func(a)<<endl;
cout<<"Value of f(300) = "<<func(b)<<endl;
bisection(a, b);
return 0;
}

#include<bits/stdc++.h>

using namespace std;

void modd(double &s){


if(s < 0) s = (-1)*s;
}

double rel_err(double a, double b){


double ans = a-b;
modd(ans);

return ans;
}

int main(){
double a, b;

cout<<"Enter X : ";
cin >> a;
cout<<"Enter X' : ";
cin >> b;

double relative_error = rel_err(a, b);

double absolute_error = relative_error / a;

double percentage_error = absolute_error * 100;

cout<<"Realtive Error : "<<relative_error<<endl;


cout<<"Absolute Error : "<<absolute_error<<endl;
cout<<"Percentage Error : "<<percentage_error<<endl;

return 0;
}
....................................................................
Langranges
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
float x[100], y[100], xp, yp=0, p;
int i,j,n;

/* Input Section */
cout<<"Enter number of data: ";
cin>>n;
cout<<"Enter data:"<< endl;
for(i=1;i<=n;i++)
{
cout<<"x["<< i<<"] = ";
cin>>x[i];
cout<<"y["<< i<<"] = ";
cin>>y[i];
}
cout<<"Enter interpolation point: ";
cin>>xp;

/* Implementing Lagrange Interpolation */


for(i=1;i<=n;i++)
{
p=1;
for(j=1;j<=n;j++)
{
if(i!=j)
{
p = p* (xp - x[j])/(x[i] - x[j]);
}
}
yp = yp + p * y[i];
}
cout<< endl<<"Interpolated value at "<< xp<< " is "<< yp;

return 0;
}
...........................
simpson 1/3
#include<iostream>
#include<iomanip>

using namespace std;

float y(float x)
{
return 1/(1+x*x);
}

int main()
{
float x0,xn,h,s;
int i,n;

cout<<"Enter the x0,xn, no. of subintervals"<<endl;


cin>>x0>>xn>>n;
cout<<fixed;

h=(xn-x0)/n;
s=y(x0)+y(xn)+4*y(x0+h);

for(i=3;i<=n-1;i=i+2)
s +=4*y(x0+i*h)+2*y(x0+(i-1)*h);

cout<<"Value of integral is ";


cout<<setw(6)<<setprecision(4);
cout<<(h/3)*s<<endl;
return 0;
}

..........
simposon 3/8
#include <bits/stdc++.h>
using namespace std;

float y(float x) {
return (1 / (1 + x * x)); // Modify this function according to the function you want to
integrate
}

int main() {
float x0, xn, h, S;
int n;

cout << "Enter the lower limit (x0), upper limit (xn), and the number of subintervals: ";
cout << fixed;
cin >> x0 >> xn >> n;
h = (xn - x0) / n;
S = y(x0) + y(xn);

for (int i = 1; i < n; i++) {


if (i % 3 == 0)
S += 2 * y(x0 + i * h);
else
S += 3 * y(x0 + i * h);
}

S = 3 * h / 8 * S;

cout << "Value of integral using Simpson's 3/8 rule: " << setprecision(4) << S << endl;

return 0;
}
.............
trapezodial
#include <bits/stdc++.h>
using namespace std;

float y(float x) {
return 1 / (1 + x * x);
}

int main() {
float x0, xn, h, S;
int n;

cout << "Enter the lower limit (x0), upper limit (xn), and the number of subintervals: ";
cin >> x0 >> xn >> n;
cout << fixed;
h = (xn - x0) / n;
S = y(x0) + y(xn);

for (int i = 1; i <= n - 1; i++) {


S += 2 * y(x0 + i * h);
}

cout << "Value of the integral is: " << setw(6) << setprecision(4) << (h / 2) * S << endl;

return 0;
}
..................
range kutta 2order
#include <bits/stdc++.h>
using namespace std;

float f(float x, float y) {


return x + y * y;
}

int main() {
float xo, yo, h, xn, x, y, k1, k2;

cout << "Enter the initial values (x0, y0), step size (h), and endpoint (xn): ";
cin >> xo >> yo >> h >> xn;
x = xo;
y = yo;

cout << fixed;

while (x < xn) {


k1 = h * f(x, y);
k2 = h * f(x + h, y + k1);

y += (k1 + k2) / 2;
x += h;

cout << "when x=" << setw(8) << setprecision(4) << x;


cout << ", y=" << setw(8) << setprecision(4) << y << endl;
}

return 0;
}
..............

range kutta 4 order


#include <bits/stdc++.h>
using namespace std;

float f(float x, float y) {


return x + y;
}

int main() {
float xo, yo, h, xn, x, y, R1, R2, R3, R4, R;
cout << "Enter the initial values (x0, y0), step size (h), and endpoint (xn): ";
cin >> xo >> yo >> h >> xn;
cout << fixed;

x = xo;
y = yo;

while (x < xn) {


R1 = h * f(x, y);
R2 = h * f(x + h / 2, y + R1 / 2);
R3 = h * f(x + h / 2, y + R2 / 2);
R4 = h * f(x + h, y + R3);

R = (R1 + 2 * R2 + 2 * R3 + R4) / 6;

y += R;
x += h;

cout << "when x=" << setw(8) << setprecision(4) << x;


cout << ", y=" << setw(8) << setprecision(4) << y << endl;
}

return 0;
}
...................
newton divided
// CPP program for implementing
// Newton divided difference formula
#include <bits/stdc++.h>
using namespace std;

// Function to find the product term


float proterm(int i, float value, float x[])
{
float pro = 1;
for (int j = 0; j < i; j++) {
pro = pro * (value - x[j]);
}
return pro;
}

// Function for calculating


// divided difference table
void dividedDiffTable(float x[], float y[][10], int n)
{
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++) {
y[j][i] = (y[j][i - 1] - y[j + 1]
[i - 1]) / (x[j] - x[i + j]);
}
}
}

// Function for applying Newton's


// divided difference formula
float applyFormula(float value, float x[],
float y[][10], int n)
{
float sum = y[0][0];

for (int i = 1; i < n; i++) {


sum = sum + (proterm(i, value, x) * y[0][i]);
}
return sum;
}

// Function for displaying


// divided difference table
void printDiffTable(float y[][10],int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
cout << setprecision(4) <<
y[i][j] << "\t ";
}
cout << "\n";
}
}

// Driver Function
int main()
{
// number of inputs given
int n = 4;
float value, sum, y[10][10];
float x[] = { 5, 6, 9, 11 };

// y[][] is used for divided difference


// table where y[][0] is used for input
y[0][0] = 12;
y[1][0] = 13;
y[2][0] = 14;
y[3][0] = 16;

// calculating divided difference table


dividedDiffTable(x, y, n);

// displaying divided difference table


printDiffTable(y,n);

// value to be interpolated
value = 7;

// printing the value


cout << "\nValue at " << value << " is "
<< applyFormula(value, x, y, n) << endl;
return 0;
}

You might also like