Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
restrepo
GitHub Repository: restrepo/ComputationalMethods
Path: blob/master/activities/miscellanea-python.ipynb
934 views
Kernel: Unknown Kernel

Task 1

This homework is an activity intended to solve some basic problems in python in order to know its basic capabilities.

Due to: May 3


Miscellanea

Solve the next exercises:

1. Write a program that sort a list of N numbers.

#! /usr/bin/python import numpy as np N = 5 #The set is generated randomly X = np.random.random(N) print "Original set:", X print "Sorted set:", np.sort(X)
Original set: [ 0.60423365 0.06462303 0.01382518 0.58877186 0.63548625] Sorted set: [ 0.01382518 0.06462303 0.58877186 0.60423365 0.63548625]

2. Find all the elements of a list of N numbers that are greater than the mean value of them. Then, create a new list with those numbers.

#! /usr/bin/python import numpy as np N = 10 #The set is generated randomly X = 100*np.random.random(N) Y = X[ X>=X.mean() ] print "Original set", X print "Numbers greater than %1.2f:"%(X.mean()), Y
Original set [ 0.88939513 17.434237 58.54453737 3.66019105 5.22876937 26.54719482 76.98181862 75.8326774 57.25619751 60.1402436 ] Numbers greater than 38.25: [ 58.54453737 76.98181862 75.8326774 57.25619751 60.1402436 ]

3. Write a program that gives the name of students who passed a course (grade3.0  \geq 3.0\ \ over 5.0  5.0\ \ ).

#! /usr/bin/python import numpy as np #All students grades = {"Juanito":2.5, "Pepito":4.8, "Carlitos":3.0, "Maria":3.9, "Pepita":4.6, "Anita":1.9, "Bertulfo":0.9} #Students that approved winners = np.array(grades.keys())[ np.array(grades.values())>=3.0 ] print "The next students approved:", winners
The next students approved: ['Pepito' 'Carlitos' 'Pepita' 'Maria']

4. Write a program that evaluates the next expression for given values of xx, aa and bb:

sin(x)+cos(x)2(a+b)\frac{\sin(x) + \cos(x)}{2}(a+b)

you may find useful the library math for the functions sin(x)  \sin(x)\ \ and cos(x)\cos(x). Import it as import math

5. Find all the prime numbers between 1 and 100.

#! /usr/bin/python import numpy, math #Prime Checker def primes(upto): primes = numpy.arange(3,upto+1,2) isprime = numpy.ones((upto-1)/2,dtype=bool) for factor in primes[:int(math.sqrt(upto))]: if isprime[(factor-2)/2]: isprime[(factor*3-2)/2::factor]=0 return numpy.insert(primes[isprime],0,2) print "The prime numbers between 1 and 100 are:", primes(100)
The prime numbers between 1 and 100 are: [ 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97]

6. Write a program that calculates the factorial of a given number.

#! /usr/bin/python N = 5 #Factorial function def factorial(N): if N==1: return 1 else: return N*factorial(N-1) print "The factorial of %d is %d"%(N, factorial(N))
The factorial of 5 is 120

7. Write a program that evaluates the cos(x)  \cos(x)\ \ function by using the Taylor expansion using 22, 55 and 1010 terms.

$$\cos(x) = \sum_{n=0}^\infty \frac{(-1)^n}{(2n!)}x^{2n}\ \ \$$
#! /usr/bin/python from __future__ import division import math #Number of terms N = [2,5,10] x = 1.5 #Taylor expansion of cos(x) def taylor_cos( x, N ): term = lambda x,n: (-1)**n/(math.factorial(2*n))*x**(2*n) return np.sum( [term(x,n) for n in xrange(0,N)] ) for n in N: print "Taylor expansion of cos(%1.2f) for %d terms: %1.6f"%( x, n, taylor_cos(x,n) ) print "Exact value of cos(%1.2f): %1.6f"%( x, math.cos(x) )
Taylor expansion of cos(1.50) for 2 terms: -0.125000 Taylor expansion of cos(1.50) for 5 terms: 0.070753 Taylor expansion of cos(1.50) for 10 terms: 0.070737 Exact value of cos(1.50): 0.070737

8. Write a program that calculates π\pi by using the next approximations:

2π=22×2+22×2+2+22×\frac{2}{\pi} = \frac{\sqrt{2}}{2}\times \frac{\sqrt{2+\sqrt{2}}}{2}\times \frac{\sqrt{2+\sqrt{2+\sqrt{2}}}}{2}\times \cdots

and

π4=113+1517+19111+\frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \frac{1}{11} + \cdots

Which approximation takes less terms for achieving a good accuracy?