HP Forums
Simulating and Fitting Data for Chemical Reactions - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: Simulating and Fitting Data for Chemical Reactions (/thread-14062.html)



Simulating and Fitting Data for Chemical Reactions - Namir - 11-27-2019 09:12 PM

Hi All,

If you are a chemist or chemical engineer, then you might be interested in my latest study "Simulating and Fitting Data for Chemical Reactions" that I posted on my website (Click here and select the last entry in the list of topics.). The study shows how to simulate measuring the concentrations of chemicals (reactants and products) for various kinds of chemical reaction. The study then shows how to perform the nonlinear curve fitting for different kinds of reactions using the optimization of custom ODE numerical solvers (I am using the Runge-Kutta-Fehlberg method). The study also shows a case where you use custom ODE numerical solver to simulate measured chemical concentrations when the analytical equations that related these concentrations and time are not available.

Enjoy!

Namir


RE: Simulating and Fitting Data for Chemical Reactions - ttw - 11-28-2019 12:14 AM

Nice article. For the optimization step, I've done better that evolutionary by using a quasi-random search for the basic steps then a short grid search to finish. To be fair, I generate several (100 or so) candidate solutions using quasi-random search then keep the best few (sort of like an evolutionary method without crossover). At the next steps, I either use a random search from each point or a grid search or something problem-dependent. The I throw in a few new quasi-random point and continue.

The method works because the quasi-random points are much better distributed than random (I'll give some quick formulas below). The grid search works because at least 1 of the grid lines is less than a right angle from the gradient. Any local search is good. Sometimes one can use direct methods like Nelder-Mead or some type of interpolation. By adding new points, one hopes to be lucky and guarantees convergence.

The easiest multi-dimensional quasi-random sequence is the Richtmeyer sequence. One uses the fractional part of multiples of the square roots of primes. Sqrt(2), Sqrt(3), etc. It's quick to do these by just setting x(i)=0 updating by x(i)=Frac(x(i)+Sqrt(P(i))). Naturally one just stores the fractional parts of the irrationals and updates. (List mode)
The sequence is also called the Kronecker or Weyl sequence at times.

A slightly better (by my computations) is my modification of the Halton sequence. It's tricky to describe. I'll post it later if there's interest. It takes a lot of time on the HP50g so I use the Richtmeyer stuff instead. (On my Fortran codes on the PC, either method is fast enough.)

Unlike using pseudo- random generator (or even real random generators should such exist), quasi random generator need a bit of care. Each independent coordinate gets its own prime (or its own Halton sequence). For example with 25 parameters to be determined, one first uses the fractional parts of (2,3,...97) and call that the first measurement X_1 (I need a name). Then X_2 is X_1 + the square roots reduced mod 1. X3 is X_2+square roots reduced mod 1, until enough samples are obtained. The distribution of these quasi-random sequences is much closer to uniform than the expected value of random sequences would be.


RE: Simulating and Fitting Data for Chemical Reactions - Namir - 11-28-2019 09:56 AM

Thanks for the info about quasi-random search. I will look up the Intenet for more information about the topic.

Namir


RE: Simulating and Fitting Data for Chemical Reactions - Namir - 11-28-2019 01:05 PM

Hi ttw,

Additional references, links, and equations are most welcome.

Namir (the algorithms junky)


RE: Simulating and Fitting Data for Chemical Reactions - Csaba Tizedes - 12-01-2019 02:35 PM

(11-28-2019 01:05 PM)Namir Wrote:  Additional references, links, and equations are most welcome.

If you interested, my method for finding the limit of a solution of a 1st order ODE (what is the value of a function if time goes to infinity, time is the independent, temperature is the measured value).

Rough translation: What is the room temperature if I measure it with a very slow, but accurate thermometer? I was in a very warm, but constant temperature room and I measured the room temperature with my CASIO wristwatch at every ten (10) minutes. After the 3rd measurement (t=30 min) the thermometer reading not fixed, I continued the measurement, but I want to calculate the room temperature earlier than it fixed.

The temperature can be estimated with a well known exponential solution of 1st order ODE, but in this case the ambient temperature is not known.

The method: Let's calculate SD of the constant (k) in every measured point and minimize the relative error (=SD of k / average k) in the function of the ambient temperature.

The result on the second page (diagram).

As you can see, the minimum is very sharp, if we can measure the temperature +/-0.05°C (yes, the displayed temperature resolution is 0.1°C on the CASIO), the minimum of the function also can be found with similar accuracy.

The two little program written on a CASIO fx-50F for faster calculation. The used data was t=0...30min range.

Csaba


RE: Simulating and Fitting Data for Chemical Reactions - SlideRule - 12-01-2019 11:32 PM

A closing suggested reference: Linear or Nonlinear Least-Squares Analysis of Kinetic Data

BEST!
SlideRule


RE: Simulating and Fitting Data for Chemical Reactions - Namir - 12-02-2019 06:09 PM

(12-01-2019 11:32 PM)SlideRule Wrote:  A closing suggested reference: Linear or Nonlinear Least-Squares Analysis of Kinetic Data

BEST!
SlideRule

Thanks for the reference. My study handles emulating the collection of lab data in two cases:

1) Analytical solution to the chemical reaction's differential equation is available. This approach allows the code to calculate concentrations of reactants/products or related information vs time. The values obtained are accurate.
2) Analytical solution to the chemical reaction's differential equation is NOT available due to the complexity of the ODE(s). The method I suggest is to use a numerical ODE solver (I chose Runge-Kutta-Fehlberg) to simulate the concentrations of the various chemicals involved in the chemical reaction being studied.

Of course if you have actual concentrations of chemicals measure in actual chemistry labs, then you don't need the above two methods.

I use a function (a custom version of a numerical ODE solver) to feed to the optimization method and primarily calculate the values for the reaction rate constants, and secondarily calculate either the initial (or equilibrium) values for the concentration of various chemicals. The values for the reaction rate constants are the main prize. The values for the concentrations give us an idea about errors in measurements and are of secondary importance.