Chapter 3

431 days ago by monetta

1a)

total = vector([20414, 21071, 21586, 21510, 21356, 22454, 22044, 22345, 22702, 22361, 22922, 23365, 23912, 23637, 23865, 23873, 23674, 24812]) year = vector(range(1979, 1997)) plotPoints = year.transpose().augment(total.transpose()) myGraph = scatter_plot(plotPoints.rows(), facecolor='white', edgecolor='purple', markersize=20) myGraph 
       

1b)

var('a, b, x') f(x)= a*x + b fit = find_fit(plotPoints.rows(), f, parameters=[a, b], variables=[x], solution_dict=true) g(x) = fit[a]*x + fit[b] myGraph = myGraph + plot(g, 1979, 1996, color='orange', plot_points=2000, legend_label='linear fit') myGraph 
       

1c)

total2 = vector([20414, 21071, 21586, 21510, 21356, 22454, 22044, 22345, 22702, 22361, 22922, 23365, 23912, 23637, 23865, 23873]) year2 = vector(range(1979, 1995)) plotPoints2 = year2.transpose().augment(total2.transpose()) var('a, b, x') f(x)= a*x + b fit = find_fit(plotPoints.rows(), f, parameters=[a, b], variables=[x], solution_dict=true) h(x) = fit[a]*x + fit[b] myGraph2 = scatter_plot(plotPoints2.rows(), facecolor='white', edgecolor='purple', markersize=20) + plot(h, 1979, 1994, color='orange', plot_points=2000, legend_label='linear fit') myGraph2 
       

The linear fit predicts the enrollment of 1995 and 1996 fairly well. The error was no more than ±3%

print h(x) print "1995\t-->\t", int(h(1995)) print "1996\t-->\t", int(h(1996)) 
       
214.83695672116494*x - 404327.1737055374
1995	-->	24272
1996	-->	24487
214.83695672116494*x - 404327.1737055374
1995	-->	24272
1996	-->	24487

1d)

Calculate the enrollments for 1995 and 1996

print 'Year when enrollment should reach 30,000: ', int(solve([g(x)== 30000], x, solution_dict=true)[0][x]) print 'Year when enrollment should reach 35,000: ', int(solve([g(x)== 35000], x, solution_dict=true)[0][x]) 
       
Year when enrollment should reach 30,000:  2021
Year when enrollment should reach 35,000:  2044
Year when enrollment should reach 30,000:  2021
Year when enrollment should reach 35,000:  2044

 

5)

Rational function where m=2 and n=2

A = matrix(RR,[[1, i*pi/20, (i*pi/20)^2, -(i*pi/20)*tan(i*pi/20), -(i*pi/20)^2*tan(i*pi/20)] for i in range(5)]) y = matrix(RR,[[tan(i*pi/20)] for i in range(5)]) c = A\y g(x) = (c[0][0] + c[1][0]*x + c[2][0]*x^2) / (1 + c[3][0]*x + c[4][0]*x^2) p = plot(tan(x), color='orange', zorder=6, legend_label='tan(x)') + plot(g, color='blue', plot_points=50, marker='.', legend_label='rational function') p.xmin(0) p.xmax(pi/2) p.ymin(0) p.ymax(1) p 
       

Rational function where m=3 and n=4

A = matrix(RR,[[1, i*pi/20, (i*pi/20)^2, (i*pi/20)^3, -(i*pi/20)*tan(i*pi/20), -(i*pi/20)^2*tan(i*pi/20), -(i*pi/20)^3*tan(i*pi/20), -(i*pi/20)^4*tan(i*pi/20)] for i in range(3)]) y = matrix(RR,[[tan(i*pi/20)] for i in range(3)]) c = A\y g(x) = (c[0][0] + c[1][0]*x + c[2][0]*x^2) / (1 + c[3][0]*x + c[4][0]*x^2) p = plot(tan(x), color='orange', zorder=6, legend_label='tan(x)') + plot(g, color='blue', plot_points=50, marker='.', legend_label='rational function') p.xmin(0) p.xmax(pi/2) p.ymin(0) p.ymax(1) p 
       

Rational function where m=2 and n=4

A = matrix(RR,[[1, i*pi/20, (i*pi/20)^2, -(i*pi/20)*tan(i*pi/20), -(i*pi/20)^2*tan(i*pi/20), -(i*pi/20)^3*tan(i*pi/20), -(i*pi/20)^4*tan(i*pi/20)] for i in range(2)]) y = matrix(RR,[[tan(i*pi/20)] for i in range(2)]) c = A\y g(x) = (c[0][0] + c[1][0]*x + c[2][0]*x^2) / (1 + c[3][0]*x + c[4][0]*x^2) p = plot(tan(x), color='orange', zorder=6, legend_label='tan(x)') + plot(g, color='blue', plot_points=50, marker='.', legend_label='rational function') p.xmin(0) p.xmax(pi/2) p.ymin(0) p.ymax(1) p