Difference between revisions of "EGR 103/Fall 2021/Lab 5"

From PrattWiki
Jump to navigation Jump to search
(Created page with "<div class="noautonum">__TOC__</div> == Errors / Additions == None yet! == 5.1 Introduction == The main purpose of this lab will be to look at debugging, dictionaries, and u...")
 
(5.6 Individual Lab Assignment)
Line 29: Line 29:
 
=== 5.6.3 ===
 
=== 5.6.3 ===
 
For this one, you will be making changes to either the original or extended version of Chapra 4.2 in order to use a mapping to estimate roots of a function.  It is very similar to using the Newton method mapping to estimate values of a square root. Note - though the mapping for the lab assignment comes from a method called Newton-Raphson, its origin is different from the Newton method discussed in class for finding a square root.
 
For this one, you will be making changes to either the original or extended version of Chapra 4.2 in order to use a mapping to estimate roots of a function.  It is very similar to using the Newton method mapping to estimate values of a square root. Note - though the mapping for the lab assignment comes from a method called Newton-Raphson, its origin is different from the Newton method discussed in class for finding a square root.
 +
 +
==== Basin Plotter ====
 +
You will be using the code below to see how various initial guesses evolve into final estimates for the roots of the equation.  The sections of code are described below:
 +
*<syntaxhighlight lang=python>
 +
"""
 +
@author: DukeEgr93
 +
"""
 +
 +
# %% Initialize workspace
 +
import numpy as np
 +
from poly_root import calc_root
 +
import matplotlib.pyplot as plt
 +
</syntaxhighlight> In addition to importing numpy and matplotlib.pyplot, this will import the calc_root function from your poly_root.py file.  Make sure your file is called <code>poly_root.py</code> and make sure your function is called <code>calc_root</code> (not <code>iter_meth</code>).
 +
 +
*<syntaxhighlight lang=python>
 +
# %% Generate guesses and start lists
 +
xi = np.linspace(0, 5, 1000)
 +
rootlist = []
 +
iterlist = []
 +
</syntaxhighlight>
 +
*<syntaxhighlight lang=python>
 +
# %% Run program in a loop and store roots and number of iterations
 +
for init in xi:
 +
    out = calc_root(init, 1e-12, 1000)
 +
    rootlist += [out[0]]
 +
    iterlist += [out[2]]
 +
</syntaxhighlight>
 +
*<syntaxhighlight lang=python>
 +
# %% Make figure with function and map
 +
fig0, ax0 = plt.subplots(2, 1, num=0, clear=True)
 +
fig0.set_size_inches(6, 8, forward=True)
 +
ax0[0].plot(xi, xi ** 3 - 7 * xi ** 2 + 14 * xi - 8, "k-")
 +
ax0[0].grid(True)
 +
ax0[0].set(title="Function", ylabel="$f(x)$", xlabel="$x$")
 +
</syntaxhighlight>
 +
*<syntaxhighlight lang=python>
 +
ax0[1].plot(
 +
    xi,
 +
    (2 * xi ** 3 - 7 * xi ** 2 + 8) / (3 * xi ** 2 - 14 * xi + 14),
 +
    "r-",
 +
    label="map",
 +
)
 +
ax0[1].plot(xi, xi, "k:", label="new=old line")
 +
ax0[1].set_ylim([-10, 10])
 +
ax0[1].set(title="Map to Find Roots", ylabel="$x_{k+1}$", xlabel="$x_k$")
 +
ax0[1].legend(loc=0)
 +
 +
fig0.tight_layout()
 +
fig0.savefig("RootPlot0.png")
 +
</syntaxhighlight>
 +
*<syntaxhighlight lang=python>
 +
# %% Make figure with roots and iteration counts
 +
fig1, ax1 = plt.subplots(2, 1, num=1, clear=True)
 +
fig1.set_size_inches(6, 8, forward=True)
 +
 +
ax1[0].plot(xi, rootlist, "k.")
 +
ax1[0].set(title="Root Estimate", ylabel="Root", xlabel="Initial Guess")
 +
 +
ax1[1].plot(xi, iterlist, "k.")
 +
ax1[1].set(title="Iteration Count", ylabel="Iterations", xlabel="Initial Guess")
 +
 +
fig1.tight_layout()
 +
fig1.savefig("RootPlot1.png")
 +
</syntaxhighlight>
 +
*<syntaxhighlight lang=python>
 +
# %% Visualize roots and interation counts differently
 +
fig2, ax2 = plt.subplots(2, 1, num=2, clear=True)
 +
fig2.set_size_inches(6, 8, forward=True)
 +
rli = ax2[0].imshow(np.array([rootlist]), aspect="auto", extent=(xi[0], xi[-1], 0, 1))
 +
ax2[0].set_yticklabels([])
 +
fig2.colorbar(rli, ax=ax2[0])
 +
ax2[0].set(title="Root Estimate", xlabel="Initial Guess")
 +
</syntaxhighlight>
 +
*<syntaxhighlight lang=python>
 +
tli = ax2[1].imshow(np.array([iterlist]), aspect="auto", extent=(xi[0], xi[-1], 0, 1))
 +
ax2[1].set_yticklabels([])
 +
