``````//* By peter blatcher *//
#include <stdlib.h>;
#include <stdio.h>;
#include <math.h>;

float trap(float b, float Xo, int n);
float integ(float X);

int main()
{

float Va, Ve, difference, n, area, Fao, kCao, y, X, a, b;
char c = n;
n = 2;
Fao = 0.4;
a = 0.8;
kCao = 0.45;
b = 0;

printf("\nNUMERICAL INTEGRATION PROGRAM:");
printf("\nThis program calculates the approximate value");
printf("\nof a function containg an integral.");
printf("\n\n V = Fao * the integral of 1/-kCao(1-X) between 0 and Xo");
printf("\n\nFao = %.3f mol.s^-1", Fao );
printf("\nXo = %.3f conversion", a );
printf("\nkCao = %.3f mol.m^-3.s^-1", kCao);
printf("\no = %.3f conversion", b);
printf("\t strips = %.1f", n);

do
{
a = 0.8;
b = 0.0;
Ve = ((Fao / kCao) * (log(1 / a)));
printf("\n\nexact Volume = %.3f m^3", Ve);
Va = trap(b, a, n) * Fao;
printf("\tapprox volume = %.3f", Va);
difference = (Va - Ve);
printf("\tdiff = %.3f", difference);
n = 2 * n;
}
while (difference >= (1 * (10 ^ -4)));
{
getchar();
return (0);
}
}

float trap(float b, float a, int n)
{
float d, area, y;
int i;
a = 0.8;
b = 0;
d = (a - b) / n;
for (i = 1; i <= (n - 1); i++);
{
y += (integ((b + (i * d))));
}
area = (d * y) + (d / 2.0) * ((integ(b))+ (integ(a)));
return area;
}

float integ(float X)
{
float y, kCao;
kCao = 0.45;
y = (1 / (kCao * (1-X)));
return y;
}``````

basically im trying to write a program which will work out the exact value of the following function; V = Fao * integrand 1/kCao*(1-X) between 0 and Xo. and then work out a numerical solution using the trapezium rule and compare the two, and then work out the difference and loop until the difference is less than 1 x 10^-4. there are no errors when i compile, but the program still doesnt work properly, grrr. frustating. any help and advice would be greatly appreciated.

>> there are no errors when i compile, but the program still doesnt work properly, grrr. frustating.
Welcome to the wounderful world of computer programming ;) Sorry that I can't help you -- the math is beyond me.

I believe possibly that the problem is in this line:

``while (difference >= (1 * (10 ^ -4)));``

The carrot operator (^) here is interpreted not as raising 10 to the -4 power. Rather it is interpreted as doing a bitwise XOR on the numbers 10 and -4. Consider using either the "pow" function or changing the line to this:

``while (difference >= 0.0001);``

http://www.cplusplus.com/reference/clibrary/cmath/pow.html

>> while (difference >= (1 * (10 ^ -4)));

Is that try to raise 10 to a negative power ? Doesn't that produce an imaginary number (which are not supported by C or C++ native languages)

You might want to check out some of the boost library math function

I checked with google and found that negative exponents don't do what I thought. So just ignore my dumb question above. See -- I said I was math impaired :) [/edit]

From what I understand here the _Complex keyword was added in ISO C99 and is probably not implemented by very many, if any, compilers on the market today.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.