0% found this document useful (0 votes)
30 views25 pages

Project 1

The document contains 10 programming problems related to numerical methods. Problem 1 asks to write a program to calculate absolute, relative and percentage error. Problem 2 asks to write a program implementing the bisection method. Problem 3 asks to write a program for the regula falsi method. Problems 4-6 ask to write programs for Simpson's 1/3 rule, Simpson's 3/8 rule, and the trapezoidal rule respectively for numerical integration. Problems 7-9 relate to solving quadratic equations, Newton-Raphson method, and Newton interpolation. Problem 10 asks to write a program for Lagrange's interpolation.

Uploaded by

alisabasaifi
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)
30 views25 pages

Project 1

The document contains 10 programming problems related to numerical methods. Problem 1 asks to write a program to calculate absolute, relative and percentage error. Problem 2 asks to write a program implementing the bisection method. Problem 3 asks to write a program for the regula falsi method. Problems 4-6 ask to write programs for Simpson's 1/3 rule, Simpson's 3/8 rule, and the trapezoidal rule respectively for numerical integration. Problems 7-9 relate to solving quadratic equations, Newton-Raphson method, and Newton interpolation. Problem 10 asks to write a program for Lagrange's interpolation.

Uploaded by

alisabasaifi
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/ 25

Program no:--1

 Write a program to find out approximate, relative and


percentage error?

#include<stdio.h>

#include<math.h>

int main( )

float xt, xa, ea, er, ep;

printf("Enter the actual value=");

scanf("%f",&xt);

printf("Enter the approx valuue=");

scanf("%f",&xa);

ea=xt-xa;

if(ea<0)

ea=-(ea);

printf("\n\t Solution error=%f",ea);

er=ea/xt;

printf("\n\t Relative errror=%f",er);

ep=er*100;

printf("\n\t Percentage error =%f%",ep);

}
outPut

Enter the acttuaal vaalluue=5.789

Enter thhe approx. value=5.0896

Absolute error=0.699400

Relative error=0.120815

Percentage error=12.081532%
Program no 2
 Write a program of Bisection method?

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define f(x) cos(x) - x * exp(x)

int main()

float x0, x1, x2, f0, f1, f2, e;

int step = 1;

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 + x1)/2;

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();

}
Output
Enter two initial guesses:
0
1
Enter tolerable error:
0.0001

Step x0 x1 x2 f(x2)
1 0.000000 1.000000 0.500000 0.053222
2 0.500000 1.000000 0.750000 -
0.856061
3 0.500000 0.750000 0.625000 -
0.356691
4 0.500000 0.625000 0.562500 -
0.141294
5 0.500000 0.562500 0.531250 -
0.041512
6 0.500000 0.531250 0.515625 0.006475
7 0.515625 0.531250 0.523438 -
0.017362
8 0.515625 0.523438 0.519531 -
0.005404
9 0.515625 0.519531 0.517578 0.000545
10 0.517578 0.519531 0.518555 -
0.002427
11 0.517578 0.518555 0.518066 -
0.000940
12 0.517578 0.518066 0.517822 -
0.000197
13 0.517578 0.517822 0.517700 0.000174
14 0.517700 0.517822 0.517761 -
0.000012

Root is: 0.517761


Program no:-3
 Write a program for regula fallsi method?
#include<stdio.h>

#include<math.h>

#define eps 0.00005

#define f(x)(x*x)-25

int main()

float x1,x2;

int i,n;

float f0,f1,f2,x0;

printf("\n\t enter the itteratioon limiitt=");

scanf(("%d",&n));

for(xo=1;x0=x0+0.50)

f0=f(x0);

if (f0<0)

break;

for(x1=1;x1=x1+0.50)
f1=f(x1);

if(f1>0)

break;

for(i=1;i<=n;i++)

x2=((x0*f1)-(x1*f0))/(f1-f0);

f2=f(x2);

if((f0*f2)>0)

x1=x2;

f1=f2;

else

x0=x2;

f0=f2;

if(fabs((f2)<eps)

break;
else

printf("\n convergent solution =x=%2f\t f=%f",x2,f2);}


outPut
Enter the iteration limit =5

Convergent solution=x==4.692307 f=-2.982251

Convergent solution =x=5.216217 f=2.20891

Convergent solution=x=4.993286 f=0.067098

Convergent solution =x=5.0000213 f=0.002132


Program no 4
 Write a program of simpson 1/3 rule?

#include<stdio.h>

#include<math.h>

double f(double x)

return x*x;

