Logo 
Search:

C Programming Forum

Ask Question   UnAnswered
Home » Forum » C Programming       RSS Feeds

Calculate pH of Rainwater

  Asked By: Dilan    Date: Sep 21    Category: C Programming    Views: 2305
  

I want a programme to find pH of rainwater by bisection method.the equation is

0=K1KHPCO2/10^6[H+]+2K2K1KHPCO2/10^6[H+]^2+Kw/[H+]-[H+]

KH=10^-1.46,K1=10^-6.3,K2=10^-10.3,and Kw=10^-14

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Waqar Afzal     Answered On: Jun 07

#include<stdio.h>
#include <math.h>
#include<conio.h>
#include<iostream>
using namespace std;
#define ESP 0.005
#define F(ph,pco) pow(10,(6-3*ph))-pow(10,-ph)*(pow(10,-7.76)*pco+pow(10,-8))-2*pow(10,-18.06)*pco
int main()
{
int i = 1;
float xu,xl,xr;
double f1,f2,f0,t;
cout<<"Enter the year : ";

int year;
cin>>year;
float pco=0.011825*pow(year - 1980.5,2) + 1.356975*(year - 1980.5) + 339;
//clrscr();
// do{

printf("\nEnter the value of xu: ");
scanf("%f",&xu);
//}while(F(xu,pco)>0);

//do{

printf("\nEnter the value of xl: ");
scanf("%f",&xl);
//}while(F(xl,pco)>0);
printf("\n__________________________________________________________________\n");
printf("\niteration\t xu\t xl\t xr\t f0\t f1\t f2");
printf("\n___________________________________________________________________\n");
do
{
xr=(xu+xl)/2;
f0=F(xu,pco);
f1=F(xl,pco);
f2=F(xr,pco);
printf("\n%d\t %f %f %f %lf %lf %lf", i, xu,xl,xr,f0,f1,f2);
if(f0*f2<0)
{
xl=xr;
}
else
{
xu=xr;
}
i++;
}while(i<12);
printf("\n__________________________________________________________\n");
printf("\n\nApp.root = %f",xr);
getch();
}
/*

Enter the year : 2013

Enter the value of xu: 12

Enter the value of xl: 2

__________________________________________________________________

iteration xu xl xr f0 f1 f2
___________________________________________________________________

1 12.000000 2.000000 7.000000 -0.000000 1.000000 -0.000000
2 7.000000 2.000000 4.500000 -0.000000 1.000000 0.000000
3 7.000000 4.500000 5.750000 -0.000000 0.000000 -0.000000
4 5.750000 4.500000 5.125000 -0.000000 0.000000 0.000000
5 5.750000 5.125000 5.437500 -0.000000 0.000000 0.000000
6 5.750000 5.437500 5.593750 -0.000000 0.000000 -0.000000
7 5.593750 5.437500 5.515625 -0.000000 0.000000 0.000000
8 5.593750 5.515625 5.554688 -0.000000 0.000000 0.000000
9 5.593750 5.554688 5.574219 -0.000000 0.000000 0.000000
10 5.593750 5.574219 5.583984 -0.000000 0.000000 -0.000000
11 5.583984 5.574219 5.579102 -0.000000 0.000000 0.000000
__________________________________________________________


App.root = 5.579102
--------------------------------
Process exited after 119.8 seconds with return value 0
Press any key to continue . . .
*/

 
Didn't find what you were looking for? Find more on Calculate pH of Rainwater Or get search suggestion and latest updates.




Tagged: