lab1

1109 days ago by ncr006

#problem 1a P = plot(sin(x),(x,0,2*pi),color='red') Q = plot(cos(x),(x,0,2*pi),color='blue') show(P+Q) 
       

                                
                            

                                
#problem 1b integral_numerical(e^(-x),0,4) 
       
(2.1952465443761056, 0.0)
(2.1952465443761056, 0.0)
derivative? 
       

File: /nfs/software/sage-6.3/local/lib/python2.7/site-packages/sage/calculus/functional.py

Type: <type ‘function’>

Definition: derivative(f, *args, **kwds)

Docstring:

The derivative of f.

Repeated differentiation is supported by the syntax given in the examples below.

ALIAS: diff

EXAMPLES: We differentiate a callable symbolic function:

sage: f(x,y) = x*y + sin(x^2) + e^(-x)
sage: f
(x, y) |--> x*y + e^(-x) + sin(x^2)
sage: derivative(f, x)
(x, y) |--> 2*x*cos(x^2) + y - e^(-x)
sage: derivative(f, y)
(x, y) |--> x

We differentiate a polynomial:

sage: t = polygen(QQ, 't')
sage: f = (1-t)^5; f
-t^5 + 5*t^4 - 10*t^3 + 10*t^2 - 5*t + 1
sage: derivative(f)
-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5
sage: derivative(f, t)
-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5
sage: derivative(f, t, t)
-20*t^3 + 60*t^2 - 60*t + 20
sage: derivative(f, t, 2)
-20*t^3 + 60*t^2 - 60*t + 20
sage: derivative(f, 2)
-20*t^3 + 60*t^2 - 60*t + 20

We differentiate a symbolic expression:

sage: var('a x')
(a, x)
sage: f = exp(sin(a - x^2))/x
sage: derivative(f, x)
-2*cos(-x^2 + a)*e^(sin(-x^2 + a)) - e^(sin(-x^2 + a))/x^2
sage: derivative(f, a)
cos(-x^2 + a)*e^(sin(-x^2 + a))/x

Syntax for repeated differentiation:

sage: R.<u, v> = PolynomialRing(QQ)
sage: f = u^4*v^5
sage: derivative(f, u)
4*u^3*v^5
sage: f.derivative(u)   # can always use method notation too
4*u^3*v^5
sage: derivative(f, u, u)
12*u^2*v^5
sage: derivative(f, u, u, u)
24*u*v^5
sage: derivative(f, u, 3)
24*u*v^5
sage: derivative(f, u, v)
20*u^3*v^4
sage: derivative(f, u, 2, v)
60*u^2*v^4
sage: derivative(f, u, v, 2)
80*u^3*v^3
sage: derivative(f, [u, v, v])
80*u^3*v^3

File: /nfs/software/sage-6.3/local/lib/python2.7/site-packages/sage/calculus/functional.py

Type: <type ‘function’>

Definition: derivative(f, *args, **kwds)

Docstring:

The derivative of f.

Repeated differentiation is supported by the syntax given in the examples below.

ALIAS: diff

EXAMPLES: We differentiate a callable symbolic function:

sage: f(x,y) = x*y + sin(x^2) + e^(-x)
sage: f
(x, y) |--> x*y + e^(-x) + sin(x^2)
sage: derivative(f, x)
(x, y) |--> 2*x*cos(x^2) + y - e^(-x)
sage: derivative(f, y)
(x, y) |--> x

We differentiate a polynomial:

sage: t = polygen(QQ, 't')
sage: f = (1-t)^5; f
-t^5 + 5*t^4 - 10*t^3 + 10*t^2 - 5*t + 1
sage: derivative(f)
-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5
sage: derivative(f, t)
-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5
sage: derivative(f, t, t)
-20*t^3 + 60*t^2 - 60*t + 20
sage: derivative(f, t, 2)
-20*t^3 + 60*t^2 - 60*t + 20
sage: derivative(f, 2)
-20*t^3 + 60*t^2 - 60*t + 20

We differentiate a symbolic expression:

sage: var('a x')
(a, x)
sage: f = exp(sin(a - x^2))/x
sage: derivative(f, x)
-2*cos(-x^2 + a)*e^(sin(-x^2 + a)) - e^(sin(-x^2 + a))/x^2
sage: derivative(f, a)
cos(-x^2 + a)*e^(sin(-x^2 + a))/x

Syntax for repeated differentiation:

sage: R.<u, v> = PolynomialRing(QQ)
sage: f = u^4*v^5
sage: derivative(f, u)
4*u^3*v^5
sage: f.derivative(u)   # can always use method notation too
4*u^3*v^5
sage: derivative(f, u, u)
12*u^2*v^5
sage: derivative(f, u, u, u)
24*u*v^5
sage: derivative(f, u, 3)
24*u*v^5
sage: derivative(f, u, v)
20*u^3*v^4
sage: derivative(f, u, 2, v)
60*u^2*v^4
sage: derivative(f, u, v, 2)
80*u^3*v^3
sage: derivative(f, [u, v, v])
80*u^3*v^3
#problem 1c var('x') g = derivative(log(x^2+cos(x)),x) g(x=2).n() 
       
