0% found this document useful (0 votes)
63 views

NM Lab in C Program

The document contains a laboratory report submitted by Roshika Balami to their lecturer Mohit Guragai. The report includes 8 problems solved using numerical methods in C programming language. For each problem, an algorithm is provided and a C program is written to find the solution. The problems cover bisection method, Newton-Raphson method, secant method, linear regression, Gauss-Seidel method, and derivative calculation using forward, backward, and central difference formulas. Sample outputs are provided for each program.

Uploaded by

xn3kaf
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)
63 views

NM Lab in C Program

The document contains a laboratory report submitted by Roshika Balami to their lecturer Mohit Guragai. The report includes 8 problems solved using numerical methods in C programming language. For each problem, an algorithm is provided and a C program is written to find the solution. The problems cover bisection method, Newton-Raphson method, secant method, linear regression, Gauss-Seidel method, and derivative calculation using forward, backward, and central difference formulas. Sample outputs are provided for each program.

Uploaded by

xn3kaf
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/ 14

Madan Bhandari Memorial College

Tribhuwan University
Binayaknagar, New Baneshwor

Laboratory Report
Numerical Method (CSC - 207)

LAB Report submitted By:


Name : Roshika Balami
Roll No : CS2076B015
Year/Semester : 2076 (3rd semester)

Submitted To:
Department of Computer Science and Information Technology
Mr. Mohit Guragai
Lecturer , MBMC

Submission Date :9th December 2021


1. Write an algorithm and C program to solve non-linear equation of the
form a3x3+a2x2+a1x+a0=0 using Bisection method.

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

4. Now check the following:


If f(xl)f(xm) = 0; then the root is xm, terminate the program.

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.

5. Find the absolute relative approximate error as: Era = (xl-xu)/xu


6. If Era> E, then go to step 2, else terminate
7. Terminate.

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.

2 .Evaluate f’(x) symbolically.


3. Use x0 to estimate the new value of the root x1 as x1 = x0 – f(x0)/f’(x0)

4. Find the absolute relative approximate error as : Era= (x1-x0)/x1.


5. Compare the absolute relative approximate error, with the error precision E
If Era> E, set x0 = x1 and go to step 2.

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)


#define fd(x) (3*a3*x*x+2*a2*x+a1) //derivative of f(x)

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).

2. Evaluate f(x0) and f(x1).

3. Estimate new value of the root x2 as x2 = x1 – (f(x1)(x1-x0))/f(x1)-f(x0)

4. Find the absolute relative approximate error as : Era = (x2-x1)/x2 .


5. Compare the absolute relative approximate error, with the error precision E
If Era> E,
set x0 = x1

f(x0) = f(x1)

x1 = x2

f(x1) = f(x2) and go to step 2.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy