Must be written in C For this lab you will write a pthreads

  Must be written in \'C\'  For this lab you will write a pthreads program that will calculate the integral of sqrt(4 - x*x) from 0 to 2 using the trapezoidal rule. There will be no more than 50 partitions.  The user will input the number of partitions they want to use.  Remember to include math.h in the code and do the -lm in the compile line. 

Solution

#include<stdio.h>
#include<math.h>
float f(float);

int main()
{
int i,n;
float x0,xn,h,y[50],ratio1,ratio2,ans,x[50];
x0=0;
xn=2;
printf(\"\ Enter values of partition:\ \");
scanf(\"%d\",&n);
h=(xn-x0)/n;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf(\"\ Y values \ \");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf(\"\ %f\ \",y[i]);
}
ratio1=0;
ratio2=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
ratio1+=y[i];
}
else
{
ratio2+=y[i];
}
}
ans=h/3*(y[0]+y[n]+4*ratio1+2*ratio2);
printf(\"\ Integration is %f\",ans);

return 0;
}
float f(float x)
{
return(4-pow(x,2));
}

Output:-

Enter values of partition: 30

Y values

4.000000

3.995556

3.982222

3.960000

3.928889

3.888889

3.840000

3.782222

3.715555

3.640000

3.555556

3.462222

3.360000

3.248889

3.128889

3.000000

2.862222

2.715555

2.560000

2.395555

2.222222

2.040000

1.848889

1.648889

1.440000

1.222222

0.995555

0.760000

0.515555

0.262222

0.000000

Integration is 5.333333

 Must be written in \'C\' For this lab you will write a pthreads program that will calculate the integral of sqrt(4 - x*x) from 0 to 2 using the trapezoidal rul
 Must be written in \'C\' For this lab you will write a pthreads program that will calculate the integral of sqrt(4 - x*x) from 0 to 2 using the trapezoidal rul

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site