Difference between revisions of "Homework 0"

From PrattWiki
Jump to navigation Jump to search
(ODE)
(Problems)
Line 6: Line 6:
 
The problems for this assignment focus on computational tools from EGR 103 you will need to solve some problems in this class. You will not be turning it in for a grade.  You can use either MATLAB or Python (or both!).  The solution will be posted on the Sakai site. If you have never used Python or MATLAB (or even if you have), there will be guidance on Pratt Pundit for how to do these problems.
 
The problems for this assignment focus on computational tools from EGR 103 you will need to solve some problems in this class. You will not be turning it in for a grade.  You can use either MATLAB or Python (or both!).  The solution will be posted on the Sakai site. If you have never used Python or MATLAB (or even if you have), there will be guidance on Pratt Pundit for how to do these problems.
 
=== Problems ===
 
=== Problems ===
 +
 
==== Linear fit ====
 
==== Linear fit ====
 
Most of the elements you will be learning about in this class have some kind of linear relationship between the voltage across the element and the current through it.  You have been given files for ten experimental runs where a particular voltage was applied to an element and the current through it was measured.  Write code that automates the process of  
 
Most of the elements you will be learning about in this class have some kind of linear relationship between the voltage across the element and the current through it.  You have been given files for ten experimental runs where a particular voltage was applied to an element and the current through it was measured.  Write code that automates the process of  
Line 12: Line 13:
 
* Storing the slope and intercept of the fit into arrays called <code>slope</code> and <code>intercept</code>, respectively.
 
* Storing the slope and intercept of the fit into arrays called <code>slope</code> and <code>intercept</code>, respectively.
 
Once all data files are finished, print a table with results of all 10 runs to the screen using scientific notation for all the numbers and making sure the numbers line up under each other.  Note that the slopes will generally be positive but the intercepts may be negative.   
 
Once all data files are finished, print a table with results of all 10 runs to the screen using scientific notation for all the numbers and making sure the numbers line up under each other.  Note that the slopes will generally be positive but the intercepts may be negative.   
 +
