<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Chapter 6 :: Coding For Chemists</title>
    <link>https://codingforchemistsbook.com/book_material/chapter-6/index.html</link>
    <description>Data Download Data for Chapter 6&#xA;Alternatively, individual files can be found in the Data section.&#xA;Code from chapter &#39;&#39;&#39; Fit data to multiple gaussian components and a linear background Requires: a .csv file with col 1 as wavelength and col 2 as intensity Written by: Ben Lear and Chris Johnson (authors@codechembook.com) v1.0.0 - 250207 - initial version &#39;&#39;&#39; import numpy as np from lmfit.models import LinearModel, GaussianModel from codechembook.quickPlots import plotFit import codechembook.symbols as cs from codechembook.quickTools import quickOpenFilename, quickPopupMessage # Ask the user for the filename containing the data to analyze quickPopupMessage(message = &#39;Select the file 0.001.csv&#39;) file = quickOpenFilename(filetypes = &#39;CSV Files, *.csv&#39;) # Read the file and unpack into arrays wavelength, absorbance = np.genfromtxt(file, skip_header=1, unpack = True, delimiter=&#39;,&#39;) # Set the upper and lower wavelength limits of the region of the spectrum to analyze lowerlim, upperlim = 450, 750 # Slice data to only include the region of interest trimmed_wavelength = wavelength[(wavelength &gt;= lowerlim) &amp; (wavelength &lt; upperlim)] trimmed_absorbance = absorbance[(wavelength &gt;= lowerlim) &amp; (wavelength &lt; upperlim)] # Construct a composite model and include initial guesses final_mod = LinearModel(prefix=&#39;lin_&#39;) # start with a linear model and add more later pars = final_mod.guess(trimmed_absorbance, x=trimmed_wavelength) # get guesses for linear coefficients c_guesses = [532, 580, 625] # initial guesses for centers s_guess = 10 # initial guess for widths a_guess = 20 # initial guess for amplitudes for i, c in enumerate(c_guesses): # loop through each peak to add corresponding gaussian component gauss = GaussianModel(prefix=f&#39;g{i+1}_&#39;) # create temporary gaussiam model pars.update(gauss.make_params(center=dict(value=c), # set initial guesses for parameters amplitude=dict(value=a_guess, min = 0), sigma=dict(value=s_guess, min = 0, max = 25))) final_mod = final_mod + gauss # add each peak to the overall model # Fit the model to the data and store the results result = final_mod.fit(trimmed_absorbance, pars, x=trimmed_wavelength) # Create a plot of the fit results but don&#39;t show it yet plot = plotFit(result, residual = True, components = True, xlabel = &#39;wavelength /nm&#39;, ylabel = &#39;intensity&#39;, output = None) # Add best fitting value for the center of each gaussian component as annotations for i in range(1, len(c_guesses)+1): # loop through components and add annotations with centers plot.add_annotation(text = f&#39;{result.params[f&#34;g{i}_center&#34;].value:.1f} {cs.math.plusminus} {result.params[f&#34;g{i}_center&#34;].stderr:.1f}&#39;, x = result.params[f&#39;g{i}_center&#39;].value, y = i*.04 + result.params[f&#39;g{i}_amplitude&#39;].value / (result.params[f&#39;g{i}_sigma&#39;].value * np.sqrt(2*np.pi)), showarrow = False) plot.show(&#39;png&#39;) # show the final plot Solutions to Exercises Targeted exercises Getting yes/no answers to questions posed by comparison statements Exercise 0 What do the following conditional expressions evaluate to (True or False)? First, write down what you think it should be before running the code, then run it to see if you are right. If you were wrong, explain what you did wrong. Suppose the following variables are already defined: trial_number = 4; pH = 2.53; acid = &#39;HCl&#39;; conc_stock = 1.0 # M</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <managingEditor>authors@codingforchemistsbook.com (Benjamin Lear and Christopher Johnson)</managingEditor>
    <webMaster>authors@codingforchemistsbook.com (Benjamin Lear and Christopher Johnson)</webMaster>
    <atom:link href="https://codingforchemistsbook.com/book_material/chapter-6/index.xml" rel="self" type="application/rss+xml" />
  </channel>
</rss>