Presentation is loading. Please wait.

Presentation is loading. Please wait.

PYTHON Prof. Muhammad Saeed.

Similar presentations


Presentation on theme: "PYTHON Prof. Muhammad Saeed."— Presentation transcript:

1 PYTHON Prof. Muhammad Saeed

2 Python Dept. Of Comp. Sc. & IT, FUUAST
Calculus Integration & Differentiation Python Dept. Of Comp. Sc. & IT, FUUAST

3 Python Dept. Of Comp. Sc. & IT, FUUAST
Differentiation: Function & Table from numpy import array, sin, cos, diff from scipy.misc import derivative def f(x): return x**3 + 4*x**2*sin(x)-x*cos(x) derivative(f, 1.5, dx=1e-6) fnc=lambda x: x**3 + 4*x**2*sin(x)-x*cos(x) x1=[ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] y1=[1. 0, 1.105, 1.221, 1.350, , 1.649, , 2.014, 2.226, 2.460] x = array(x1) y = array(y1) h=x1[4]-x1[3] dy=diff(y) dydx=dy[3]/h print(‘\n\n Derivative at point {0} = {1:.15f}’.format(0.3,dydx)) Python Dept. Of Comp. Sc. & IT, FUUAST

4 Python Dept. Of Comp. Sc. & IT, FUUAST
Differentiation: 2nd Derivative, Polynomial import numpy as np h=x1[4]-x1[3] dy=np.diff(y) dy2=np.diff(dy) dy2dx2=dy2[3]/(2*h) print(‘\n\n 2nd Derivative at point {0} = {1:.5f}’.format(0.3,dy2dx2)) p = np.poly1d([2, 0, -1, 3]) p2 = np.polyder(p) print( 'derivative = ',p2) print('value of derivative at 4= ', p2(4)) Python Dept. Of Comp. Sc. & IT, FUUAST

5 Python Dept. Of Comp. Sc. & IT, FUUAST
Integration ‘’’ Methods for Integrating Functions given function object. quad General purpose integration. dblquad General purpose double integration. tplquad General purpose triple integration. fixed_quad Integrate func(x) using Gaussian quadrature of order n. quadrature Integrate with given tolerance using Gaussian quadrature. romberg Integrate func using Romberg integration. Methods for Integrating Functions given fixed samples. trapz Use trapezoidal rule to compute integral from samples. cumtrapz Use trapezoidal rule to cumulatively compute integral. simps Use Simpson's rule to compute integral from samples. romb Use Romberg Integration to compute integral from (2**k + 1) evenly-spaced samples. x=np.linspace(0, np.pi, 21) y=x**2*np.sin(x)*np.exp(3*x*np.sin(x)) ''' Python Dept. Of Comp. Sc. & IT, FUUAST

6 Python Dept. Of Comp. Sc. & IT, FUUAST
Integration Fixed Data import numpy as np from scipy.integrate import trapz, simps, romb x=np.linspace(0,np.pi,21) y=[0.000e+00, e-03, e-02, e-01, 7.026e-01, e+00, e+00, e+01, e+01, e+02, e+02, e+02, e+02, e+02, e+02, e+02, 3.122e+02, e+02, e+01, e+00, e-15] r1=trapz(y,x) r2=simps(y,x) print (‘\n Trapezoidal Rule=%f’ %(r1) print(‘\nSimpsons Rule=%f’ %(r2) from scipy import integrate fn = lambda x: 1/np.sqrt(np.pi) * np.exp(-x**2) result = integrate.romberg(fn, 0, 1) Python Dept. Of Comp. Sc. & IT, FUUAST

7 Python Dept. Of Comp. Sc. & IT, FUUAST
Integration Function from scipy.integrate import quad, nquad, dblquad, tplquad from scipy import exp, pi, sin from numpy import inf N = 3 a=1 b=1 c=1 def integrand1(x): return exp(-a*x)*sin(b*x)/(c*x**N) r1=quad(integrand1, 0, 1) print('integral using "quad": ',r1) def integrand2(t, x): return exp(-a*x*t)*sin(c*x)/b*t**N r2=nquad(integrand2, [[1, inf],[0, inf]]) print('Double integral using "nquad": ',r2) Python Dept. Of Comp. Sc. & IT, FUUAST

8 Python Dept. Of Comp. Sc. & IT, FUUAST
Integration Function from scipy.integrate import quad, nquad, dblquad, tplquad from scipy import exp, pi, sin from numpy import inf area = dblquad(lambda x, y: x*y, 0, 0.5, lambda x: 0, lambda x: 1-2*x) print('Double integral using "dblquad": ',area) Python Dept. Of Comp. Sc. & IT, FUUAST

9 Python Dept. Of Comp. Sc. & IT, FUUAST
Integration Function from scipy.integrate import quad, nquad, dblquad, tplquad from scipy import exp, pi, sin from numpy import inf def f(x, y): return x*y def bounds_y(): return [0, 0.5] def bounds_x(y): return [0, 1-2*y] r=nquad(f, [bounds_x, bounds_y]) print('Double integral using "nquad": ',r) def integrand3(x,y,z): return 9-z**3+2*x*y-7*z*y volume1 = nquad(integrand3,[[0, 2],[0, 1],[0,1]]) print('(1)Triple integral using "nquad": ',volume1) Python Dept. Of Comp. Sc. & IT, FUUAST

10 Python Dept. Of Comp. Sc. & IT, FUUAST
Integration Function from scipy.integrate import quad, nquad, dblquad, tplquad from scipy import exp, pi, sin from numpy import inf def integrand4(x,y,z): return x**2 * exp(-x**2) * exp(-0.5*y*z/x) volume2 = nquad(integrand4,[[0, 2],[0, 1],[0,1]]) print('(2)Triple integral using "nquad": ',volume2) inf = 1.e22 fn = lambda x,y,z: x**2 * exp(-x**2) * exp(-0.5*y*z/x) x1,x2 = 0, 2 y1,y2 = lambda x: 0, lambda x: 1 z1,z2 = lambda x,y: 0, lambda x,y: 1 volume3 = tplquad(fn,x1,x2,y1,y2,z1,z2) print('Triple integral using lambda and "tplquad": ',volume3) Python Dept. Of Comp. Sc. & IT, FUUAST

11 Python Dept. Of Comp. Sc. & IT, FUUAST
END Python Dept. Of Comp. Sc. & IT, FUUAST


Download ppt "PYTHON Prof. Muhammad Saeed."

Similar presentations


Ads by Google