Anda di halaman 1dari 12

# Category Archives: NUMERICAL

## ANALYSIS and LINEAR

PROGRAMMING Lab Programs
Write a program to solve first order ordinary differential
equations (initial value problem) using adaptive Runge-
Kutta method .
Jun 26
Posted by jaisha57
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
float fun(float x,float y)
{
return(x*y);
}
void main()
{
float y,x,h,e,k1,k2,k3,k4,k5,k;
int n,i;
clrscr();
printf(\n enter the value of x0:);
scanf(%f,&x);
printf(\n enter the value of y0:);
scanf(%f,&y);
printf(\n enter step length h: );
scanf(%f,&h);
printf(\n enter the vlaue of x at which y is needed :);
scanf(%f,&e);
n=(e-x)/h;
for(i=1;i<=n;i++)
{
k1=h*fun(x,y);
k2=h*fun(x+h/4,y+k1/4);
k3=h*fun(x+(3*h)/8,y+(3*k1)/32+(9*k2)/32);
k4=h*fun(x+(12*h)/13,y+(1932*k1/21971)+(7200*k2/2197)+(7296*k3/2197));
k5=h*fun(x+h,y+(439*k1/216)-8*k2+(3600*k3/513)-(845*k4/4104));
k=((25*k1/216)+(1048*k3/2565)+(2197*k4/4104)-k5/5);
y=y+k;
x=x+h;
printf(the value of y at x=%f is %f ,x,y);
}
getch();
}
Posted in NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs
Write a program to solve first and second order ordinary
differential equations (initial value problem) using Runge-
Kutta fourth order method.
Jun 26
Posted by jaisha57
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
float fun(float x,float y)
{
return(x+fabs(sqrt(y)));
}
void main()
{
float y,x,h,e,k1,k2,k3,k4,k;
int n,i;
clrscr();
printf(\n enter the value of x0:);
scanf(%f,&x);
printf(\n enter the value of y0:);
scanf(%f,&y);
printf(\n enter step length h: );
scanf(%f,&h);
printf(\n enter the vlaue of x at which y is needed :);
scanf(%f,&e);
n=(e-x)/h;
for(i=1;i<=n;i++)
{
printf(%d the iteration \n,i);
k1=h*fun(x,y);
k2=h*fun(x+h/2,y+k1/2);
k3=h*fun(x+h/2,y+k2/2);
k4=h*fun(x+h,y+k3);
k=(k1+2*k2+2*k3+k4)/6;
y=y+k;
x=x+h;
printf(\n the value of y at x=%f is %f,x,y);
}
getch();
}
Posted in NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs
Write a program to solve the system of equations Ax = b
using Gauss-Seidel method
Jun 26
Posted by jaisha57
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
float a[20][20],x[20],e,big,temp,relerror,sum;
int n,i,j,maxit,itr;
char ch;
clrscr();
printf(enter the size of the equation:);
scanf(%d,&n);
top:for(i=1;i<=n;i++)
{
printf(\n enter the co-efficient of the equation %d and rhs:\n,i);
for(j=1;j<=n+1;j++)
scanf(%f,&a[i][j]);
}
printf(\n enter the relative error and number of iteration:);
scanf(%f%d,&e,&maxit);
for(i=1;i<=n;i++)
x[i]=0;
for(itr=1;itr<=maxit;itr++)
{
big=0;
for(i=1;i<=n;i++)
{
sum=0;
for(j=1;j<=n;j++)
{
if(j!=i)
sum=sum+a[i][j]*x[j];
}
temp=(a[i][n+1]-sum)/a[i][i];
relerror=fabs((x[i]-temp)/temp);
if(relerror>big)
big=relerror;
x[i]=temp;
}
if(big<=e)
{
printf(\n converges to a solution \n);
for(i=1;i<=n;i++)
printf(%f\t,x[i]);
getch();
exit(1);
}
}
printf(\n does not converge is %d iteration \n,maxit);
printf(\n please try by interchanging any two equation \n);
printf(make diagonal entries pivotal \n );
printf(\n do you want to try(y/n):);
fflush(stdin);
ch=getchar();
if(ch==y)
goto top;
for(i=1;i<=n;i++)
printf(%f\t,x[i]);
getch();
}
Posted in NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs
Write a program to solve the system of equations Ax = b
using Gauss elimination method.
Jun 26
Posted by jaisha57
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
float u,e,z,a[20][20],temp,x[20],sum,max;
int m,n,j,i,p,k,q;
clrscr();
printf(enter the size of the equation);
scanf(%d,&n);
for(i=1;i<=n;i++)
{
printf(\n enter the co-eficient of the equation %d and RHS:\n,i);
for(j=1;j<=n+1;j++)
scanf(%f,&a[i][j]);
}
printf(\n enter the error allowed : \n);
scanf(%f,&e);
for(k=1;k<=n-1;k++)
{
max=abs(a[k][k]);
p=k;
for(m=k+1;m<=n;m++)
{
z=fabs(a[m][k]);
if(z>max)
{
max=abs(a[m][k]);
p=m;
}
}
if(max<=e)
{
printf(ill conditional equation:);
getch();
exit(1);
}
if(p==k)goto cont;
for(q=k;q<=n+1;q++)
{
temp=a[k][q];
a[k][q]=a[p][q];
a[p][q]=temp;
}
cont:for(i=k+1;i<=n;i++)
{
u=a[i][k]/a[k][k];
for(j=k;j<=n+1;j++)
a[i][j]=a[i][j]-u*a[k][j];
}
}
x[n]=a[n][n+1]/a[n][n];
printf(\nthe value of x%d is %f \n,n,x[n]);
for(i=n-1;i>=1;i)
{
sum=0;
for(j=i+1;j<=n;j++)
{
sum=sum+a[i][j]*x[j];
}
x[i]=(a[i][n+1]-sum)/a[i][i];
printf(\nthe value of x%2d is %f\n,i,x[i]);
}
getch();
}
Posted in NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs
Write a program to solve the system of equations Ax = b in
tridiagonal form using Thomas Algorithm.
Jun 26
Posted by jaisha57
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX_N 20
void tridge(float a[],float b[],float c[],float f[],int i,int iflag);
void main()
{
float a[MAX_N+1],b[MAX_N+1],c[MAX_N+1],f[MAX_N+1];
int n,i,j,iflag;
clrscr();
printf(\n what is the order n of system ?);
scanf(%d,&n);
printf(\n give b[1],c[1],rhs[1] for equation 1:);
scanf(%f%f%f,&b[1],&c[1],&f[1]);
for(i=2;i<=n-1;i++)
{
printf(\n give a[%d],b[%d],c[%d],rhs[%d] for the equation %d\n,i,i,i,i,i);
scanf(%f%f%f%f,&a[i],&b[i],&c[i],&f[i]);
}
printf(\n give a[n],b[n],rhs[n] for equation %d:,n);
scanf(%f%f%f,&a[n],&b[n],&f[n]);
iflag=0;
tridge(a,b,c,f,n,iflag);
printf(solution);
printf(\ni a[i]);
for(j=1;j<=n;j++)
printf(\n%d\t%f,j,f[j]);
printf(\n);
getch();
return;
}
void tridge(float a[],float b[],float c[],float f[],int n,int iflag)
{
const float zero=0.0;
int j;
if(iflag==0)
{
for(j=2;j<=n;j++)
{
if(b[j-1]==zero)
{
return;
}
a[j]=a[j]/b[j-1];
b[j]=b[j]-a[j]*c[j-1];
}
if(b[n]==zero)
{
return;
}
}
for(j=2;j<=n;j++)
f[j]=f[j]-a[j]*f[j-1];
f[n]=f[n]/b[n];
for(j=n-1;j>=1;j)
f[j]=(f[j]-c[j]*f[j+1])/b[j];
return;
}
Posted in NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs
Write a program to find the integral of a function using
Simpsons 1/3rd and 3/8th rule using switch case.
Jun 26
Posted by jaisha57
#include<stdio.h>
#include<conio.h>
#include<math.h>
float sim_1(float,float,int);
float sim_2(float,float,int);
int i;
float sum;
float fun(float x)
{
return(1/(1+pow(x,2)));
}
void main()
{
float x0,x1,sum,result;
int n,cho=0;
clrscr();
printf(enter the lower and upper limit \n);
scanf(%f%f,&x0,&x1);
printf(enter number of intervals:\n);
scan:scanf(%d,&n);
if(cho==0)
{
top:printf(\t enter choice \n);
printf(\n 1.for simpsons 1/3 rule\n);
printf(\n 2.for simpsons 3/8 rule\n);
scanf(%d,&cho);
}
switch(cho)
{
case 1:if(n%2==0)
result=sim_1(x0,x1,n);
else
{
printf(wrong choice of interval);
goto scan;
}
break;
case 2:if(n%3==0)
result=sim_2(x0,x1,n);
else
{
printf(wrong choice of intervals \n );
printf(\n please enter a multiple of 3 \n);
goto scan;
}
break;
default:printf(\n wrong choice enter again:);
goto top;
}
printf(\n the result=%f,result);
getch();
}
float sim_1(float x0,float x1,int n)
{
float result,h;
h=(x1-x0)/n;
sum=fun(x0)+fun(x1);
for(i=1;i<n;i++)
{
if(i%2==0)
sum=sum+2*fun(x0+i*h);
else
sum=sum+4*fun(x0+i*h);
}
result=sum*(h/3);
return(result);
}
float sim_2(float x0,float x1,int n)
{
float result,h;
h=(x1-x0)/n;
sum=fun(x0)+fun(x1);
for(i=1;i<n;i++)
{
if(i%3==0)
sum=sum+2*fun(x0+i*h);
else
sum=sum+3*fun(x0+i*h);
}
result=sum*(3*h/8);
return(result);
}
Posted in NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs
Write a program to find the integral of a function using
Trapezoidal rule.
Jun 26
Posted by jaisha57
#include<stdio.h>
#include<conio.h>
#include<math.h>
float trap(float,float,int);
int i;
float sum;
float fun(float);
void main()
{
float x0,x1,res;
int n;
clrscr();
printf(\n enter the lower and upper limits :);
scanf(%f%f,&x0,&x1);
printf(\n enter the number of intervals :);
scanf(%d,&n);
res=trap(x0,x1,n);
printf(\n TRAPEZOIDAL RULE =%f \n,res);
getch();
}
float trap(float x0,float x1,int n)
{
float h,result;
h=(x1-x0)/n;
sum=fun(x0)+fun(x1);
for(i=1;i<n;i++)
sum=sum+2*fun(i*h);
result=(sum*h)/2;
return(result);
}
float fun(float x)
{
return(1/(1+pow(x,2)));
}
Posted in NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs
Write a program to find the simple/multiple roots of f (x)
= 0 using Newton Raphson method.
Jun 26
Posted by jaisha57
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float a,x1,x2;
int i;
float func(float);
float func1(float);
clrscr();
printf(\n enter the initial value);
scanf(%f,&a);
x1=a;
printf(a=%f,x1);
x2=(x1-(func(x1)/func1(x1)));
printf(\nx2=%f,x2);
for(i=1;x1!=x2;i++)
{
printf(\n(%d)%f,i,x2);
x1=x2;
x2=x1-(func(x1)/func1(x1));
}
printf(\n the root is %f converge is %d approximately,x2,i);
getch();
return(0);
}
float func(float x)
{
return(pow(x,3)+(-2*x-5));
}
float func1(float x)
{
return((3*pow(x,2)-2));
}
Posted in NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs
Write a program to find the roots of an equation f (x) = 0
using Bisection method.
Jun 26
Posted by jaisha57
#include<stdio.h>
#include<conio.h>
#include<math.h>
float fun(float x)
{
return(pow(x,3)-x-1);
}
void main()
{
float a,b,eps,x;
int i=0;
clrscr();
printf(\n enter the lower and upper limit );
scanf(%f%f,&a,&b);
printf(\n enter the epsilon value);
scanf(%f,&eps);
if((fun(a)*fun(b))>0)
printf(\n starting value is unsuitable );
else
while(fabs((b-a)/b)>eps)
{
x=(a+b)/2;
i++;
if((fun(x)*fun(a))>0)
a=x;
else
b=x;
}
printf(\n solution converges to a root \n);
printf(\n number of iterationo=%d\n,i);
printf(%f\n,x);
getch();
}
Posted in NUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs