CoCalc Public FilesAssignments / AssignmentCh6 / problemSetCh6.sagews
Authors: Edgar Arevalo, Lydia Novozhilova
Views : 50
reset
var('x')

<function reset at 0x7f25649c4050> x
#Exercise 1. Make a CAS function fcn_and_tangent(fcn,x0,a,b,c) that takes a differentiable function f, a point x0  in the function domain, end points of an interval that contains the point, and a small float c and returns
# (a) a figure with the function graph on [a,b] and the tangent line passing through the point (x0, f(x0)).
# (b) the relative error of the approximation of f(x0+c) by the linearization at the point x0.

def fcn_and_tangent(fcn,x0,a,b,c):
fcn_plot=plot(fcn,a,b,color='blue',thickness=2,figsize=[3,2])
fprime=diff(fcn,x)
tangent_eq=fcn(x0)+fprime(x=x0)*(x-x0)# Encode the rhs of the equation of the tangent line
tangent_plot=plot(tangent_eq,a,b,color='green',thickness=2,figsize=[3,2])
G=fcn_plot+tangent_plot+point([x0,fcn(x=x0)],pointsize=40,color='red')
rel_error=100*(tangent_eq(x=x0+c)-fcn(x=x0+c))/fcn(x=x0+c)
print "Error: %s %%\n" % rel_error # Tricky format; just use it as a "black box"
return G
fcn_and_tangent(cos(x),0.75,0,2,0.1)# Use your own example. Experiment with c, the change in x0

Error: 0.536657187170416 %
#####################
#Exercise 4. Make a CAS function plot_piecewise_fcn(L) that takes a list of data points of a continuous piecewise linear function and returns a figure with the plots of the function and its derivative. Assume that the points on the list L are sorted in the ascending order of their x-coordinates. Test your function on the set of points of your choice.


def my_piecewise_fcn(L):
n=len(L)
Gf=Graphics();Gfp=Graphics()# Graphics object provides another way of plotting multiple function in one figure
for j in range(n-1):
slope=(L[j+1][1]-L[j][1])/(L[j+1][0]-L[j][0])
fprime=plot(slope,L[j][0],L[j+1][0],thickness=2,color='red')
Gfp+=fprime
eq=L[j][1]+slope*(x-L[j][0])
fcn=plot(eq,L[j][0],L[j+1][0],thickness=2,color='blue')
Gf+=fcn
(Gf+Gfp).show(figsize=[4,3])

L=[[4.,1],[2.,3],[3.,-1.],[5.,0]]
my_piecewise_fcn(L)# Use your own example

#Remark: plotting piecewise function as a whole
f=piecewise([([(-1,0),1]),((0,1),2),((1,2),3)])
plot(f(x),(x,-1,2),detect_poles='False',thickness=3,figsize=[3,2])

#########################
#Exercise 2. Make a CAS function f_and_fprime(fcn, a, b) that takes a function differentiable on an interval [a, b] and returns a figure with plots of the function and its derivative on the interval. Use CAS command for finding the derivative function.
# No template. You are on your own.
def f_and_fprime(fcn, a, b)
G=Graphics()


###############################
#Exercise 5 (demo). Make a CAS function my_bisection(f, a, b, n) that takes function f,end points of an interval with a single root of the function, and a real number eps and returns the approximation of the root. The steps of the bisection method are repeated while the value of f at the midpoint of the current interval exceeds eps.Test your code on an example of your choice.



#This code is a simplified fragment from function written by William Stein, the founder of SageMath
def bisect(f,a,b,eps):
two = float(2)
while True:
c = (a+b)/two
fa = f(a); fb = f(b); fc = f(c)
if abs(fc) < eps: return c
if fa*fc < 0:
a, b = a, c
elif fc*fb < 0:
a, b = c, b
else:
raise ValueError, "f must have a sign change in the interval (%s,%s)"%(a,b)
f(x)=cos(x)-x
bisect(f,0,1,0.001)

0.7392578125
f(_)
#Remark about notation f(_):# The underline stand for the previous result of calculation

-0.000289009146790087



######################
#Exercise. Use your own example for testing the function bisect

######################
#Exercise 6. Make a CAS function my_MVT(f, a, b) that takes a function continuous on interval [a, b]  and differentiable in (a,b) and returns a figure similar to Fig. 6.4 in Lecture 6. You will need CAS root-finding command.
def my_MVT(f,a,b):
G=Graphics()
f_plot=plot(f,a,b,thickness=2,color='blue')
G+=f_plot
secant_slope=(f(x=b)-f(x=a))/(b-a)# enter the expression for the secant slope
secant_plot=plot(f(x=a)+secant_slope*(x-a),a,b,thickness=2,color='red')# enter the expression for the secant equation
G+=secant_plot
#Find the x-coordinate of the point of tangency for tangent parallel to the secant
t=find_root(diff(f(x),x)==secant_slope,a,b)
tangent_plot=plot(f(x=t)+secant_slope*(x-t),a,b,thickness=2,color='red')
G+=tangent_plot
G.show(figsize=[4,3])
my_MVT(sin(x),-1,3)