Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Views: 5222
reset var('x')
<function reset at 0x7f46cd1caf50> 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=cos(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 %