main(){

int n,i;

double a,b,h,x,sum=0,integral;

printf("\nEnter the no. of sub-intervals(EVEN): ");

scanf("%d",&n);

printf("\nEnter the initial limit: ");

scanf("%lf",&a);

printf("\nEnter the final limit: ");

scanf("%lf",&b);

h=fabs(b-a)/n;

for(i=1;i<n;i++){

x=a+i*h;

if(i%2==0){
sum=sum+2*f(x);

else{

sum=sum+4*f(x);

integral=(h/3)*(f(a)+f(b)+sum);

printf("\nThe integral is: %lf\n",integral);

}
Output

Enter the no of sub interval(EVEN):24


Enter the initial limit:2
Enter the fiinal limit:4
The integer is:18.66667
Program no 5
 Write a program of simpson 3/8 rule?

#include<stdio.h>
#include<math.h>
double f(double x)
{
return x*x;
}
main(){
int n,i;
double a,b,h,x,sum=0,integral;
printf("\nEnter the no. of sub-intervals(MULTIPLE OF 3): ");
scanf("%d",&n);
printf("\nEnter the initial limit: ");
scanf("%lf",&a);
printf("\nEnter the final limit: ");
scanf("%lf",&b);
h=fabs(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
if(i%3==0){
sum=sum+2*f(x);
}
else{
sum=sum+3*f(x);
}
}
integral=(3*h/8)*(f(a)+f(b)+sum);
printf("\nThe integral is: %lf\n",integral);
}
Output
Enter the no of sub interval (multiple 3):81
Enter thhe initial limit :2
Enter the final limit:1
The interger is:6.296296
Program no :6
 Write a program of trapezoidal rule?
#include<stdio.h>
#include<math.h>
double f(double x){
return x*x;
}
double trapezoidal(double f(double x), double a, double b, int
n){
double x,h,sum=0,integral;
int i;
h=fabs(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
sum=sum+f(x);
}
integral=(h/2)*(f(a)+f(b)+2*sum);
return integral;
}
main(){
int n,i=2;
double a,b,h,eps,sum=0,integral,integral_new;
printf("\nEnter the initial limit: ");
scanf("%lf",&a);
printf("\nEnter the final limit: ");
scanf("%lf",&b);
printf("\nEnter the desired accuracy: ");
scanf("%lf",&eps);
integral_new=trapezoidal(f,a,b,i);
do{
integral=integral_new;
i++;
integral_new=trapezoidal(f,a,b,i);
}while(fabs(integral_new-integral)>=eps);
printf("The integral is: %lf\n with %d
intervals",integral_new,i);
}
Output

Enter the initial limit:5


Enter the final limit:2
Enter the desire accuracy:56
The interger is:99.500000
Program no 7
 Write a program to solve a quaraatic equation?
#include<stdio.h>
#include<math.h>
int a,b,c,d;
float z, x1=0,x2=0;
printf("given the value of ax^2+bx+conly integers
(a,b,c)");
scanf ("%d%d%d",&a,&b&c);
d=b*b-4*a*c;
if(d<0)
printf("sorry roots are imaginary\n");
else
if(d==0)
x1=-b/(2*a);
printf("your roots are %f%f",x1,x2);
else
if(d>0)
z=sqrt(d);
x1=(-b-z)/(2*a);
x2(-b-z)/(2*a);
printf("your roots are f%f% "x1,x2);
getch();
output
Given the vlue of ax^2+bx+c only integer(a,b,c):
1
2
3
Your roots are:1.000000-2.000000
Program no :-8
 Write a program of newton raphson method?

#include<stdio.h>
#include<math.h>
#define f1(x) pow (x,4)-x-9
#define f2 $*pow(x,3)-1
void main()
float x0,x1;
int n,i;
printf("\n enter the limit of iteration:");
scanf("%d",&n);
printf("\n enter the first approx:");
scanf("%f",&x0);
for(i=0;i<n;i++)
x1=x0-(f1(x0))/(f2(x0));
printf("\n\troots are =%d",x0);
x0=x1;
output
enter vthe limit of iteration:7
enter the first approx.:1.7
roots are:1.825879
roots are:1.813520
roots are:1.815521
roots are:1.823335
roots are:1.813387
roots are:1.813387
Program no -9
 Write a program to find newton interpolation?
#include<stdio.h>
#define mn 100
#define or 4
void main()
{
float ax[mn+1],diff[mn+1][or+1],nr=1,dr=1,x,p,h,ye;
int n,j,k;
printf(“enter the value of\n”);
scanf(“%d”,&n);
printf(“enter the value in the form x,y\n”);
for(i=0;i<n;i++)
scanf(“%f%f”,&a[i},a&[j];
printf(“enter the value in the of x for which the value mof
y is wanted\n”);
scanf(“%f”,&x);
h=ax[1]-ax[0];
for(i=0;i<=n-1;i++)
diff[i]=ay[i=1]-ay[i];
for(j=2;j<=or;j++)
for(i=0;i<=n;i++)
diff[i][j]=diff[i=1]-diff[i][j]-1;
i=0;
while(!(a[i]/h,ye=ay[i]);
for(k=0;k<=or;k++)
nr*=p-k+1;dr*=k;
printf(“when x=%f,y=%f\n”,x,ye);
return(0);
}
Output
Enter the value of n/n2
Enter the value in the form of x,y
2 2 4 2
Enter the value of x for which value of y
is wanted
4
When x=4.000000,y=6.000000
Program no-10
Write a program to find lagrange’s
interpoliton?
#include<stdio.h>
#include<math.h

You might also like