# part (a): v = [48,46,44,42,40,38,36,34,32,30,28,26,24,23,22,21,20,19,18,17,16,15,14,13,12] # data from Boyle table, column A p = [29+2/16,30+9/16,31+15/16,33+8/16,35+5/16,37,39+4/16,41+10/16,44+3/16,47+1/16,50+5/16,54+5/16,58+13/16,61+5/16,64+1/16,67+1/16,70+11/16,74+2/16,77+14/16,82+12/16,87+14/16,93+1/16,100+7/16,107+13/16,117+9/16] # data from Boyle table, column D # part (b) α = (1 ./ p) \ v # least-square fit # part (b) plot using Plots # you might have to install it first: import Pkg; Pkg.add("Plots") plot(p, v, seriestype = :scatter, label="data", title = "Problem 1(b): Boyle's Law fit", fmt=:png) plot!(p, α ./ p, label="fit $(round(α,sigdigits=3))/p") xlabel!("pressure (mercury height, in)") ylabel!("gas volume (height, in)") x = [ones(25) 1 ./ p] \ v # least-square fit x = [ones(25) log.(p)] \ log.(v) using LaTeXStrings import PyPlot as plt q = [3/5, 4/5] # an arbitrary unit vector F = [1 0; 0 1] - 2*q*q' # the reflector matrix x1 = [6, 8] # parallel to q x2 = [4, -3] # perpendicular to q x3 = [1, 5] # some other direction Fx1 = F*x1 Fx2 = F*x2 Fx3 = F*x3 # the hyperplane orthogonal to q plt.plot([-2*x2[1],2*x2[1]], [-2*x2[2],2*x2[2]],"k--",zorder=-10) plt.text(-2*x2[1], -2*x2[2]-1.3, L"\mathrm{plane} \perp q", rotation=rad2deg(atan(x2[2], x2[1]))) # the original points plt.arrow(0, 0, x1[1], x1[2], width=0.1, color="y") plt.text(x1[1]+0.3, x1[2], L"x_1", color="y") plt.arrow(0, 0, x2[1], x2[2], width=0.1, color="g") plt.text(x2[1]+0.3, x2[2], L"x_2 = Fx_2", color="g") plt.arrow(0, 0, x3[1], x3[2], width=0.1, color="b") plt.text(x3[1]+0.3, x3[2], L"x_3", color="b") # q plt.arrow(0, 0, q[1], q[2], width = 0.15, color ="r") plt.text(q[1]+0.5, q[2], L"q", color="r") # the resulting points plt.arrow(0, 0, Fx1[1], Fx1[2], color="y", head_width=0.4, linestyle=":", fill=false) plt.text(Fx1[1]+0.5, Fx1[2], L"Fx_1", color="y") plt.arrow(0, 0, Fx3[1], Fx3[2], color="b", head_width=0.4, linestyle=":", fill=false) plt.text(Fx3[1]-0.4, Fx3[2]-0.8, L"Fx_3", color="b") plt.axis("equal") [ 1 -1; 1 -3; 1 0 ] \ [0,0,-1] [-5/7, -2/7] q1 = 1/sqrt(2)*[1,-1,0,0] q2 = 1/sqrt(6)*[1,1,-2,0] q3 = 1/(2*sqrt(3))*[1,1,1,-3] Q = [q1 q2 q3] Q' * [1,1,1,1] Q'Q using LinearAlgebra # for I Q'Q ≈ I A = [[1,-1,0,0] [1,1,-2,0] [1,1,1,-3]] # [a1 a2 a3] Matrix(qr(A).Q) Q