Crush NM

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

BCA 1styear(2nd Sem) 2023-24

Numerical Method [practical List]


1. Write a program in c for finding real roots of given equation x^3-4x-9=0
using Bisection method.
2. Write a program in c for finding real roots of given equation x*log10(x)-
1.2=0 using False position method.
3. Write a program in c for finding real roots of given equation 3x-cos(x)-1=0
using Newton Raphson method.
4. Write a program in c for finding real roots of given equation x^3-2x-5=0
using Secant method.
5. Write a program in c for finding real roots of given equation
x^3+2x^2+10x=20 using Muller’s method.
6. Write a program in c to solve the following system of equations by Gauss
elimination method.
x+y+z=9; 2x-3y+4z=13; 3x+4y+5z=40
7. Write a program in c to solve the following system of equations by Gauss
elimination method with partial pivoting.
3x+2y-4z=3; 2x+3y+3z=15; 5x-3y+z=14
8. Write a program in c to solve the following system of equations by Gauss
Jordan method. 10x-7y+5z=9; 3x+6y= -9; 9x+3y-2z=-1
9. Write a program in c to solve the following system of equations by Matrix
inversion method for two unknown variables.
2x+y=5; 3x-y=1
10.Write a program in c to find value of y when x=10 by Lagrange Interpolation
method.
X 5 7 11 13 17

Y 150 392 1452 2366 5202

11.Write a program in c to demonstrate linear interpolation method(Spline


interpolation).
12.Write a program in c to calculate regression equation of x on y from the
following data by Least Square Regression fitting.
X 50 70 100 120

Y 12 15 21 25

13.Write a program in c to find numerical integration of equation 1/(1+x2 )


