Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Can't find my mistake

Rusty

Active Coder
The calc_vle function takes an element of temperature array and calculates a value of x. The unique value x goes through an iteration until it's condition equation is satisfied. Then x is written to it's own arrray.
Everything about the function is executing correctly except writing the iterated and solved x1 to it's array.
x1 writes to screen print with correct and decrementing values.
But array values print to screen as zero.

What am I missing?

Thanks

Screenshot (171).png



C:
void calc_vle(int max_num, double atm_p, double pot_mol_0, double pot_mol_last, double *pot_mol, double *pot_temp,
               double (*calc_gamma_e)(double x1, double x2),
               double (*calc_gamma_w)(double x1, double x2),
               double (*calc_psat)(double A, double B, double C, double bp, double gamma, double x))
{
   double bp, x1, x2 = 0;
   double psat1 = 0;
   double psat2 = 0;
   double gamma_e = 0;
   double gamma_w = 0;


   pot_mol[max_num] = pot_mol_last;
   x1 = pot_mol_0;   //ethanol component of mixture in mols
   printf("VLE\tx1 = %lf", x1);
   x2 = 1-x1;

   for (int index = 0; index < max_num; index++)
{
    bp = pot_temp[index];

    gamma_e = calc_gamma_e(x1, x2);
    gamma_w = calc_gamma_w(x1, x2);

   do
    {
        psat1 = calc_psat(eA, eB, eC, bp, gamma_e, x1);  //Saturated vapor pressure ethanol
        psat2 = calc_psat(wA, wB, wC, bp, gamma_w, x2);  //Saturated vapor pressure water
        x1 -= 0.00001;
        printf("\nx1 = %lf", x1);
        printf("\npressure = %lf\ttemp = %lf\tnew x1 = %lf", psat1+psat2, pot_temp[index], pot_mol[index]);

    }
    while (psat1 + psat2 > atm_p); //When sum of vapor pressures = or exceed atmospheric pressure the mixture is boiling
    pot_mol[index] = x1;

}

    return;
}
 
Hi,

ChatGPT has been useful to me in questions like this. Give it a try. I expect that it surprises you. Good luck.
I'm sure ChatGPT is extremely powerful but does it really analyze an arbitrary chunk of C code and tell you what you have done wrong ?
 
C:
do
    {
        psat1 = calc_psat(eA, eB, eC, bp, gamma_e, x1);  //Saturated vapor pressure ethanol
        psat2 = calc_psat(wA, wB, wC, bp, gamma_w, x2);  //Saturated vapor pressure water
        x1 -= 0.00001;
        printf("\nx1 = %lf", x1);
        printf("\npressure = %lf\ttemp = %lf\tnew x1 = %lf", psat1+psat2, pot_temp[index], pot_mol[index]);

    }
    while (psat1 + psat2 > atm_p); //When sum of vapor pressures = or exceed atmospheric pressure the mixture is boiling
    pot_mol[index] = x1;

I could be wrong but it seems likely that the last line pot_mol[index] = x1; should be inside the loop, before the printf's, rather than following it.
 
Well, ChatGPT is clever ! It already pointed out an error (erroneous call of scanf) before I actually posted the C code 😁
But to be fair, once I posted the code it arrived at the same conclusion as I did :

  1. In each iteration, you are updating the value of x1, but you're printing pot_mol[index], which is not being updated inside the loop. This may explain why you're consistently seeing "new x1 = 0.000000" in the output.

Wow... before long, forums like this may be obsolete... and to be really pessimistic, all human interaction may cease to exist as we only talk to AI anymore 😲
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom