time $t$ for this system|\n", "|2. |${\\Delta S}$ ${= \\int_{300}^{400}\\frac{mc}{T}\\ dT }$|$m$ is mass; $m=1$ kg

$c$ is specific heat capacity; $c = 4190$ J/kg K

$T$ is temperature; $T = [300, 400]$ K|Change in entropy for

thermal processes|\n", "|3. |$\\Phi = \\int_1^2 \\frac{Q}{4 \\pi \\epsilon_o r^2} dr$|$r$ is distance; $r = [1, 2]$ m

$\\epsilon_o$ is the Permittivity of Free Space

$Q$ is the charge; $Q = 1$ C|$\\Phi$ is the electric potential energy

gained by moving along line $r$|\n", "|4. |$I = \\int_0^\\infty \\frac{2 \\pi h c^2}{\\lambda^5(e^{hc/\\lambda k T} - 1)}\\ d\\lambda$|$h$ is Planck's constant

$c$ is speed of light

$k$ is Boltzmann's Constant

$T$ is the absolute temperature; T = 400K

$\\lambda$ is wavelength; $\\lambda = [0, \\infty]$ m|Planck's radiation law;

Integrating gives Stefan Boltzmann Law|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Analytically integrate each for the region and values provided, and record your answer in the `analytical_result` variables below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "analytical_result_expr1 = None # replace the None's with your results\n", "analytical_result_expr2 = None\n", "analytical_result_expr3 = None\n", "analytical_result_expr4 = None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Show your work in the cell below, either in a picture file for written derivations or in Latex" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write the each expression to be integrated as a python function. \n", "\n", "> For example, if I want to integrate the expression\n", "> \n", "> $$ \\Large \n", "> F = \\int 3x^2 + 17\\ dx\n", "> $$\n", "> \n", "> then my integrand is\n", "> \n", "> $$ \\Large\n", "> f(x) = 3x^2 + 17\n", "> $$\n", "> \n", "> and I would write the following function:\n", "> ```\n", "> def integrand(x):\n", "> f_x = 3*np.power(x, 2) + 17\n", "> return f_x\n", "> ```\n", "> \n", "> This function takes `x` as my function argument, and returns the calculated value `f_x`. Note that I am not yet evaluating the limits of my integrand." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Helpful constants:\n", "pi = np.pi #unitless\n", "c = 2.99E8 #m/s\n", "h = 6.62607015E-34 #J\n", "k = 1.380649E-23 #J/K\n", "epsilon = 8.854187817E-12 #F/m\n", "sigma = 5.6704E-8 #W/(m^2 K^4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def integrand(x):\n", " # YOUR CODE HERE\n", " raise NotImplementedError()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Randomly choose values for `x` from a distribution between the limits of the definite integral. \n", "\n", ">*Hint*: if one of your limits is $\\infty$, it is okay to approximate it with a large number. Another way to do it is to plot [x, f(x)] and visually estimate the most important region of your integration." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lower_limit = # this can be a float\n", "upper_limit = # this can be a float\n", "num_x_vals = # this must be an integer value less than or equal to 10^8\n", "x_vals = np.random.uniform(lower_limit, upper_limit, num_x_vals)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the `f_x` values:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = integrand(x_vals)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "approx_int = ((upper_limit - lower_limit)*np.sum(y))/(num_x_vals - 1)\n", "print(f\"The Monte Carlo approximation is {approx_int}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the error between the `approx_int` and the `analytical_result` variables using one or more of the metrics discussed above" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mse = None # replace with your calculation\n", "print(f\"The Mean Squared Error is {mse}\")\n", "\n", "pe = None # replace with your calculation\n", "print(f\"The Percent Error is {pe}%\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we want to visualize how the error decreases as the number of random trials `num_x_vals` increases. Write code to the do the following:\n", "\n", "* Using the error metric you decided on above, write a for-loop that calculates the error as a function of the number of points you sampled. For example, calculate the error when you summed two values of $\\langle F^N \\rangle$, then calculate the error for three summed values of $\\langle F^N \\rangle$, and so on until you have calculated the errors for the full range of $\\langle F^N \\rangle$.\n", "\n", "* IMPORTANT: You do not need to re-do the experiment to calculate this analysis; if you do it will slow down your for-loop and potentially crash your notebook kernel. Instead, you will want to reuse all of the integrand values are stored in the `y` variable. Python indexing into this list using the `y[:N]` functionality will give you the first `N` values in this list. The first `N` values can then be used to calculate a $\\langle F^N \\rangle$ value for the first `N` samples.\n", "\n", "* Make a figure showing how the error changes with the number of values in the sum." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "error_data = [] \n", "# Write code here to fill error_data with the percentage error corresponding to each of the number of points you sampled in the MC integration" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, plot it" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(np.linspace(2, 2000000, 1999998, endpoint=True), error_data)\n", "plt.xlabel(\"Number of Values in Sum\")\n", "plt.ylabel(\"Percent Error\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Answer the following questions:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Model vs Simulation: In your own words, describe the difference between a model and a simulation. Give your own example of a model, and how you would simulate it.\n", "\n", "> **Answer:**\n", "\n", "- Markov Chain: In your own words, describe a Markov Chain and its properties. Give your own example of a stochastic system and how you would implement a Markov Chain for it.\n", "\n", "> **Answer:**" ] } ], "metadata": { "celltoolbar": "Create Assignment", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.2" } }, "nbformat": 4, "nbformat_minor": 2 }