NM Lab in C Program
NM Lab in C Program
Tribhuwan University
Binayaknagar, New Baneshwor
Laboratory Report
Numerical Method (CSC - 207)
Submitted To:
Department of Computer Science and Information Technology
Mr. Mohit Guragai
Lecturer , MBMC
Algorithm:
1. Choose xl and xu as two guesses for the root such that f(xl)f(xu) < 0 and stopping
criterion E.
2. Compute f(xl) and f(xu).
3. Estimate the root, xm of the equation f(x) = 0 as the mid point between xl and xu as
xm = (xl+xu)/2
Else if f(xl)f(xm) < 0, the root lies between xl and xm. Set xu = xm.
Else if f(xl)f(xm) > 0, the root lies between xm and xu. Set xl = xm.
Program :
#include <stdio.h>
int a0,a1,a2,a3;
float f(float x){
return (a3*x*x*x+a2*x*x+a1*x+a0);}
int main(){
float xl,xm,xu,fxl,fxm,fxu,E,Era;
printf("Enter the cofficients a3,a2,a1 and a0 respectively\n");
scanf("%d%d%d%d",&a3,&a2,&a1,&a0);
printf("Enter the initial bracket and E\n");
scanf("%f%f%f",&xl,&xu,&E);
fxl=f(xl);
fxu=f(xu);
while(1){
fxu=f(xu);
xm=(xl+xu)/2;
fxm=f(xm);
if((fxl*fxm)==0){
printf("Root = %f\n",xm);
}
else if((fxl*fxm)<0)
xu=xm;
else
xl=xm;
Era=(xu-xl)/xu;
if(Era<E){
printf("Root = %f\n",xm);
break;}
}
}
Output
2. Write an algorithm and C program to solve non-linear equation of the
form a3x3+a2x2+a1x+a0=0 using Newton-Raphson method.
Algorithm:
1. Input the initial guess x0 and error precision E.
Else go to step 6.
6. Terminate.
Program :
#include <stdio.h>
#include <math.h>
float a0,a1,a2,a3;
int main(){
float x0,x1,fx0,fdx0,E,Er;
printf("Enter the coefficients a3,a2,a1,a0\n");
scanf("%f%f%f%f",&a3,&a2,&a1,&a0);
printf("Enter the initial guess and E\n");
scanf("%f%f",&x0,&E);
while(1){
fx0=f(x0);
fdx0=fd(x0);
x1=x0-fx0/fdx0;
Er=(x1-x0)/x1;
if(fabs(Er)<E){
printf("Root = %f\n",x1);
break;
}
x0=x1;
}
}
Output
3. Write C program to solve non-linear equation of the form
a3x3+a2x2+a1x+a0=0 using Secant method.
Algorithm:
1. Input two initial guess (say x0 and x1) and error precision (say E).
f(x0) = f(x1)
x1 = x2
Else go to step 6.
6. Terminate.
Program :
#include <stdio.h>
#include <math.h>
#define f(x) (a3*x*x*x+a2*x*x+a1*x+a0)
float a0,a1,a2,a3;
int main(){
float x0,x1,x2,fx0,fx1,E,Er;
printf("Enter the coordinates a3,a2,a1 and a0\n");
scanf("%f%f%f%f",&a3,&a2,&a1,&a0);
printf("Enter the two initial guess and error\n");
scanf("%f%f%f",&x0,&x1,&E);
while(1){
fx0=f(x0);
fx1=f(x1);
x2=x1-(fx1*(x1-x0))/(fx1-fx0);
Er=(x2-x1)/x2;
if(fabs(Er)<E){
printf("Root = %f\n",x2);
break;
}
x0=x1;
x1=x2;
}
Output
4. Write C program to find linear regression of the form y = a + bx.
#include <stdio.h>
int main(){
int i,n;
float a=0,b=0,x[10],y[10],sx=0,sy=0,sxy=0,sx2=0;
printf("Enter the number of points\n");
scanf("%d",&n);
printf("Enter the value of a x and fx\n");
for(i=0;i<n;i++){
scanf("%f%f",&x[i],&y[i]);
}
for(i=0;i<n;i++){
sx=sx+x[i];
sy=sy+y[i];
sxy=sxy+x[i]*y[i];
sx2=sx2+x[i]*x[i];
}
b=((n*sxy)-(sx*sy))/((n*sx2)-(sx*sx));
a=(sy/n)-(b*sx/n);
printf("Fitted line is : %f+%fx\n",a,b);
return 0;
}
Output
5. Write C program to find solution of system of linear equations using
Gauss-Seidel method.
#include <stdio.h>
#include <math.h>
#define X 2
int main(){
float x[X][X+1],a[X],ae,max,t,s,e;
int i,j,r,mxit;
for(i=0;i<X;i++)
a[i]=0;
puts("Enter the elements of augumented matrix rowwiswe\n");
for(i=0;i<X;i++){
for(j=0;j<X+1;j++){
scanf("%f",&x[i][j]);
}
}
printf("Enter the allowed error and maximum number of iteration : ");
scanf("%f%d",&ae,&mxit);
printf("Iteration\tx\[1]\tx[2]\n");
for(r=1;r<mxit;r++){
max=0;
for(i=0;i<X;i++){
s=0;
for(j=0;j<X;j++)
if(j!=i) s+=x[i][j]*a[j];
t=(x[i][X]-s)/x[i][i];
e=fabs(a[i]-t);
a[i]=t;
}
printf("%5d\t",r);
for(i=0;i<X;i++)
printf("%9.4f\t",a[i]);
printf("\n");
if(max<ae){
printf("Conversion in %3d iteration\n",r);
for(i=0;i<X;i++)
printf("a[%3d]=%7.4f\n",i+1,a[i]);
return 0;
}
}
}
Output
6. Write C program to find derivative of f(x) = sinx + 1 using two point
forward difference formula.
#include <stdio.h>
#include <math.h>
float function(float x){
return (sin(x)+1);
}
int main()
{
float x,h,f1;
printf("Enter the value of 'x' and 'h' : ");
scanf("%f%f",&x,&h);
x=x*(3.14/180);
f1=(function(x+h)-function(x))/h;
printf("2 point derivative of sin(x)+1: %f\n",f1);
return 0;
}
Output
7 . Write C program to find derivative of f(x) = 2ex using two point
backward difference formula.
#include <stdio.h>
#include <math.h>
float function(float x){
return (2*exp(x));
}
int main()
{
float x,h,f1;
printf("Enter the value of 'x' and 'h' : ");
scanf("%f%f",&x,&h);
f1=(function(x)-function(x+h))/h;
printf("2 point derivative of 2*e^x : %f\n",f1);
return 0;
}
Output
8. Write C program to find derivative of f(x) = 2x2 + 1 using three point
(central difference) formula.
#include <stdio.h>
#include <math.h>
float function(float x){
return (2*pow(x,2)+1);
}
int main()
{
float x,h,f1;
printf("Enter the value of 'x' and 'h' : ");
scanf("%f%f",&x,&h);
f1=(function(x+h)-function(x-h))/(2*h);
printf("3 point derivative 2*x^2+1 : %f\n",f1);
return 0;
}
Output