SAGE

Gaussian plot

Making a polar plot (describing the radiation pattern of an antenna)

# A linear broadside array of short vertical dipoles
# located along the z axis with 1/2 wavelength spacing
var('r, theta')
N = 7
normalized_element_pattern = sin(theta)
array_factor = 1 / N * sin(N * pi / 2 * cos(theta)) / sin(pi / 2 * cos(theta))
array_plot = polar_plot(abs(array_factor), (theta, 0, pi), color='red', legend_label='Array')
radiation_plot = polar_plot(abs(normalized_element_pattern * array_factor), (theta, 0, pi), color='blue', legend_label='Radiation')
combined_plot = array_plot + radiation_plot
combined_plot.xmin(-0.25)
combined_plot.xmax(0.25)
combined_plot.set_legend_options(loc=(0.5, 0.3))
show(combined_plot, figsize=(2, 5), aspect_ratio=1)
# Repeat the antenna pattern example with the Pyplot interface

import numpy
import matplotlib.pyplot as plt

path = '/Users/dev/Desktop/'

N = float(7)
theta = numpy.arange(0, numpy.pi, numpy.pi/100)

def normalized_element_pattern(theta):
    return abs(numpy.sin(theta))
def array_factor(theta, N):
    return abs(float(1 / N) * numpy.sin(float(N * pi / 2)
        * numpy.cos(theta))
        / numpy.sin(float(pi / 2) * numpy.cos(theta)))
plt.figure(figsize=(6, 4))
plt.subplot(121, polar=True)
plt.polar(theta, normalized_element_pattern(theta))
plt.title('Element factor')
plt.subplot(122, polar=True)
plt.polar(theta, array_factor(theta, N), color='red',
    label="Array factor")
plt.polar(theta, array_factor(theta, N) *
    normalized_element_pattern(theta),
    label="Pattern", color='blue')
plt.legend(loc='lower right', bbox_to_anchor = (1, 0))
plt.subplots_adjust(wspace=0.3)
plt.savefig(os.path.join(path, 'example3b.png'))
plt.close()

Mortgage calculation with SAGE

  • PMT: Payment
  • PV: Present Value (loan’s principal, amount barrowed)
  • r: monthly interest rate (compounded monthly)
  • t: number of years of the loan
  • m: 12 (compounding period)
  • i: r/m=r/12 (intereset rate per compounding period)
  • n: mt=12*t (loan’s term, number of monthly payments)

PMT(PV, i, n) = (PV*i)/(1-(1+i)^(-n))

PMT(PV, r, t) = (PVr/12)/(1-(1+r/12)^(-12t))

or with building functions on top of functions

PMT1(PV, r, t) = PMT(PV, i=r/12, n=12*t)

Example: 200,000 USD for a 30-year mortgage at 6.5% compounded monthly PMT(200000, 0.065, 30)

Compounded interest calculation with SAGE

Suppose you deposit $5000 in an account that earns 4.5% compounded monthly. You are curious when your total will reach $ 7000. Using the formula A = P(1+i)n, where P is the principal, A is the amount at the end, i is the interest rate per month, and n is the number of compounding periods (number of months), we have

7000 = 5000(1+0.045/12)^n
1.4=(1+0.0045/12)^n
log 1.4 = log (1+0.0045/12)^n
log 1.4 = n log (1+0.0045/12)

SAGE: n = log(1.4)/log(1+0.045/12)