You are on page 1of 6

JINNAH UNIVERSITY FOR WOMEN

DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING


LAB TASK

Course Title: Numerical Computing Class: BS (SE) 2018


Course Code: MAT-4322 Marks: 5
Course Incharge: Ms. Javairia Zulfiqar

Question#1 [2.5 marks]


A robotic arm with a rapid laser scanner is doing a quick quality check on holes drilled in a
15 times 10rectangular plate. The centers of the holes in the plate describe the path the arm needs
to take, and the hole centers are located on a Cartesian coordinate system (with the origin at the
bottom left corner of the plate) given by the specifications in Figure 1.

XY-Values
8

7 7.2 7.1

6 6

5 5 5

4
3.5
3

0
0 2 4 6 8 10 12

Figure 1: Location of the holes on the rectangular plate

If the laser is traversing from x=2 to x= 4.25 in a linear path, what is the value of f ' (x )at x= 4.00
Implement using Lagrange method?

SOLUTION
X Y= F(X)
2 7.2
4.25 7.1
5.25 6
7.81 5
9.2 3.5
10.6 5

Code

#include<stdio.h>
#include<conio.h>
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB TASK

void main()
{
float x[100], y[100], xp, yp=0, p;
int i,j,n;

/* Input Section */
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);
/* Implementing Lagrange Interpolation */
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();
}

Output:
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB TASK
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB TASK

Question#2: [2.5 marks]

You have a spherical storage tank containing oil. The tank has a diameter of 6 ft. You are asked to
calculate the height h to which a dipstick 8 ft long would be wet with oil when immersed in the tank
when it contains 4 ft3of oil.

The equation that gives the height, h , of the liquid in the spherical tank for the given volume and
radius is given by

Implement using the bisection method of finding roots of equations to find the height, h, to which
the dipstick is wet with oil. Conduct three iterations to estimate the root of the above equation. h
Find the absolute relative approximate error at the end of each iteration and the number of
significant digits at least correct at the end of each iteration.

SOLUTION

Code

#include<stdio.h>
#include<math.h>
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB TASK

double F( double h) //Equation


{
return (pow(h,3) - 9*pow(h, 2) + 3.8197);
}

int main()
{
printf("Given Equation:\n");
printf(" h^3 - 9*h^2 + 3.8197= 0\n");
double h0,h1;

printf("\nEnter the first approximation to the root : ");


scanf("%lf",&h0);

printf("\nEnter the second approximation to the root : ");


scanf("%lf",&h1);

int iter;
printf("\nEnter the number of iteration to be performed : ");
scanf("%d",&iter);

int ctr=1;
double l1=h0;
double l2=h1;
double r,f1,f2,f3;

//Checking if initial approximations are the root or not


if(F(l1)==0)
r=l1;
else if(F(l2)==0)
r=l2;
else
{
while(ctr<=iter)
{
//Implementation of Bisection algorithm
f1=F(l1);
r=(l1+l2)/2.0;
f2=F(r);
f3=F(l2);

if(f2==0)
{
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB TASK

r=f2;
break;
}
printf("\nThe root after %d iteration is %lf",ctr,r);

if(f1*f2<0)
l2=r;
else if(f2*f3<0)
l1=r;
ctr++;
}
}

printf("\n\nThe approximation to the root is %lf\n",r);

return 0;
}

Output:

You might also like