Professional Documents
Culture Documents
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
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
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
int main()
{
printf("Given Equation:\n");
printf(" h^3 - 9*h^2 + 3.8197= 0\n");
double h0,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;
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++;
}
}
return 0;
}
Output: