# -*- coding: utf-8 -*- """ Created on Tue Jan 30 13:57:24 2018 @author: neil """ # a very simple example of using an imported method # Here we integrate x-squared from 0 to 1. The answer is 1/3. from scipy.integrate import quad #quad is the basic scipy integration method # define a function that we want to integrate def myfunc(y): return y*y result = quad(myfunc,0,1) # quad returns a tuple (integral,error) # note that if 'myfunc' only takes 1 input variable, using an imported method is usualy fairly # easy. If 'myfunc' takes more than one, then you need to be very careful that the order of the # variables is what the imported method expects # Below is a simple and a somewhat more elegant way of printing out the results integral = result[0] print("The integral of x*x from 0 to 1 is- " + str(integral)) # here we take advantage of the fact that you can add strings # 'str()' is a built-in that converts a number to a string # using some more elegant python, you should try to figure this out print("the integral of x*x from 0 to 1 is- {:8.3f}".format(quad(myfunc,0,1)[0])) # the 'string.format' method replaces the '{xxx}' in the string with variables # from the '()' of the format. A typical '{}' is {:8.3f} which means convert the # () data to a string representation of floating point number in 8 spaces, and 3 decimals