* References: [[Python:Fitting]] and specifically [[Python:Fitting#Polynomial_Fitting]], [[Python:Loading and Saving Data]], and [[Python:Flexible Programming]]
  
 
====Nonlinear fit====
 
====Nonlinear fit====
Line 21: Line 23:
 
</math></center>
 
</math></center>
 
use nonlinear regression to determine values for $$A$$, $$B$$, and $$C$$. Good initial values for the three coefficients are 1e-15, 20, and 0, respectively. Make sure the values are displayed onscreen.  Once you have calculated the values, make a plot of the current through the LED as a function of the voltage across the LED that has a curve for the original data as a solid black line and the model as a dashed green line.  Label the axes, include a legend, and turn on the grid.
 
use nonlinear regression to determine values for $$A$$, $$B$$, and $$C$$. Good initial values for the three coefficients are 1e-15, 20, and 0, respectively. Make sure the values are displayed onscreen.  Once you have calculated the values, make a plot of the current through the LED as a function of the voltage across the LED that has a curve for the original data as a solid black line and the model as a dashed green line.  Label the axes, include a legend, and turn on the grid.
 +
* References: [[Python:Fitting]] and specifically [[Python:Fitting#Nonlinear_Regression]], [[Python:Loading and Saving Data]], and [[Python:Plotting]]
  
 
==== Solve a linear algebra problem ====
 
==== Solve a linear algebra problem ====
Line 37: Line 40:
 
\end{align*}
 
\end{align*}
 
</math></center>
 
</math></center>
 +
* References: [[Python:Linear Algebra]]
  
 
==== Solve a linear algebra problem with a sweep ====
 
==== Solve a linear algebra problem with a sweep ====
Line 48: Line 52:
  
 
Write code that will solve $$i_1$$ and $$i_2$$ as a function of $$R_4$$ for 2000 linearly spaced values of $$R_4$$ between 0 and 5000.  Make a figure with two subplots - the top one should be a graph of $$i_1$$ as a function of $$R_4$$ and the bottom one should be a graph of $$i_2$$ as a function of $$R_4$$.  You do not need to label or title these plots nor do you need legends or grids.
 
Write code that will solve $$i_1$$ and $$i_2$$ as a function of $$R_4$$ for 2000 linearly spaced values of $$R_4$$ between 0 and 5000.  Make a figure with two subplots - the top one should be a graph of $$i_1$$ as a function of $$R_4$$ and the bottom one should be a graph of $$i_2$$ as a function of $$R_4$$.  You do not need to label or title these plots nor do you need legends or grids.
 +
* References: [[Python:Linear Algebra]] and specifically [[Python:Linear_Algebra#Sweeping_a_Parameter]] and [[Python:Plotting]]
  
 
==== Solve a single ODE ====
 
==== Solve a single ODE ====
Line 64: Line 69:
 
* $$y(0)=4$$, $$(t)=\sin(6t)$$
 
* $$y(0)=4$$, $$(t)=\sin(6t)$$
 
For each of the four cases, your code should make a new figure that graphs $$f(t)$$ as a solid blue line and $$y(t)$$ as a solid red line. Each graph should have a legend and a title that looks like <code>Case N</code> where <code>N</code> is the case number.
 
For each of the four cases, your code should make a new figure that graphs $$f(t)$$ as a solid blue line and $$y(t)$$ as a solid red line. Each graph should have a legend and a title that looks like <code>Case N</code> where <code>N</code> is the case number.
 +
* References: [[Python:Ordinary Differential Equations]] and [[Python:Plotting]]
  
 
=== Numerical / Graphical Solutions ===
 
=== Numerical / Graphical Solutions ===

Revision as of 01:47, 18 January 2022

This page is a supplement to "Homework 0," an assignment for students in 2nd-year and later engineering courses meant as a refresher (or introduction) for computational methods learned in EGR 103 that might prove useful in later classes. The assignment below is specifically written for either ECE 110 (Fundamentals of ECE) or EGR 224 (Electrical Fundamentals of Mechatronics) though over time it may evolve to be either more general or to have more specific examples for other classes (such as EGR 201).

Assignment

Introduction

The problems for this assignment focus on computational tools from EGR 103 you will need to solve some problems in this class. You will not be turning it in for a grade. You can use either MATLAB or Python (or both!). The solution will be posted on the Sakai site. If you have never used Python or MATLAB (or even if you have), there will be guidance on Pratt Pundit for how to do these problems.

Problems

Linear fit

Most of the elements you will be learning about in this class have some kind of linear relationship between the voltage across the element and the current through it. You have been given files for ten experimental runs where a particular voltage was applied to an element and the current through it was measured. Write code that automates the process of

  • Loading data from a file,
  • Determining the line of best fit for the current as a function of the voltage, and
  • Storing the slope and intercept of the fit into arrays called slope and intercept, respectively.

Once all data files are finished, print a table with results of all 10 runs to the screen using scientific notation for all the numbers and making sure the numbers line up under each other. Note that the slopes will generally be positive but the intercepts may be negative.

Nonlinear fit

Some of the elements you will be learning about have a nonlinear relationship between the voltage across the element and the current through it. You have been given a file (on Sakai in Resources) with voltages across a circuit with a resistor and an LED in series. The file has three columns. The first is total voltage across the resistor and LED; you won't need this one. The second is the voltage over the resistor; you will divide this by 1000 to get the LED current. The third is the voltage across the LED; you will use this as the LED voltage. Given a model of:

\( \begin{align*} i&=Ae^{Bv}+C \end{align*} \)

use nonlinear regression to determine values for $$A$$, $$B$$, and $$C$$. Good initial values for the three coefficients are 1e-15, 20, and 0, respectively. Make sure the values are displayed onscreen. Once you have calculated the values, make a plot of the current through the LED as a function of the voltage across the LED that has a curve for the original data as a solid black line and the model as a dashed green line. Label the axes, include a legend, and turn on the grid.

Solve a linear algebra problem

While there are several methods for coming up with systems of equations for a circuit, with purely resistive circuits these methods generally lead to a system of linear algebra equations to solve. Write code to numerically solve for the unknown values $$v_1$$, $$v_2$$, and $$v_3$$ assuming known values of:

\( \begin{align*} v_s&=10{\mbox V}& R_k&=1000~\Omega \end{align*} \)

for all $$k=1..7$$ and given the following equations:

\( \begin{align*} \frac{v_a-v_s}{R_1}+\frac{v_a-v_b}{R_3}+\frac{v_a-v_c}{R_2}&=0\\ \frac{v_b-v_s}{R_4}+\frac{v_b-v_a}{R_3}+\frac{v_b-v_c}{R_5}+\frac{v_b}{R_6}&=0\\ \frac{v_c-v_a}{R_2}+\frac{v_c-v_b}{R_5}+\frac{v_c}{R_7}&=0\\ \end{align*} \)

Solve a linear algebra problem with a sweep

Once you have symbolically solved for a system of equations, you may want to see what impact changing one or more of the values will have on the results. Given the following system of equations, assume that you want to know how the values of $i_1$ and $i_2$ change as $R_4$ changes:

\( \begin{align*} -10+1000i_1+3000(i_1-i_2)&=0\\ 3000(i_2-i_1)+750i_2+R_4i_2&=0 \end{align*} \)

Write code that will solve $$i_1$$ and $$i_2$$ as a function of $$R_4$$ for 2000 linearly spaced values of $$R_4$$ between 0 and 5000. Make a figure with two subplots - the top one should be a graph of $$i_1$$ as a function of $$R_4$$ and the bottom one should be a graph of $$i_2$$ as a function of $$R_4$$. You do not need to label or title these plots nor do you need legends or grids.

Solve a single ODE

Note: Some offerings of EGR 103 did not cover setting up and solving differential equations. Please take a look at the Pundit page Python:Ordinary Differential Equations for it!

Once we start working with reactive elements (capacitors and inductors), the models will move from linear algebra to differential equations. Write code to calculate the values of $$y$$ for 1000 times between 0 and 10 for the following differential equation:

\( \begin{align*} 2\frac{dy(t)}{dt}+y(t)=f(t) \end{align*} \)

with the following initial conditions and forcing functions:

  • $$y(0)=0$$, $$f(t)=1$$
  • $$y(0)=4$$, $$f(t)=1$$
  • $$y(0)=0$$, $$f(t)=\sin(6t)$$
  • $$y(0)=4$$, $$(t)=\sin(6t)$$

For each of the four cases, your code should make a new figure that graphs $$f(t)$$ as a solid blue line and $$y(t)$$ as a solid red line. Each graph should have a legend and a title that looks like Case N where N is the case number.

Numerical / Graphical Solutions

The codes to solve these problems will beon Sakai; for now, here are the outputs from the programs:

Linear fit

     Slope  Intercept
 5.787e+00  1.440e-01
 4.640e+00  3.912e-02
 5.934e+00  1.174e-01
 5.234e+00  9.758e-02
 4.898e+00  1.168e-01
 5.923e+00  1.745e-01
 4.179e+00 -9.780e-02
 5.296e+00  8.575e-02
 5.206e+00  1.369e-01
 5.205e+00  4.678e-02

Nonlinear fit

\( \begin{align*} A&\approx1e-18 & B&\approx18 & C&\approx7.5e-6 \end{align*} \)

Linear Algebra

\( \begin{align*} v_a&=6.25 & v_b&=5.0 & v_c&=3.75 \end{align*} \)

Linear Algebra Sweep

ODE