Lagrange Interpolation
#include<stdio.h>
#include<conio.h>
void main()
{
float ax[100],ay[100],x,y=0,temp;
int n,i,j;
printf("Enter n: ");
scanf("%d",&n);
printf("Enter given data...");
for(i=0;i<=n;i++)
{
printf("\nEnter x[%d] and y[%d]: ",i,i);
scanf("%f%f",&ax[i],&ay[i]);
}
printf("Enter x: ");
scanf("%f",&x);
for(i=0;i<=n;i++)
{
temp=1;
for(j=0;j<=n;j++)
{
if(j!=i)
{
temp*=(x-ax[j])/(ax[i]-ax[j]);
}
}
y+=temp*ay[i];
}
printf("Hence, f(%f) = %f",x,y);
getch();
}
OUTPUT:
C Program for Least Square Method
(Regression Analysis)
#include<stdio.h>
#include<conio.h>
void main()
{
float x[100],y[100],sumx=0,sumx2=0,sumy=0,sumyx=0,b,a;
int i,n;
printf("Enter n: ");
scanf("%d",&n);
printf("Enter the values:\n");
for(i=0;i<n;i++)
{
printf("Enter x[%d] and y[%d]: ",i,i);
scanf("%f%f",&x[i],&y[i]);
}
for(i=0;i<n;i++)
{
sumx=sumx+x[i];
sumx2=sumx2+x[i]*x[i];
sumy=sumy+y[i];
sumyx=sumyx+y[i]*x[i];
}
//for y=ax+b
b=(sumx2*sumy-sumyx*sumx)/(n*sumx2-sumx*sumx);
a=(n*sumyx-sumx*sumy)/(n*sumx2-sumx*sumx);
printf("\nHence, the required eqn is y = %fx + %f",a,b);
getch();
}
OUTPUT:
C Program for Euler Method
#include <stdio.h>
#include <conio.h>
float f(float x,float y)
{return x*y;}
void main()
{
float x0,y0,xn,yn,h;
printf("Enter x0, y0 and h: ");
scanf("%f%f%f",&x0,&y0,&h);
printf("Enter xn: ");
scanf("%f",&xn);
do
{
yn=y0+h*f(x0,y0);
x0=x0+h;
y0=yn;
}while(x0<xn);
printf("y(%0.2f) = %0.4f Ans",xn,yn);
getch();
}
OUTPUT:
C Program for Gauss Elimination
Method
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
float A[20][20],c,x[10],sum;
printf("\nEnter the order of matrix: ");
scanf("%d",&n);
printf("\nEnter the elements of augmented matrix row-wise:\n\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf("A[%d][%d] : ", i,j);
scanf("%f",&A[i][j]);
}
}
/* loop for the generation of upper triangular matrix*/
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(j>i)
{
c=A[j][i]/A[i][i];
for(k=1; k<=n+1; k++)
{
A[j][k]=A[j][k]-c*A[i][k];
}
}
}
}
/* Upper Traingular matrix */
printf("\nThe Upper Triangular matrix is: \n\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf("%f ",A[i][j]);
}
printf("\n");
}
/* initializing x[i] to zeros */
for(i=1; i<=n; i++)
{
x[i]=0;
}
/* loop is for backward substitution */
printf("\nAfter applying Backward Substitution: \n");
for(i=n; i>=1; i--)
{
sum=0;
for(j=1; j<=n; j++)
{
if(i!=j)
sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]-sum)/A[i][i];
}
printf("\nThe solution is: \n");
for(i=1; i<=n; i++)
{
printf("\nx%d=%f\t",i,x[i]);
/* x1, x2, x3 are the required solutions */
}
getch();
}
OUTPUT:
C Program for Runge-Kutta-4 (RK-4)
Method
#include <stdio.h>
#include <conio.h>
#include <math.h>
float f(float x,float y)
{
return ((y*y-x*x)/(y*y+x*x));
//y'=f(x,y)=equation
}
void main()
{
float x0,y0,h,xn,yn;
printf("Enter x0 and y0: ");
scanf("%f%f",&x0,&y0); //y(x0)=y0
printf("Enter xn: ");
scanf("%f",&xn);
printf("Enter interval(h): ");
scanf("%f",&h);
do
{
float m1=f(x0,y0);
float m2=f(x0+h/2,y0+m1*h/2);
float m3=f(x0+h/2,y0+m2*h/2);
float m4=f(x0+h,y0+m3*h);
float m=(m1+2*m2+2*m3+m4)/6;
yn=y0+m*h;
//for next iteration
x0=x0+h;
y0=yn;
}while(x0<xn);
printf("\n\nHence, y(%0.1f)=%0.4f",xn,yn);
getch();
}
OUTPUT:
Code for Simpson's 3/8 rule
#include <stdio.h>
#include <conio.h>
#include <math.h>
float f(float x)
{
return (x*x+(sin(x)/x));
//f(x)=x*x+sin(x)/x;
}
void main()
{
float a,b,h,x,sum=0;
int n,i,k;
printf("Enter a and b: ");
scanf("%f%f",&a,&b);
printf("Here, n=3 for Simpson's 3/8 rule");
printf("\nn>3 for Composite Simpson's 3/8 rule\n");
printf("So, Enter n: ");
scanf("%d",&n);
h=(b-a)/n;
for(x=a,i=0,k=3;x<=b,i<=n;x=x+h,i++,k=k+3)
{
if(i==0||i==n)
sum=sum+f(x);
else if(i==k)
sum=sum+2*f(x);
else
sum=sum+3*f(x);
}
sum=3*h/8*sum;
printf("\nI=%f",sum);
getch();
}
OUTPUT:
Code for Simpson's 1/3 rule
#include <stdio.h>
#include <conio.h>
#include <math.h>
float f(float x)
{
return (sqrt(sin(x)));
//f(x)=sqrt(sin(x));
}
void main()
{
float a,b,h,x,sum=0;
int n,i,k;
printf("Enter a and b: ");
scanf("%f%f",&a,&b);
printf("Here, n=2 for Simpson's 1/3 rule");
printf("\nn>2 for Composite Simpson's 1/3 rule\n");
printf("So, Enter n: ");
scanf("%d",&n);
h=(b-a)/n;
for(x=a,i=0,k=1;x<=b,i<=n;x=x+h,i++,k=k+2)
{
if(i==0||i==n)
sum=sum+f(x);
else if(i==k)
sum=sum+4*f(x);
else
sum=sum+2*f(x);
}
sum=h/3*sum;
printf("\nI=%f",sum);
getch();
}
OUTPUT:
Simpson's 1/3 Rule
It uses three terms of Newton-Cotes formula, so order (n)=3-1=2. Hence, it
uses 2nd order polynomial.
Composite Simpson's 1/3 Rule
Simpson’s 3/8 Rule
It uses four terms of Newton-Cotes formula, so order (n)=4-1=3. Hence, it
uses 3rd order polynomial.
Composite Simpson’s 3/8 Rule
Trapezoidal Rule
It uses two points of "Newton-Cotes formula". So, it is also called 2-point formula of
Newton-Cotes. Since it takes two points, order (n) = 2-1 = 1. If the range to be integrated is
large, the two-point interval is divided into multiple segments and is called "Composite
Trapezoidal Rule".
Using Trapezoidal rule,
Using Composite Trapezoidal rule,
Example:
C Code for Trapezoidal Rule
#include <stdio.h>
#include <conio.h>
#include <math.h>
float f(float x)
{
return (exp(x)); //f(x)=exp(x);
}
void main()
{
float a,b,h,x,sum=0;
int n;
printf("Enter a and b: ");
scanf("%f%f",&a,&b);
printf("Here, n=1 for Trapezoidal rule"); printf("\nn>1 for
Composite Trapezoidal rule\n");
printf("So, Enter n: ");
scanf("%d",&n);
h=(b-a)/n;
for(x=a;x<=b;x=x+h)
{
if(x==a)
sum=sum+f(x);
else if(x==b)
sum=sum+f(x);
else
sum=sum+2*f(x);
}
sum=h/2*sum;
printf("\nI=%f",sum);
getch();
}
OUTPUT:
C Program for Gauss-Legendre
Integration
//for integration of f(x) from a to b.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void GaussLegendre(float,float,int);
float f(float x){return (exp(x));}
float g(float a,float b,float z)
{float x=(b-a)/2*z+(b+a)/2;
return (exp(x));}
void main()
{
float a,b;
int n;
printf("Enter a and b: ");
scanf("%f%f",&a,&b);
INPUT:
printf("Enter 2 for 2-point formula: \n");
printf("Enter 3 for 3-point formula: \n");
printf("Enter 4 for 4-point formula: \n");
scanf("%d",&n);
switch(n)
{
case 2:
printf("Using 2-point Formula::\n");
GaussLegendre(a,b,n);
break;
case 3:
printf("Using 3-point Formula::\n");
GaussLegendre(a,b,n);
break;
case 4:
printf("Using 4-point Formula::\n");
GaussLegendre(a,b,n);
break;
default:
printf("INVALID\n");
goto INPUT;
}
getch();
}
void GaussLegendre(float a,float b,int n)
{
float I;
if(a==-1 && b==1)
{
if(n==2)
{
I=1*f(-1/sqrt(3))+1*f(1/sqrt(3));
printf("I=%f",I);
}
if(n==3)
{
I=5/9*f(-sqrt(3/5))+8/9*f(0)+5/9*f(sqrt(3/5));
printf("I=%f",I);
}
if(n==4)
{
I=0.34785*f(-0.86114)+0.65215*f(-
0.33998)+0.65215*f(0.33998)+0.34785*f(0.86114);
printf("I=%f",I);
}
}
else
{
if(n==2)
{
I=(b-a)/2*(1*g(a,b,-1/sqrt(3))+1*g(a,b,1/sqrt(3)));
printf("I=%f",I);
}
if(n==3)
{
I=(b-a)/2*(5/9*g(a,b,-sqrt(3/5))+8/9*g(a,b,0)+5/9*g(a,b,sqrt(3/5)));
printf("I=%f",I);
}
if(n==4)
{
I=(b-a)/2*(0.34785*g(a,b,-0.86114)+0.65215*g(a,b,-
0.33998)+0.65215*g(a,b,0.33998)+0.34785*g(a,b,0.86114));
printf("I=%f",I);
}
}
}
OUTPUT:
For f(x)=exp(x) from -1 to 1
For f(x)=exp(x) from 0 to 1
Algorithm for Gauss-Legendre
Integration
Start.
For given integration, input lower limit a, upper limit b.
If (a=-1 && b=1)
For n-point formula,
where wi=w1,w2,w3,.... are weights
and
xi=x1,x2,x3,.... are respective points.
Else
For n-point formula,
where wi=w1,w2,w3,.... are weights
and
zi=z1,z2,z3,.... are respective points same as xi.
c=(b-a)/2.
Stop.
Algorithm for Gauss-Seidel Method
Start
Given system is:
a1x+b1y+c1z=d1
a2x+b2y+c2z=d2
a3x+b3y+c3z=d3
Convert the 1st equation in terms of 1st variable, 2nd
equation in terms of 2nd variable and so on.
x=(d1-b1y-c1z)/a1
y=(d2-c2z-a2x)/b2
z=(d3-a3x-b3y)/c3
Assume initial guesses as x0,y0,z0.
Substituting x0,y0,z0, find x1, y1, z1 from above converted
form as
x1=(d1-b1y0-c1z0)/a1
y1=(d2-c2z0-a2x1)/b2
z1=(d3-a3x1-b3y1)/c3
If |x0-x1|<accuracy && |y0-y1|<accuracy&& |z0-
z1|<accuracy, required roots are x1,y1,z1, Else set
x0=x1,y0=y1,z0=z1 and go to step (5).
End
C Program for Gauss-Seidel Method >>
0 Comments
Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest
Algorithm for Gauss-Jacobi Method
Start
Given system is:
a1x+b1y+c1z=d1
a2x+b2y+c2z=d2
a3x+b3y+c3z=d3
Convert the 1st equation in terms of 1st variable, 2nd
equation in terms of 2nd variable and so on.
x=(d1-b1y-c1z)/a1
y=(d2-c2z-a2x)/b2
z=(d3-a3x-b3y)/c3
Assume initial guesses as x0,y0,z0.
Substituting x0,y0,z0, find x1, y1, z1 from above converted
form as
x1=(d1-b1y0-c1z0)/a1
y1=(d2-c2z0-a2x0)/b2
z1=(d3-a3x0-b3y0)/c3
If |x0-x1|<accuracy && |y0-y1|<accuracy&& |z0-
z1|<accuracy, required roots are x1,y1,z1, Else set
x0=x1,y0=y1,z0=z1 and go to step (5).
End
C Program for Gauss-Seidel Method
// Given system is:
// 20x+y-2z=17
// 3x+20y-z=-18
// 2x-3y+20z=25
#include <stdio.h>
#include <conio.h>
#include <math.h>
float f(float x,float y,float z)
{
return ((17-y+2*z)/20);
// x=(17-y+2*z)/20
}
float s(float x,float y,float z)
{
return ((-18+z-3*x)/20);
// y=(-18+z-3*x)/20
}
float t(float x,float y,float z)
{
return ((25-2*x+3*y)/20);
// z=(25-2*x+3*y)/20
}
void main()
{
float x0,y0,z0,x1=0,y1=0,z1=0,tempx,tempy,tempz,acc=0.0001;
int iteration=0;
printf("Enter initial guesses:\n");
scanf("%f%f%f",&x0,&y0,&z0);
do
{
tempx=x1;
tempy=y1;
tempz=z1;
x1=f(x0,y0,z0);
y1=s(x1,y0,z0);
z1=t(x1,y1,z0);
iteration++;
x0=x1;
y0=y1;
z0=z1;
}while(fabs(tempx-x1)>acc && fabs(tempy-y1)>acc && fabs(tempz-
z1)>acc);
printf("\n\nFinally,\n");
printf("x=%f Ans\ny=%f Ans\nz=%f Ans\n",x1,y1,z1);
printf("Iteration=%d",iteration);
getch();
}
OUTPUT:
C Program for Gauss-Jacobi Method
// Given system is:
// 20x+y-2z=17
// 3x+20y-z=-18
// 2x-3y+20z=25
#include <stdio.h>
#include <conio.h>
#include <math.h>
float f(float x,float y,float z)
{
return ((17-y+2*z)/20);
//x=(17-y+2*z)/20
}
float s(float x,float y,float z)
{
return ((-18+z-3*x)/20);
//y=(-18+z-3*x)/20
}
float t(float x,float y,float z)
{
return ((25-2*x+3*y)/20);
//z=(25-2*x+3*y)/20
}
void main()
{
float x0,y0,z0,x1=0,y1=0,z1=0,tempx,tempy,tempz,acc=0.0001;
int iteration=0;
printf("Enter initial guesses:\n");
scanf("%f%f%f",&x0,&y0,&z0);
do
{
tempx=x1;
tempy=y1;
tempz=z1;
x1=f(x0,y0,z0);
y1=s(x0,y0,z0);
z1=t(x0,y0,z0);
iteration++;
x0=x1;
y0=y1;
z0=z1;
}while(fabs(tempx-x1)>acc && fabs(tempy-y1)>acc && fabs(tempz-
z1)>acc);
printf("\n\nFinally,\n");
printf("x=%f Ans\ny=%f Ans\nz=%f Ans\n",x1,y1,z1);
printf("Iteration=%d",iteration);
getch();
}
OUTPUT:
Gauss-Seidel Method
Q. Solve the following system of linear equation using Gauss-Seidel method.
20x+y-2z=17
3x+20y-z=-18
2x-3y+20z=25
Solution:
Lagrange Interpolation
#include<stdio.h>
#include<conio.h>
void main()
{
float ax[100],ay[100],x,y=0,temp;
int n,i,j;
printf("Enter n: ");
scanf("%d",&n);
printf("Enter given data...");
for(i=0;i<=n;i++)
{
printf("\nEnter x[%d] and y[%d]: ",i,i);
scanf("%f%f",&ax[i],&ay[i]);
}
printf("Enter x: ");
scanf("%f",&x);
for(i=0;i<=n;i++)
{
temp=1;
for(j=0;j<=n;j++)
{
if(j!=i)
{
temp*=(x-ax[j])/(ax[i]-ax[j]);
}
}
y+=temp*ay[i];
}
printf("Hence, f(%f) = %f",x,y);
getch();
}
OUTPUT: