SharedAssignments / AssignmentCh6 / MiniProjectsCh6.sagewsOpen in CoCalc
Authors: Edgar Arevalo, Lydia Novozhilova
Views : 8
# Mini project: Geometric median
# Your are a postmaster for a district in some state. There are n towns along a straight road. Let x[j], j=0..n-1,be the locations of the towns along the road. Your job is to determine the best location for a new post office; it should be located in one of the towns. Assume that the best location is the one that minimizes the average distance between the post office and the towns.

#Follow the plan in Lecture 6 or design your own plan. Enumerate the steps of your solution.


#Template for Step 2.Plotting the objective function (the sum of the distances to the post office) is similar to Exercise 4 in the problem set for this chapter.
def sum_of_dist(L):
n=len(L)
intervals=[(x,L[j],L[j+1]) for j in range(?)]
G=Graphics()
#Make and plot linear pieces using the formula for objective function
for k in range(1,n):
piece=(?)*x+sum(L[j] for j in range(?,?))-sum(L[j] for j in range(?))
piece_plot=plot(?,intervals[k-1],thickness=2)
G+=piece_plot
G.show(figsize=[4,3])
S=[1,3,6,7,8,10];sum_of_dist(S)



#############################
# Mini project: Analysis of a function f(x)=x/(x+1)-cos(40/x) with many characteristic features
#Step 1 (manual)
#Domain= # Complete the line
# lim_(x->infinity)=0; lim_(x->-infinity)=0

#Step 2:Plot the function
f(x)=x/(x+1)-cos(40/x)
p = plot(f(x),-2,2, ymin=-10, ymax=10 ); p.show(figsize=[5,4])

#Step 3. Find the smallest negative and the largest positive x-intercepts for this function. (Note that the large intercept is REALLY large)
p_neg=plot(f(x),-.48,-.47);p_neg.show(figsize=[5,3],ymin=-1, ymax=.5)
find_root(f(x),-.48,-.47)


-0.4740729100063728
#The large positive
p_neg=plot(f(x),800,850);p_neg.show(figsize=[5,3],)
find_root(f(x),800,850)


800.83244633264
#Step 4 (read Lecture 4, answer the question on Step 4)
# NO BECAUSE THERE ARE MANY SOLUTIONS.



plot(f(x),-1.5,-1.05)

#Step 5. Find the largest local minimum on the interval (-infinity,-1).
deriv_plot=plot(derivative(f(x),x),-1.35,-1);deriv_plot.show(figsize=[5,4],ymin=-25,ymax=55)
find_root(derivative(f(x),x),-1.35,-1.)

#plot(derivative(f(x),x),-1.5,-1.1)
print "largest local minimum"

-1.294057347382484 largest local minimum