logo

Euler's number and integral: Trapezoidal float Part1 trap(float a, float b, float f)

4 Pages660 Words101 Views
   

Computers in Engineering (COMP 208)

   

Added on  2021-11-07

About This Document

return (h/2)*s; /* Integration using Simpson's rule */ float simpson (int no, float x0, float xn) int r; float interval, sum=0., x; interval = ((xn -x0) /(no-1)); for (r=2; rno; r+=2) /* loop for odd points */ x = interval * (r-1); sum +

Euler's number and integral: Trapezoidal float Part1 trap(float a, float b, float f)

   

Computers in Engineering (COMP 208)

   Added on 2021-11-07

ShareRelated Documents
/*
*COMP 208: COMPUTERS IN ENGINEERING
*WINTER 2018 ASSIGNMENT 4
* WHISKEY PRODUCTION USING A BATCH DISTILLATION COLUMN
*
*/
//PART ONE
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ME 2.7182818284590452354E0 /* Euler's number */
float y(float x)
{
// Declaring the function f(x) = 1/(x) from integral(f(x)dx)
return 1/(x);
}
// Function to evalute the value of integral: Trapezoidal
float Part1trap(float a, float b, float n)
{
// Grid spacing
float h = (b-a)/n;
// Computing sum of first and last terms
// in above formula
float s = y(a)+y(b);
// Adding middle terms in above formula
int i;
for (i = 1; i < n; i++)
s += 2*y(a+i*h);
Euler's number and integral: Trapezoidal float Part1 trap(float a, float b, float f)_1
// h/2 indicates (b-a)/2n. Multiplying h/2
// with s.
return (h/2)*s;
}
/* Integration using Simpson's rule */
float simpson (int no, float x0, float xn)
{
int r;
float interval, sum=0., x;
interval = ((xn -x0) /(no-1));
for (r=2; r<no; r+=2) /* loop for odd points */
{
x = interval * (r-1);
sum += 4 * y(x);
}
for (r=3; r<no; r+=2) /* loop for even points */
{
x = interval * (r-1);
sum += 2 * y(x);
}
sum += y(x0) + y(xn); /* add first and last value */
sum *= interval/3.; /* then multilpy by interval*/
return (sum);
}
//function to find roots of a function
void Part2Bisect(float *x, float x0, float xn, int *itr)
/* this function performs and prints the result of one iteration */
{
*x=(x0+xn)/2;
++(*itr);
Euler's number and integral: Trapezoidal float Part1 trap(float a, float b, float f)_2

End of preview

Want to access all the pages? Upload your documents or become a member.