def Fig1(): 
# Figure #1 in the Lab: The solution of y'=y-2x+4, y(0)=0, is 
# y(x) = -2 + 2x + (ya + 2)e^x. This code plots the solution for 0<x<2,
# and then plots the approximation given by Euler's method
# Text Example
	a, b, ya = 0.0, 2.0, 0.0
	def f(x,ya): return -2. + 2.*x + (ya + 2.)*np.exp(x)
	
	def ode_f(x,y): return np.array([1.*y -2.*x + 4.])
	
	plt.plot(np.linspace(a,b,11), Euler(ode_f,a,b,10,ya) , 'b-',label="h = 0.2")
	plt.plot(np.linspace(a,b,21), Euler(ode_f,a,b,20,ya) , 'g-',label="h = 0.1")
	plt.plot(np.linspace(a,b,41), Euler(ode_f,a,b,40,ya) , 'r-',label="h = 0.05")
	
	x = np.linspace(0,2,200); k =int(200/40)
	plt.plot(x[::k], solution.function(x,f,0.0)[::k], 'k*-',label="Solution") # The solution 
	plt.plot(x[k-1::k], solution.function(x,f,0.0)[k-1::k], 'k-') # The solution 
	
	plt.legend(loc='best')
	plt.xlabel('x')
	plt.ylabel('y')
	plt.savefig('Fig1.pdf')
	# plt.show()
	plt.clf()
	return 
Example #2
0
 def test_non_integer_argument(self):
     with self.assertRaises(TypeError) as context:
         arg = "arg"
         result = function(arg)
         self.assertEqual('Argument must be interger',
                          context.exception.message,
                          'Only integers are allowed as input')
Example #3
0
 def test_negative_integer_argument(self):
     with self.assertRaises(ValueError) as context:
         arg = -10
         result = function(arg)
         self.assertEqual('Argument must be positive interger',
                          context.exception.message,
                          'Only positive integers are allowed as input')
def Exercise4old(): 
# Integral curves for f(x).
	a , b, n = 0.0,  1.6,  200
	k, x = int(n/20), np.linspace(a,b,n+1)
	def f(x,ya): return 4. - 2.*x + (ya - 4.)*np.exp(-x)
	
	plt.plot(x, solution.function(x,f,0.0), 'k-')
	plt.plot(x, solution.function(x,f,2.0), 'k-')
	plt.plot(x[::k], solution.function(x,f,4.0)[::k], 'k*-', label='Particular solution for 'r"$y' +y =  - 2x + 2 $.")
	plt.plot(x, solution.function(x,f,6.0), 'k-')
	plt.plot(x, solution.function(x,f,8.0), 'k-')
	plt.legend(loc='best')
	plt.xlabel('x')
	plt.ylabel('y')
	# plt.show()
	plt.savefig('Exercise4.pdf')
	plt.clf()
	return
def Exercise1(): 
	a, b, ya = 0.0, 2.0, 0.0
	def f(x,ya): return 4. - 2.*x + (ya - 4.)*np.exp(-x)
	
	def ode_f(x,y): return np.array([-1.*y -2.*x + 2.])
	
	
	plt.plot(np.linspace(a,b,11), Euler(ode_f,a,b,10,ya) , 'b-',label="h = 0.2")
	plt.plot(np.linspace(a,b,21), Euler(ode_f,a,b,20,ya) , 'g-',label="h = 0.1")
	plt.plot(np.linspace(a,b,41), Euler(ode_f,a,b,40,ya) , 'r-',label="h = 0.05")
	
	x = np.linspace(0,2,200); k =int(200/40)
	plt.plot(x[::k], solution.function(x,f,0.0)[::k], 'k*-',label="Solution") # The solution 
	plt.plot(x[k-1::k], solution.function(x,f,0.0)[k-1::k], 'k-') # The solution 
	
	plt.legend(loc='best')
	plt.xlabel('x')
	plt.ylabel('y')
	plt.savefig('Exercise1.pdf')
	# plt.show()
	plt.clf()
	return
Example #6
0
 def test_0_returns_empty_list(self):
     arg = 0
     result = function(arg)
     self.assertEqual(result, [],
                      msg='Expected {}, got {}'.format([], result))
Example #7
0
 def test_ouput1(self):
     arg = 10
     result = function(arg)
     self.assertEqual(result, [2, 3, 5, 7],
                      msg='Expected {}, got {}'.format([2, 3, 5, 7],
                                                       result))
Example #8
0
 def test_function(self):
     self.assertEqual(42, solution.function(7, 3))