fig2.colorbar(tli, ax=ax2[1])
 +
ax2[1].set(title="Iteration Count", xlabel="Initial Guess")
 +
</syntaxhighlight>
 +
*<syntaxhighlight lang=python>
 +
fig2.tight_layout()
 +
fig2.savefig("RootPlot2.png")
 +
</syntaxhighlight>
  
 
{{Protected Class Document}}
 
{{Protected Class Document}}

Revision as of 14:19, 21 September 2021

Errors / Additions

None yet!

5.1 Introduction

The main purpose of this lab will be to look at debugging, dictionaries, and using Chapra Figure 4.2.

5.2 Resources

See main EGR 103 page for links to these

5.3 Getting Started

Beginning of Lab

5.4 Debugging Introduction

Learn more about debugging by reading Python:Debugging page.

5.5 Connect

The second Connect assignment is active. For Chapra 3.4, note that you will be using your function as part of the lab assignment and also note that the table in the Connect assignment may have different values from the book. Use the values from the Connect assignment in all cases. This Connect assignment can be done in small groups, but remember, Connect will give different values to different people so be sure to have your values in the code you write since the autograder will be using your values.

5.6 Individual Lab Assignment

5.6.1

Note that the skeleton of the code is already given. The chapra_0304.py file should not do any of the graphing - it should only contain a function that takes two arguments and then returns an array of temperatures.

5.6.2

For this one, you will be making changes to an extended version of the Chapra 4.2 code in order to use accumulation to estimate values of cosine. It is very similar to using an accumulation to estimate values of the exponential.

5.6.3

For this one, you will be making changes to either the original or extended version of Chapra 4.2 in order to use a mapping to estimate roots of a function. It is very similar to using the Newton method mapping to estimate values of a square root. Note - though the mapping for the lab assignment comes from a method called Newton-Raphson, its origin is different from the Newton method discussed in class for finding a square root.

Basin Plotter

You will be using the code below to see how various initial guesses evolve into final estimates for the roots of the equation. The sections of code are described below:

  • """
    @author: DukeEgr93
    """
    
    # %% Initialize workspace
    import numpy as np
    from poly_root import calc_root
    import matplotlib.pyplot as plt
    
    In addition to importing numpy and matplotlib.pyplot, this will import the calc_root function from your poly_root.py file. Make sure your file is called poly_root.py and make sure your function is called calc_root (not iter_meth).
  • # %% Generate guesses and start lists
    xi = np.linspace(0, 5, 1000)
    rootlist = []
    iterlist = []
    
  • # %% Run program in a loop and store roots and number of iterations
    for init in xi:
        out = calc_root(init, 1e-12, 1000)
        rootlist += [out[0]]
        iterlist += [out[2]]
    
  • # %% Make figure with function and map
    fig0, ax0 = plt.subplots(2, 1, num=0, clear=True)
    fig0.set_size_inches(6, 8, forward=True)
    ax0[0].plot(xi, xi ** 3 - 7 * xi ** 2 + 14 * xi - 8, "k-")
    ax0[0].grid(True)
    ax0[0].set(title="Function", ylabel="$f(x)$", xlabel="$x$")
    
  • ax0[1].plot(
        xi,
        (2 * xi ** 3 - 7 * xi ** 2 + 8) / (3 * xi ** 2 - 14 * xi + 14),
        "r-",
        label="map",
    )
    ax0[1].plot(xi, xi, "k:", label="new=old line")
    ax0[1].set_ylim([-10, 10])
    ax0[1].set(title="Map to Find Roots", ylabel="$x_{k+1}$", xlabel="$x_k$")
    ax0[1].legend(loc=0)
    
    fig0.tight_layout()
    fig0.savefig("RootPlot0.png")
    
  • # %% Make figure with roots and iteration counts
    fig1, ax1 = plt.subplots(2, 1, num=1, clear=True)
    fig1.set_size_inches(6, 8, forward=True)
    
    ax1[0].plot(xi, rootlist, "k.")
    ax1[0].set(title="Root Estimate", ylabel="Root", xlabel="Initial Guess")
    
    ax1[1].plot(xi, iterlist, "k.")
    ax1[1].set(title="Iteration Count", ylabel="Iterations", xlabel="Initial Guess")
    
    fig1.tight_layout()
    fig1.savefig("RootPlot1.png")
    
  • # %% Visualize roots and interation counts differently
    fig2, ax2 = plt.subplots(2, 1, num=2, clear=True)
    fig2.set_size_inches(6, 8, forward=True)
    rli = ax2[0].imshow(np.array([rootlist]), aspect="auto", extent=(xi[0], xi[-1], 0, 1))
    ax2[0].set_yticklabels([])
    fig2.colorbar(rli, ax=ax2[0])
    ax2[0].set(title="Root Estimate", xlabel="Initial Guess")
    
  • tli = ax2[1].imshow(np.array([iterlist]), aspect="auto", extent=(xi[0], xi[-1], 0, 1))
    ax2[1].set_yticklabels([])
    fig2.colorbar(tli, ax=ax2[1])
    ax2[1].set(title="Iteration Count", xlabel="Initial Guess")
    
  • fig2.tight_layout()
    fig2.savefig("RootPlot2.png")
    
Class Document Protection