using Trapezoidal rule.
14.Write a program in c to numerical integration of equation 1/(1+x2 +x) using
Simpsons 1/3rd rule.
15.Write a program in c to find an approximate value of y when x=1 with initial
condition x0=0 and y0=0 and width(h)=0.1 of differential equation dy/dx=(x-
y)/(x+y) using Runge Kutta 2 nd order method.
16.Write a program in c to c to find an approximate value of y when x=0.4 with
initial condition x0=0 and y0=1 and width(h)=0.1 of differential equation
dy/dx=(y2 -x 2 )/(y2 +x2 ) using Runge Kutta 4th order method.
Practical – 1
#include< stdio.h>
#include< math.h>
float fun (float x)
{
return (x*x*x - 4*x - 9);
}
void bisection (float *x, float a, float b, int *itr)
{
*x=(a+b)/2;
++(*itr);
printf("Iteration no. %3d X = %7.5f\n", *itr, *x);
}
void main ()
{
int itr = 0, maxmitr;
float x, a, b, allerr, x1;
printf("\nEnter the values of a, b, allowed error and maximum iterations:\n");
scanf("%f %f %f %d", &a, &b, &allerr, &maxmitr);
bisection (&x, a, b, &itr);
do
{
if (fun(a)*fun(x) < 0)
b=x;
else
a=x;
bisection (&x1, a, b, &itr);
if (fabs(x1-x) < allerr)
{
printf("After %d iterations, root = %6.4f\n", itr, x1);
return 0;
}
x=x1;
}
while (itr < maxmitr);
printf("The solution does not converge or iterations are not sufficient");
return 1;
}
Practical – 2
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*log10(x) - 1.2
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1;
clrscr();
up:
printf("\nEnter two initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
f0 = f(x0);
f1 = f(x1);
if( f0*f1 > 0.0)
{
printf("Incorrect Initial Guesses.\n");
goto up;
}
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
x2 = x0 - (x0-x1) * f0/(f0-f1);
f2 = f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2);
if(f0*f2 < 0)
{
x1 = x2;
f1 = f2;
}
else
{
x0 = x2;
f0 = f2;
}
step = step + 1;
}while(fabs(f2)>e);
printf("\nRoot is: %f", x2);
getch();
return 0;
}
Practical – 3
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return 3*x-cos(x)-1;
}
float of (float x)
{
{
return 3+sin(x);
}
void main()
{
int itr,maxmitr;
float h,x0,x1,allerr;
printf("\n Enter x0 allowed error and maximum iteration:");
scanf("%f%f%d",&x0,&allerr,&maxmitr);
for(itr=1;itr<=maxmitr;itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
printf("At iteration No.%3d,x0=%9.6f\n",itr,x1);
if(fabs(h)<allerr)
{
printf("After %3d iteration,root=%8.6f\n",itr,x1);
return 0;
}
x0=x;
}
getch();
}
Practical – 4
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return((x*x*x)-(2*x)-5);
}
void secant(float*a,float*b)
{float c=(*a*f(*b)-*b*f(*a)/(f(*b)-f(*a));
*a=*b;
*b=c;
}
int main()
{
float a,b,c,e;
int main()
{
clrscr();
printf("Enter the values of a,b,allowed error and number of iteration:");
scanf("%f%f%f%d",&a,&b,&e,&n);
if(f(a)==f(b))
{
printf("Solution cannot be found");
return-1;
getch();
}
do
{
secant(&a,&be);
printf("Iteration No-%d x=%f\n",count,b);
count ++;
if(count==n)
{
break;
}
}while(fabs(f(b))>e);
printf("The required solution is %f\n",b);
getch();
return o;
}
}
Practical – 5
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return pow(x,3)+2*x*x+10*x-20;
}
void calculate Root(float*a,float*b,float*c,float*result,float err)
{
float f1=f(*a);
float f2=f(*b);
float f3=f(*c);
float d1=f1-f3;
float d2=f2-f3;
float h1=*a-*c;
float h2=*b-*c;
float a0=f3;
float a1=(((d2*pow(h1,2))-(d1*pow(h2,2)))/((h1*h2)*(h1-h2)));
float a2=(((d1*h2)-(d2*h1)))/((h1*h2)*(h1-h2)));
float x=((-2*a0)/(a1+fabs(sqrt(a1*a1-4*a0*a2))));
float 4=((-2*a0)/(a1-fabs(sqrt(a1*a1-4*a0*a2))));
if(x>=4)
*result=x+*c;
else
*result=4+*c
}
int main()
{
float a,b,c,err;
int maxitr;
clrscr();
printf("Enter a,b,c,maximum iteration and err tolerance");
scanf("%f%f%f%d%f",&a,&b,&c,&maxitr,&err);
for(int i=0;i<maxitr,i++)
{
calculate Root(&a,&b,&c,&result,err);
if(fabs(result-c)<=err)
break;
a=b;
b=c;
c=result;
printf("current iteration %d x=%f\n",c);
}
if(fabs(result-c)<=err)
printf("The root is %f\n",result);
else
printf("Root can't be found using muller method\n");
return 0;
getch();
}
Practical – 6
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define SIZE 10
int main()
{
float a[SIZE][SIZE], x[SIZE], ratio;
int i,j,k,n;
clrscr();
printf("Enter number of unknowns: ");
scanf("%d", &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]);
}
}
for(i=1;i<=n-1;i++)
{
if(a[i][i] == 0.0)
{
printf("Mathematical Error!");
exit(0);
}
for(j=i+1;j<=n;j++)
{
ratio = a[j][i]/a[i][i];
for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
x[n] = a[n][n+1]/a[n][n];
for(i=n-1;i>=1;i--)
{
x[i] = a[i][n+1];
for(j=i+1;j<=n;j++)
{
x[i] = x[i] - a[i][j]*x[j];
}
x[i] = x[i]/a[i][i];
}
printf("\nSolution:\n");
for(i=1;i<=n;i++)
{
printf("x[%d] = %0.3f\n",i, x[i]);
}
getch();
return(0);
}
Practical – 7
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define MAXN 10
void partial_pivot(double A[MAXN][MAXN+1],int n)
{
int i,j,k;
double temp;
for(i=0;i<n;i++)
{
int pivot_row=i;
for(j=i+1;j<n;j++)
{
if(abs(A[j][i])>abs(A[pivot_row][i]))
{
pivot_row=j;
}
}
if(pivot_row!=i)
{
for(j=i;j<=n;j++)
{
temp=A[i][j];
A[i][j]=A[pivot_row][j];
A[pivot_row][j]=temp;
}
}
for(j=i+1;j<n;j++)
{
double factor=A[j][i]/A[i][i];
for(k=i;k<n;k++)
{
A[j][k]=factor*A[i][k];
}
}
}
}
void back_subtitute(double A[MAXN][MAXN+1],int n,double x[MAXN])
{
int i,j;
for(i=n-1;i>=0;i--)
{
double sum=0;
for(j=i+1;j<n;j++)
{
sum+=A[i][j]*x[j];
}
x[i]=(A[i][n]-sum)/A[i][i];
}
}
int main()
{
int n=3,i;
double A[MAXN][MAXN+1]={{3.0,2.0,-4.0,3.0},{2.0,3.0,3.0,15.0},{5.0,-
3,1.0,14.0}};
double x[MAXN];
clrscr();
partial_pivot(A,n);
back_subtitute(A,n,x);
printf("Solution for the system:\n");
for(i=0;i<n;i++)
{
printf("%f\n",x[i]);
}
return 0;
}
Practical – 8
#include<stdio.h>
int main()
{
int i,j,k,n;
float A[20][20],c,x[10];
printf("\nEnter the size of matrix: ");
scanf("%d",&n);
printf("\nEnter the elements of augmented matrix row-wise:\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]);
}
}
for(j=1; j<=n; j++)
{
for(i=1; i<=n; i++)
{
if(i!=j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
printf("\nThe solution is:\n");
for(i=1; i<=n; i++)
{
x[i]=A[i][n+1]/A[i][i];
printf("\n x%d=%f\n",i,x[i]);
}
return(0);
}
Practical – 9
#include<stdio.h>
#include<conio.h>
double determinant(double a,double b,double c,double d)
{
return a*d-b*c;
}
void main()
{
double a[2][2]={{2,3},{1,-1}};
double b[2]={5,1};
double det=determinant(a[0][0],a[0][1],a[1][0],a[1][1]);
double x=determinant(b[0],a[0][1],b[1],a[1][1])/det;
double y=determinant(a[0][0],b[0],a[1][0],b[1])/det;
clrscr();
if((det)< 1e-9)
{
printf("The system has a singular matrix\n");
}
printf("x=%.2If y=%.2If\n",x,y);
getch();
}
Practical – 10
#include<stdio.h>
#include<conio.h>
void main()
{
float x[100], y[100], xp, yp=0, p;
int i,j,n;
clrscr();
printf("Enter number of data: ");
scanf("%d", &n);
printf("Enter data:\n");
for(i=1;i<=n;i++)
{
printf("x[%d] = ", i);
scanf("%f", &x[i]);
printf("y[%d] = ", i);
scanf("%f", &y[i]);
}
printf("Enter interpolation point: ");
scanf("%f", &xp);
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];
}
printf("Interpolated value at %.3f is %.3f.", xp, yp);
getch();
}
Practical – 11
#include<stdio.h>
#include<conio.h>
int main()
{
float x0,y0,x1,y1,xp,yp;
clrscr();
printf("Enter first point (x0,y0):\n");
scanf("%f%f",&x0,&y0);
printf("Enter second point (x1,y1):\n");
scanf("%f%f",&x1,&y1);
printf("Enter interpolation point: ");
scanf("%f", &xp);
yp = y0 + ((y1-y0)/(x1-x0)) * (xp - x0);
printf("Interpolated value at %0.3f is %0.3f", xp, yp);
getch();
return 0;
}
Practical – 12
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,j,k,n;
double x[10],y[10],a,b;
double x sum=0,y sum=0,xz sum=0,xy sum=0;
printf("Enter the number of data pairs to be entered");
scanf("%d",&n);
printf("Enter the x-axis values");
for(i=0;i<n;i++)
{
scanf("%lf",&x[i]);
}
printf("Enter the y-axis values");
for(i=0;i<n;i++)
{
scanf("%lf",&y[i]);
}
for(i=0;i<n;i++)
{
xsum=x sum+x[i];
ysum=y sum+y[i];
xzsum=xz sum+pow(x[i],2);
xy sum=xy sum+x[i]*y[i];
}
a=(n*xy sum-xy sum*y sum)/(n*xz sum-x sum*x sum);
b=(xz sum*y sum-x sum*xy sum)/(xz sum*n-x sum*x sum);
double y-fit[10];
for(i=0;i<n;i++)
{
y-fit[i]=a*x[i]+b;
}
printf("sr no. x y(observation) y(fitted)\n");
for(i=0;i<n;i++)
{
printf("%d %10.2lf%15.%18.2lf\n",i+1,x[i],y[i],y-fit[i]);
}
printf("\n The linear fit line is of form:\n\n%lf2+%lf\n",a,b);
return 0;
}
Practical – 13
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) 1/(1+x*x)
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
stepSize = (upper - lower)/subInterval;
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
if(i%2==0)
{
integration = integration + 2 * f(k);
}
else
{
integration = integration + 4 * f(k);
}
}
integration = integration * stepSize/3;
printf("\nRequired value of integration is: %.3f", integration);
getch();
return 0;
}
Practical – 14
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(1/(1+pow(x,2)));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h:\n");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf("\nrefined value of n and h are:%d %f\n",n,h);
printf("\n Y values \n");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n%f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}
}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\nfinal integration is %f",ans);
getch();
}
Practical – 15
#include<stdio.h>
#include<math.h>
double f(double x, double y){
return x;
}
void main(){
int i;
double x,y,x0,y0,h,k1,k2;
clrscr();
printf("Enter the initial condition for y: ");
scanf("%lf",&y0);
printf("Enter the initial condition for x: ");
scanf("%lf",&x0);
printf("Enter the value of x for which y is required: ");
scanf("%lf",&x);
printf("Enter the step-width h: ");
scanf("%lf",&h);
while((x-x0)>0.0000000001){
k1=h*f(x0,y0);
k2=h*f(x0+h/2.0,y0+k1/2.0);
y=y0+k2;
y0=y;
x0=x0+h;
}
printf("x0=%lf\ty0=%lf\n",x0,y0);
printf("The value of y is %lf\n\n",y);
getch();
}
Practical -16
#include<stdio.h>
#include<math.h>
double f(double x, double y){
return x;
}
void main(){
int i;
double x,y,x0,y0,h,k1,k2,k3,k4;
clrscr();
printf("Enter the initial condition for y: ");
scanf("%lf",&y0);
printf("Enter the initial condition for x: ");
scanf("%lf",&x0);
printf("Enter the value of x for which y is required: ");
scanf("%lf",&x);
printf("Enter the step-width h: ");
scanf("%lf",&h);
while((x-x0)>0.0000000001){
k1=h*f(x0,y0);
k2=h*f(x0+h/2.0,y0+k1/2.0);
k3=h*f(x0+h/2.0,y0+k2/2.0);
k4=h*f(x0+h,y0+k3);
y=y0+1/6.0*(k1+2*k2+2*k3+k4);
y0=y;
x0=x0+h;
}
printf("x0=%lf\ty0=%lf\n",x0,y0);
printf("The value of y is %lf\n\n",y);
getch();
}

You might also like