def p1_7(part=None): """ Complete solution to problem 1.7. Parameters ---------- part: str, optional(default=None) The part number you would like evaluated. If this is left blank the default value of None is used and the entire problem will be solved. Returns ------- i-dont-know-yet """ x, h = make_grid(0, 5, 1000, 'cell_edge') # cell-edge data f = np.cos(x) y = np.cos(x) + .001 * np.random.rand(x.size) yp = np.zeros(y.shape) # Pre-allocate memory for yp and ypp ypp = np.zeros(y.shape) # Get centered estimate for yp(p) yp = centered_difference(y, 1, h, 'linear') ypp = centered_difference(y, 2, h, 'linear') # Get real derivatives fp = -np.sin(x) fpp = -np.cos(x) # Plot f, y, fp, yp plt.figure() plt.subplot(211) plt.plot(x, f, 'b', x, y, 'g') plt.legend(('Real', 'Noisy'), loc=0) plt.title('f(x)') plt.subplot(212) plt.plot(x, fp, 'k', x, yp, 'r') plt.legend(('Real', 'Estimated'), loc=0) plt.title('fp(x)') plt.draw() pass # return nothing
def p1_4(part=None): """ Complete solution to problem 1.4 Parameters ---------- part: str, optional(default=None) The part number you would like evaluated. If this is left blank the default value of None is used and the entire problem will be solved. Returns ------- i-dont-know-yet TODO: Call centered difference formula for derivatives. """ x, h = make_grid(0, 5, 100) f = lambda x: jn(0, x) # define function y = f(x) # Evaluate function on grid # Get numerical derivatives using routine from tools.py yplin = centered_difference(y, 1, h, 'linear') ypplin = centered_difference(y, 2, h, 'linear') ypquad = centered_difference(y, 1, h, 'quadratic') yppquad = centered_difference(y, 2, h, 'quadratic') # Calculate real derivatives fp = - jn(1, x) fpp = 1 / 2 * (- jn(0, x) + jn(2, x)) abs_err_linp = np.mean(np.abs(yplin - fp)) # linear 1st der. error abs_err_linpp = np.mean(np.abs(ypplin - fpp)) # linear 2nd der. error abs_err_quadp = np.mean(np.abs(ypquad - fp)) # quad 1st der. error abs_err_quadpp = np.mean(np.abs(yppquad - fpp)) # quad 2nd der. error print 'Linear first derivative error: %.8e' % (abs_err_linp) print 'Linear second derivative error: %.8e' % (abs_err_linpp) print 'Quadratic first derivative error: %.8e' % (abs_err_quadp) print 'Quadratic second derivative error: %.8e' % (abs_err_quadpp) plt.figure() plt.subplot(211) plt.plot(x, fp, x, yplin, x, ypquad) plt.title('First derivatives') plt.legend(('Real Derivative', 'Linear Extrapolation', 'quadratic Extrapolation'), loc=0) plt.subplot(212) plt.plot(x, fpp, x, ypplin, x, yppquad) plt.title('Second derivatives') plt.legend(('Real Derivative', 'Linear Extrapolation', 'quadratic Extrapolation'), loc=0) plt.draw() pass # return nothing