COMP 208: Computers in Engineering, Winter 2018, Assignment 4: Whiskey

Verified

Added on  2021/11/07

|4
|660
|101
Homework Assignment
AI Summary
This assignment solution addresses COMP 208: Computers in Engineering, focusing on whiskey production using a batch distillation column. The solution is divided into two parts. Part one involves numerical integration using the Trapezoidal rule and Simpson's 1/3 rule to evaluate a definite integral. Part two focuses on finding the roots of a function using the Bisection method. The code is written in C, implementing the necessary functions for each method, including the evaluation of the function f(x) = 1/x and the iterative process for root finding. The main function demonstrates the application of these methods, calculating and printing the results, including the value of the integral and the root of the function, with iterative steps for the bisection method.
Document Page
/*
*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);
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
// 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);
Document Page
printf("Iteration no. %3d X = %.5f\n", *itr, *x);
}
int main()
{
// Range of definite integral
float x0 = 0.99;
float xn = 0.49;
int itr = 0;
int maxmitr=20;
float allerr=0.0005;
float x,x1,simpsum;
// Number of grids. Higher value means
// more accuracy
int n = 100;
int no=n;
//Integration by trapezoidal
printf("Using Trapezoidal Method\n\n");
printf("Value of integral is %.4f\n",Part1trap(x0, xn, n));
//Integration by Simpsons
printf("Using Simpson 1/3 rule Method for Integration\n\n");
simpsum=simpson (no,x0,xn); /* Simpson's rule */
printf("%f\n\n",simpsum);
//Finding roots by Bisection method
printf("Using Bisection Method to find roots\n\n");
Part2Bisect(&x, x0, xn, &itr);
do
{
if (y(x0)*y(x) < 0)
xn=x;
else
x0=x;
Document Page
Part2Bisect(&x1, x0, xn, &itr);
if (fabs(x1-x) < allerr)
{
printf("After %d iterations, root = %6.4f\n\n", itr, x1);
return 0;
}
x=x1;
}
while (itr < maxmitr);
printf("The solution does not converge or iterations are not sufficient");
return 1;
}
chevron_up_icon
1 out of 4
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]