EGR 103/Spring 2018/Lab 5

From PrattWiki
Jump to navigation Jump to search

Typographical errors

None identified yet!

5.1 Introduction

The main purpose of this lab is to learn even more about structured programming, including iterative solutions, as well as about graphics.

5.4.1 Chapra 4.12

The primary goal here is to calculate a series of estimates to the \(\sin(x)\) function using a Maclaurin series.

  • The core of the function is Chapra Figure 4.2. Learn Chapra Figure 4.2.
  • The IterMethNew code adds two new tracking variables - be sure to understand how those work before working on your own code.
  • Keep in mind that there are two numbers to keep track of - which iteration you are on, and what the value of the power of \(x\) and the factorial in the numerator might be. They can be written in terms of each other...
  • Note that your fourth and fifth outputs will have several entries depending on what stopping condition was reached:
>> [a,b,c,d,e] = SinSeries(pi/6, .1, 5)
a =
   5.0000e-01
b =
   6.5590e-02
c =
     3
d =
   5.2360e-01   4.9967e-01   5.0000e-01
e =
   1.0000e+02   4.7880e+00   6.5590e-02

and

>> [a,b,c,d,e] = SinSeries(pi/6, 1e-12, 5)
a =
   5.0000e-01
b =
   1.6303e-06
c =
     5
d =
   5.2360e-01   4.9967e-01   5.0000e-01   5.0000e-01   5.0000e-01
e =
   1.0000e+02   4.7880e+00   6.5590e-02   4.2814e-04   1.6303e-06
  • Do not change anything about the SinSeriesChecker program! When you want to run it, type
SinSeriesChecker('NetID')

where NetID is your NetID. For instance, for Dr. G it would be:

SinSeriesChecker('mrg')

5.4.2 Chapra 4.28

Getting this code to work for a new series should require very few changes from the SinSeriesChecker or the IterMethNew programs. Hint: generally the only changes you will need to make involve the lines of code that start with sol =.

5.4.3 Data Logger

The code you need here
Mostly exists on Pundit
(sans the fprintf...)

Four words of caution
To remember when coding:
Columns are not rows!

Sample TempCheck.m Run

From MATLAB:

>> TempCheck('blah')
blah
out =
   3.3524e+02
Readings Minimum Average Maximum
       1  335.24  335.24  335.24
out =
   3.0038e+02
Readings Minimum Average Maximum
       2  300.38  317.81  335.24
out =
   2.7228e+02
Readings Minimum Average Maximum
       3  272.28  302.63  335.24
out =
   3.0576e+02
Readings Minimum Average Maximum
       4  272.28  303.41  335.24
out =
   3.3455e+02
Readings Minimum Average Maximum
       5  272.28  309.64  335.24
out =
   2.6636e+02
Readings Minimum Average Maximum
       6  266.36  302.43  335.24

Sample Text File

In UNIX:

> more MyTemps.txt
   3.3523719e+02
   3.0037886e+02
   2.7227560e+02
   3.0576493e+02
   3.3454886e+02
   2.6636305e+02

5.4.4 Palm 5.33

The main programming concepts here are creating contour plots and imagesc plots. More information on the former can be found at MATLAB:Contour_Plots. The temperatures are in degrees Celsius and the distances are in meters.

5.4.5 Palm 4.28

This problem expands on making surface plots to using 2-D matrices to solve optimization problems. MATLAB:Plotting_Surfaces and MATLAB:Contour_Plots will be useful in making the plots. The section MATLAB:Plotting_Surfaces#Finding_Minima_and_Maxima_in_2-D will be especially helpful in terms of finding the best location for the distribution center.

When testing your code, best bet is to write code to first calculate the distances between one customer and your grid of points. Make a surface plot of that to see if the distance calculations are 0 at the customer and increase radially from there. You can make a one customer data file by running MakeCustomers and entering 1 as the NetID. Then try to figure out how to get the cost function for just that one customer and plot it. With one customer, you can also try to get the rest of the plots as well as the customer map working. You can also find the ideal location and its cost - it should be where the customer is (which is (-22, 4) using MakeCustomersfor one customer) and the cost should be free.

Once you have figured out one customer, put some thought into the work you need to do for two customers - how are you going to get and store the locations and volumes? What's the best way to get the total cost at each location for two customers? For N customers? Note that with two customers the best location will be at (12, -8) because that grid point happens to be closest to the straight line between the customer at (9,-28) and the customer at (15,11); since the two customers have equal volumes, the optimal location is anywhere along that straight line. You can see this in the contour plot: DistContourTwo.png

Also, Douglas Adams wrote funny books.

For MAC People

There is a known graphics issue once a surface has too many nodes. If you get an infinite "Busy" warning or an error about libGL, you need to start your figure with:

figure(1)
clf
set(gcf, 'Renderer', 'ZBuffer')

You may choose to just do this in general for surf, surfc, mesh, or meshc plots to avoid the infinite business error.


General Concepts

This section is not in the lab report but rather has some items in it that span multiple problems in the lab.

Determining and Locating Extrema

See MATLAB:Plotting_Surfaces#Finding_Minima_and_Maxima_in_2-D for some examples.

Class Document Protection