0.862396541435472
0.862396541435472
#problem 1d sum([1/i^2 for i in range(1,1001)]).n()-pi.n()^2/6 
       
-0.000999500166666500
-0.000999500166666500
#problem 2 ring=RealField(3) # make a field whose mantissa is three bits print ring('1').nextabove() # constructs the number 1.01 in binary, truncated print ring('1').nextabove().str(truncate=False) # same thing but with out truncating print ring('1').nextbelow().str(truncate=False) # constructs the number 0.111, without truncating ring=RealField(20) # make a real field whose mantissa is 20 bits print ring('1').nextabove().str(truncate=False) # constructs the number 1.000000000000000001 print ring('1').nextbelow().str(truncate=False) # constructs the number 0.1111111111111111111 ringup=RealField(3,rnd='RNDU') # forces ties to be broken by rounding up ringdown=RealField(3,rnd='RNDD') # forces ties to be broken by rounding down ring(1/9).str(truncate=False) ringup(1/9).str(truncate=False) ringdown(1/9).str(truncate=False) # these three show that since 1/9 is in a gap, that the rounding convention affects things ring(1/9).str(base=2) # 1/9 in binary to 3 bits ring(1/9).exact_rational() # converts the binary string of three bits into a rational number n(233017/2097152) #shows how close things are to reality x = 0.1+(0.2+0.3) x.str(truncate=False) x = (0.1+0.2)+0.3 x.str(truncate=False) # addition is not associative 
       
1.2
1.2
0.88
1.0000019
0.99999905
'0.60000000000000009'
1.2
1.2
0.88
1.0000019
0.99999905
'0.60000000000000009'
#problem 3 sys.float_info # max is the largest representable number, expressed in decimal # max_exp is the largest exponent when the base is 2 # max_10_exp is the largest exponent when the base is 10 # min is the smallest representable number in absolute value, expressed in decimal # min_exp is the smallest exponent when the base is 2 # min_10_exp is smallest exponent when the base is 10 # dig is the number of decimal digits # mant_dig is the number of binary digits # epsilon is machine epsilon # radix is the base # rounds is the rounding convention utilized by the system 
       
sys.float_info(max=1.7976931348623157e+308, max_exp=1024,
max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021,
min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16,
radix=2, rounds=1)
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
#problem 5 def machine_epsilon(prec): ring = RealField(prec) one = ring('1') return one.nextabove()-one 
       
machine_epsilon(53).str(base=10) 
       
'2.22044604925031e-16'
'2.22044604925031e-16'
#problem 9 def quadratic_formula(a,b,c): if b > 0: a,b,c = -a,-b,-c r1 = (-b + sqrt(b^2-4*a*c,prec=32))/2/a r2 = 2*c/(-b + sqrt(b^2-4*a*c,prec=32)) return r1,r2 
       
quadratic_formula(2,10,1) 
       
(-4.898, -0.1021)
(-4.898, -0.1021)
def quadratic_formula_naive(a,b,c): r1 = (-b + sqrt(b^2-4*a*c,prec=32))/2/a r2 = (-b - sqrt(b^2-4*a*c,prec=32))/2/a return r1,r2 
       
print quadratic_formula(2,10,1) print quadratic_formula_naive(2,10,1) 
       
(-4.89791576, -0.102084238)
(-0.102084238, -4.89791576)
(-4.89791576, -0.102084238)
(-0.102084238, -4.89791576)
print quadratic_formula(1,-4,3.99999) print quadratic_formula_naive(1,-4,3.99999) 
       
(2.00316228, 1.99683772)
(2.00316228, 1.99683772)
(2.00316228, 1.99683772)
(2.00316228, 1.99683772)
print quadratic_formula(1,-8.01,16.04) print quadratic_formula_naive(1,-8.01,16.04) 
       
(4.01000000, 4.00000000)
(4.01000000, 4.00000000)
(4.01000000, 4.00000000)
(4.01000000, 4.00000000)
print quadratic_formula(2*10^17,10^18,10^17) print quadratic_formula_naive(2*10^17,10^18,10^17) 
       
(-4.89791576, -0.102084238)
(-0.102084238, -4.89791576)
(-4.89791576, -0.102084238)
(-0.102084238, -4.89791576)
print quadratic_formula(10^(-17),-10^17,10^17) print quadratic_formula_naive(10^(-17),-10^17,10^17) 
       
(1.00000000e34, 1.00000000)
(1.00000000e34, 0.000000000)
(1.00000000e34, 1.00000000)
(1.00000000e34, 0.000000000)
print quadratic_formula(1,10^8,1) print quadratic_formula_naive(1,10^8,1) 
       
(-1.00000000e8, -1.00000000e-8)
(0.000000000, -1.00000000e8)
(-1.00000000e8, -1.00000000e-8)
(0.000000000, -1.00000000e8)
 
       
1.0
1.0