Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News Sign UpSign In
| Download
Views: 116
reset var('x')
<function reset at 0x7f28635c1f50> 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.
f(x)=2*x^2-x;fprime=diff(f,x);fprime(x=1)
3
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) 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.484064625987355 %
fcn_and_tangent(x^2,2,-4,4,.1)
Error: -0.249999999999995 %
##################### #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() 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=[[1.,1],[2.,3],[3.,-1.],[4.,0]] my_piecewise_fcn(L)# Use your own example
L=[[0,0],[2,1],[3,3],[4,-1]] my_piecewise_fcn(L)
#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.
############################### #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(a)-f(b))/(a-b)# enter the expression for the secant slope #secant_plot=plot(f(b)+secant_slope*(x-a),a,b,thickness=2,color='red')# enter the expression for the secant equation ## YOUR LINE secant_plot=plot(f(b)+secant_slope*(x-b),a,b,thickness=2,color='red') ## CORRECT LINE 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)==0,a,b) ##YOUR LINE t=find_root(diff(f(x),x)==secant_slope,a,b) #CORRECT LINE tangent_eq=f(t)+secant_slope*(x-t) tangent_plot=plot(tangent_eq,a,b,thickness=2,color='red') G+=tangent_plot G.show(figsize=[4,3]) my_MVT(sin(x),-1,3)