How a parameter affects a function in the IJulia Notebook

This blog post illustrates a simple way to visualize how the shape of a function changes as we tune a parameter in the function. We use the IJulia Notebook with the Gadfly and Interact packages.

As an example, the Langmuir equation is a ubiquitous model in chemical engineering to describe the adsorption of molecules on a surface as a function of concentration in the bulk phase. The Langmuir equation is:

The variables in this function are:

  • : the number of molecules adsorbed on the surface
  • : pressure of gas phase in contact with the surface

The parameters in the funtion are:

  • : the total number of adsorption sites on the surface
  • : Langmuir constant describing affinity of the molecule for the surface

We are interested in how the parameters and affect the shape of the Langmuir equation . Using Gadfly, we start by plotting for a given value of and .

using Gadfly

# make a pretty theme
plot_theme = Theme(line_width=1mm, 
        default_color=color("green"), 
        panel_fill=color("lightgray"),
        grid_color=color("white"));

# create an array of pressures
P = linspace(0, 1)  # bar

# define parameters
M = 3.0  # (mmol/g)
K = 5.0  # (1/bar)

# plot N(P)
plot(x=P, y=M*K*P./(1+K*P), 
        Geom.line, 
        Scale.y_continuous(maxvalue=3), Scale.x_continuous(maxvalue=1),
        Guide.xlabel("P (bar)"), Guide.ylabel("N (mmol/g)"),
        Guide.title("Langmuir isotherm"),
        plot_theme)
image

To get an idea of how and affect , we could manually change and in the above code and plot several times. With the Julia package Interact, there is a much nicer way. The Interact package creates a slider bar with the parameters and so that we can interact with the Gadfly plot and see how changes somewhat continuously.

The @manipulate macro in the Interact package creates the interactive plot with sliders to change the parameters. We can choose the parameter space of and to explore in the interactive plot. The code below explores and in increments of 0.1.

using Gadfly
using Interact

@manipulate for K = .1:.1:100, M = 1.:.1:3.
    plot(x=P, y=M*K*P./(1+K*P), 
            Geom.line, 
            Scale.y_continuous(maxvalue=3), Scale.x_continuous(maxvalue=1),
            Guide.xlabel("P (bar)"), Guide.ylabel("N (mmol/g)"),
            Guide.title("Langmuir isotherm"),
            plot_theme)
end

A screenshot of the result is below.

image

In the IJulia Notebook, we can play with the sliders to gain insight into how the parameters and affect the shape of the Langmuir equation. The IJulia notebook with this example is here.

At a high enough pressure, as the adsorption sites become filled with molecules. Intuitively, determines the value of at large pressures.

The Langmuir constant determines the initial slope of the adsorption curve at low pressures for a given . In other words, it determines the pressure at which the adsorption curve starts to saturate. If we note that , is inversely related to the pressure at which the adsorption sites are half-filled with molecules. Molecules with a high affinity with the surface will fill the adsorption sites at a relatively low pressure ; their adsorption curve will exhibit a high slope at low pressures. Therefore, a high affinity of the adsorbing molecule for the surface implies a high in the Langmuir model.

comments powered by Disqus