[2021 Fall] Final Exam - Question 1¶
Course: AP3021
In [1]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
#讀資料
with open("./data/obs_2021-03-18_12_00_00.sound") as a:
for i in range(2):
head=a.readline()
data=a.readlines()
T,P=np.zeros((len(data)))*np.nan,np.zeros((len(data)))*np.nan
for i,tmp in enumerate(data):
T[i]=tmp.split()[3]
P[i]=tmp.split()[0]
#確認資料
plt.figure(figsize=(3,9))
plt.scatter(T,P)
plt.gca().invert_yaxis()
plt.title("Temperature Profile")
plt.ylabel("Pressure (hPa)")
plt.xlabel("Temperature (degree Celsius)")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
#讀資料
with open("./data/obs_2021-03-18_12_00_00.sound") as a:
for i in range(2):
head=a.readline()
data=a.readlines()
T,P=np.zeros((len(data)))*np.nan,np.zeros((len(data)))*np.nan
for i,tmp in enumerate(data):
T[i]=tmp.split()[3]
P[i]=tmp.split()[0]
#確認資料
plt.figure(figsize=(3,9))
plt.scatter(T,P)
plt.gca().invert_yaxis()
plt.title("Temperature Profile")
plt.ylabel("Pressure (hPa)")
plt.xlabel("Temperature (degree Celsius)")
plt.show()
(a)
In [2]:
Copied!
def D(l, y): #微分,中插為主,前後為前插及後插
ans = []
h = float(l[1] - l[0])
for i in range(0, len(l)):
if i == 0:
d = (float(y[i+1] - y[i])) / (h)
elif i == len(l) - 1:
d = (float(y[i] - y[i-1])) / (h)
else:
d = (float(y[i+1] - y[i-1])) / (2*h)
ans.append(d)
return ans
temp_grad = D(P, T)
#print(temp_grad)
for i in range(0, len(temp_grad)):
print('When P =', P[i], ', temperature gradient =', temp_grad[i])
def D(l, y): #微分,中插為主,前後為前插及後插
ans = []
h = float(l[1] - l[0])
for i in range(0, len(l)):
if i == 0:
d = (float(y[i+1] - y[i])) / (h)
elif i == len(l) - 1:
d = (float(y[i] - y[i-1])) / (h)
else:
d = (float(y[i+1] - y[i-1])) / (2*h)
ans.append(d)
return ans
temp_grad = D(P, T)
#print(temp_grad)
for i in range(0, len(temp_grad)):
print('When P =', P[i], ', temperature gradient =', temp_grad[i])
When P = 1009.9839 , temperature gradient = 0.07667405009371818 When P = 1008.8101 , temperature gradient = 0.05111603339581111 When P = 1008.2232 , temperature gradient = 0.055375702845462796 When P = 1007.0579 , temperature gradient = 0.059635372295114474 When P = 1006.476 , temperature gradient = 0.029817686147557237 When P = 1005.894 , temperature gradient = 0.02555801669790707 When P = 1005.3077 , temperature gradient = 0.04259669449650926 When P = 1004.1254 , temperature gradient = 0.04685636394616094 When P = 1003.5343 , temperature gradient = 0.029817686147557237 When P = 1002.9434 , temperature gradient = 0.029817686147557237 When P = 1002.3524 , temperature gradient = 0.029817686147557237 When P = 1001.7685 , temperature gradient = 0.04259669449650926 When P = 1000.6122 , temperature gradient = 0.03833702504685909 When P = 1000.0319 , temperature gradient = 0.03407735559720892 When P = 998.8713 , temperature gradient = 0.051116033395812624 When P = 998.291 , temperature gradient = 0.04685636394616094 When P = 997.711 , temperature gradient = 0.03407735559720892 When P = 997.1311 , temperature gradient = 0.025558016697905556 When P = 996.5512 , temperature gradient = 0.029817686147555724 When P = 995.3936 , temperature gradient = 0.029817686147557237 When P = 994.8185 , temperature gradient = 0.021298347248255387 When P = 994.2434 , temperature gradient = 0.02555801669790707 When P = 992.5253 , temperature gradient = 0.017038677798605215 When P = 991.9564 , temperature gradient = -0.008519338899303366 When P = 990.8186 , temperature gradient = -0.012779008348953534 When P = 990.2497 , temperature gradient = -0.021298347248253875 When P = 989.6808 , temperature gradient = -0.06389504174476617 When P = 988.543 , temperature gradient = -0.06815471119441632 When P = 987.9741 , temperature gradient = -0.04259669449650926 When P = 987.4052 , temperature gradient = -0.07667405009371818 When P = 986.2715 , temperature gradient = -0.1490884307377862 When P = 985.1433 , temperature gradient = -0.14482876128813452 When P = 984.5792 , temperature gradient = -0.11075140569092559 When P = 984.0151 , temperature gradient = -0.11927074459022895 When P = 983.451 , temperature gradient = -0.24280115863010807 When P = 981.1947 , temperature gradient = -0.22576248083150438 When P = 980.6306 , temperature gradient = -0.18316578633499359 When P = 979.5024 , temperature gradient = -0.19594479468394713 When P = 978.9383 , temperature gradient = -0.09371272789232339 When P = 978.3742 , temperature gradient = -0.10649173624127542 When P = 977.2461 , temperature gradient = -0.11501107514057726 When P = 976.6839 , temperature gradient = -0.09371272789232188 When P = 976.1262 , temperature gradient = -0.08945305844267172 When P = 975.5685 , temperature gradient = -0.08093371954336986 When P = 975.0108 , temperature gradient = -0.10223206679162374 When P = 973.8953 , temperature gradient = -0.09371272789232188 When P = 973.3376 , temperature gradient = -0.11501107514057726 When P = 971.1068 , temperature gradient = -0.11927074459022895 When P = 970.5491 , temperature gradient = -0.055375702845462796 When P = 969.9914 , temperature gradient = -0.034077355597207405 When P = 969.4336 , temperature gradient = -0.029817686147557237 When P = 968.8759 , temperature gradient = -0.025558016697905556 When P = 968.3182 , temperature gradient = -0.021298347248255387 When P = 967.7605 , temperature gradient = -0.029817686147557237 When P = 967.2028 , temperature gradient = -0.04685636394616094 When P = 966.0954 , temperature gradient = -0.04685636394616094 When P = 965.5427 , temperature gradient = -0.03407735559720892 When P = 964.4375 , temperature gradient = -0.0383370250468606 When P = 963.3322 , temperature gradient = -0.025558016697905556 When P = 962.7796 , temperature gradient = -0.012779008348952021 When P = 962.2269 , temperature gradient = -0.017038677798603703 When P = 961.6743 , temperature gradient = -0.029817686147557237 When P = 960.569 , temperature gradient = -0.02555801669790707 When P = 960.0164 , temperature gradient = -0.008519338899301851 When P = 959.4638 , temperature gradient = -0.012779008348952021 When P = 958.3585 , temperature gradient = -0.012779008348953534 When P = 957.8059 , temperature gradient = -0.008519338899301851 When P = 957.2533 , temperature gradient = -0.008519338899301851 When P = 956.7043 , temperature gradient = -0.012779008348953534 When P = 954.5158 , temperature gradient = -0.008519338899301851 When P = 953.9686 , temperature gradient = -0.0 When P = 953.4215 , temperature gradient = -0.004259669449651683 When P = 952.8744 , temperature gradient = -0.0 When P = 951.7801 , temperature gradient = 0.004259669449651683 When P = 951.233 , temperature gradient = -0.0 When P = 950.6858 , temperature gradient = 0.004259669449650169 When P = 949.5915 , temperature gradient = 0.008519338899301851 When P = 948.4973 , temperature gradient = 0.008519338899303366 When P = 947.9501 , temperature gradient = 0.004259669449651683 When P = 947.403 , temperature gradient = 0.012779008348952021 When P = 946.3202 , temperature gradient = 0.017038677798603703 When P = 945.7802 , temperature gradient = 0.012779008348953534 When P = 944.7002 , temperature gradient = 0.012779008348952021 When P = 944.1602 , temperature gradient = 0.008519338899301851 When P = 943.6202 , temperature gradient = 0.008519338899303366 When P = 943.0801 , temperature gradient = 0.012779008348953534 When P = 942.5401 , temperature gradient = 0.017038677798603703 When P = 942.0001 , temperature gradient = 0.021298347248253875 When P = 940.9201 , temperature gradient = 0.017038677798603703 When P = 940.3801 , temperature gradient = 0.012779008348953534 When P = 939.8401 , temperature gradient = 0.029817686147557237 When P = 938.22 , temperature gradient = 0.042596694496510774 When P = 937.14 , temperature gradient = 0.034077355597207405 When P = 936.06 , temperature gradient = 0.021298347248255387 When P = 935.52 , temperature gradient = 0.017038677798605215 When P = 934.98 , temperature gradient = 0.012779008348952021 When P = 934.4399 , temperature gradient = 0.017038677798603703 When P = 933.8999 , temperature gradient = 0.02555801669790707 When P = 933.3599 , temperature gradient = 0.021298347248255387 When P = 932.8199 , temperature gradient = 0.021298347248253875 When P = 932.281 , temperature gradient = 0.034077355597207405 When P = 931.2151 , temperature gradient = 0.03407735559720892 When P = 930.6822 , temperature gradient = 0.029817686147557237 When P = 929.6163 , temperature gradient = 0.03833702504685909 When P = 928.5503 , temperature gradient = 0.03833702504685909 When P = 928.0174 , temperature gradient = 0.029817686147557237 When P = 927.4844 , temperature gradient = 0.029817686147557237 When P = 926.4185 , temperature gradient = 0.03833702504685909 When P = 925.3526 , temperature gradient = 0.03407735559720892 When P = 924.8197 , temperature gradient = 0.021298347248255387 When P = 924.2867 , temperature gradient = 0.021298347248253875 When P = 923.7538 , temperature gradient = 0.029817686147557237 When P = 923.2208 , temperature gradient = 0.042596694496510774 When P = 922.1549 , temperature gradient = 0.04259669449650926 When P = 921.089 , temperature gradient = 0.025558016697905556 When P = 920.5561 , temperature gradient = 0.02555801669790707 When P = 920.0231 , temperature gradient = 0.059635372295114474 When P = 918.4242 , temperature gradient = 0.05963537229511296 When P = 917.3583 , temperature gradient = 0.02555801669790707 When P = 916.8254 , temperature gradient = 0.02555801669790707 When P = 916.2924 , temperature gradient = 0.029817686147555724 When P = 915.7595 , temperature gradient = 0.04259669449650926 When P = 914.6936 , temperature gradient = 0.04685636394616245 When P = 914.1606 , temperature gradient = 0.029817686147557237 When P = 913.6277 , temperature gradient = 0.034077355597207405 When P = 912.5624 , temperature gradient = 0.04685636394616094 When P = 911.5148 , temperature gradient = 0.059635372295114474 When P = 910.4672 , temperature gradient = 0.04685636394616245 When P = 909.9434 , temperature gradient = 0.025558016697905556 When P = 909.4196 , temperature gradient = 0.021298347248253875 When P = 908.8958 , temperature gradient = 0.042596694496510774 When P = 907.8482 , temperature gradient = 0.04685636394616245 When P = 907.3244 , temperature gradient = 0.029817686147557237 When P = 906.8006 , temperature gradient = 0.029817686147555724 When P = 906.2768 , temperature gradient = 0.029817686147557237 When P = 905.753 , temperature gradient = 0.05537570284546431 When P = 904.1816 , temperature gradient = 0.04685636394616094 When P = 903.6578 , temperature gradient = 0.025558016697905556 When P = 903.134 , temperature gradient = 0.029817686147557237 When P = 902.6102 , temperature gradient = 0.025558016697905556 When P = 902.0864 , temperature gradient = 0.055375702845462796 When P = 900.515 , temperature gradient = 0.051116033395812624 When P = 899.9912 , temperature gradient = 0.021298347248255387 When P = 899.4674 , temperature gradient = 0.021298347248255387 When P = 898.9436 , temperature gradient = 0.029817686147557237 When P = 897.896 , temperature gradient = 0.03833702504685909 When P = 897.3722 , temperature gradient = 0.029817686147557237 When P = 896.8484 , temperature gradient = 0.03407735559720892 When P = 895.8008 , temperature gradient = 0.034077355597207405 When P = 895.277 , temperature gradient = 0.025558016697905556 When P = 894.7532 , temperature gradient = 0.021298347248255387 When P = 894.2294 , temperature gradient = 0.017038677798603703 When P = 893.7056 , temperature gradient = 0.055375702845462796 When P = 891.6313 , temperature gradient = 0.059635372295114474 When P = 891.1165 , temperature gradient = 0.02555801669790707 When P = 890.6017 , temperature gradient = 0.021298347248255387 When P = 890.0869 , temperature gradient = 0.021298347248253875 When P = 889.5721 , temperature gradient = 0.025558016697905556 When P = 889.0572 , temperature gradient = 0.02555801669790707 When P = 888.5424 , temperature gradient = 0.029817686147557237 When P = 888.0276 , temperature gradient = 0.025558016697905556 When P = 887.5128 , temperature gradient = 0.021298347248255387 When P = 886.998 , temperature gradient = 0.02555801669790707 When P = 886.4832 , temperature gradient = 0.025558016697905556 When P = 885.9684 , temperature gradient = 0.021298347248253875 When P = 885.4536 , temperature gradient = 0.017038677798603703 When P = 884.9388 , temperature gradient = 0.04685636394616094 When P = 883.9092 , temperature gradient = 0.06389504174476617 When P = 882.8796 , temperature gradient = 0.03407735559720892 When P = 882.3648 , temperature gradient = 0.04259669449650926 When P = 881.3352 , temperature gradient = 0.042596694496510774 When P = 880.8204 , temperature gradient = 0.02555801669790707 When P = 880.3056 , temperature gradient = 0.05963537229511296 When P = 879.2759 , temperature gradient = 0.051116033395812624 When P = 878.7611 , temperature gradient = 0.021298347248255387 When P = 878.2463 , temperature gradient = 0.025558016697905556 When P = 877.7315 , temperature gradient = 0.021298347248255387 When P = 877.2167 , temperature gradient = 0.06815471119441632 When P = 875.6723 , temperature gradient = 0.08093371954336986 When P = 874.6427 , temperature gradient = 0.03833702504685909 When P = 874.1279 , temperature gradient = 0.034077355597207405 When P = 873.6131 , temperature gradient = 0.029817686147557237 When P = 873.0983 , temperature gradient = 0.021298347248255387 When P = 872.5922 , temperature gradient = 0.025558016697905556 When P = 872.0867 , temperature gradient = 0.055375702845462796 When P = 871.0758 , temperature gradient = 0.04685636394616094 When P = 870.5703 , temperature gradient = 0.021298347248255387 When P = 870.0648 , temperature gradient = 0.021298347248255387 When P = 869.5594 , temperature gradient = 0.025558016697905556 When P = 869.0539 , temperature gradient = 0.042596694496510774 When P = 868.043 , temperature gradient = 0.03833702504685909 When P = 867.5375 , temperature gradient = 0.04685636394616094 When P = 866.5265 , temperature gradient = 0.04685636394616245 When P = 866.0211 , temperature gradient = 0.021298347248255387 When P = 865.5156 , temperature gradient = 0.017038677798603703 When P = 865.0101 , temperature gradient = 0.03833702504685909 When P = 863.9992 , temperature gradient = 0.04685636394616094 When P = 863.4937 , temperature gradient = 0.029817686147557237 When P = 862.9882 , temperature gradient = 0.025558016697905556 When P = 862.4828 , temperature gradient = 0.025558016697905556 When P = 861.9773 , temperature gradient = 0.03833702504685909 When P = 860.9664 , temperature gradient = 0.051116033395812624 When P = 859.9554 , temperature gradient = 0.05537570284546431 When P = 858.9445 , temperature gradient = 0.059635372295114474 When P = 857.9335 , temperature gradient = 0.03833702504685909 When P = 857.428 , temperature gradient = 0.025558016697905556 When P = 856.9226 , temperature gradient = 0.04259669449650926 When P = 855.9116 , temperature gradient = 0.051116033395812624 When P = 854.9007 , temperature gradient = 0.042596694496510774 When P = 854.3952 , temperature gradient = 0.03833702504685909 When P = 853.3843 , temperature gradient = 0.029817686147557237 When P = 852.8895 , temperature gradient = 0.021298347248253875 When P = 852.3964 , temperature gradient = 0.025558016697905556 When P = 851.9033 , temperature gradient = 0.029817686147557237 When P = 850.9171 , temperature gradient = 0.029817686147557237 When P = 850.424 , temperature gradient = 0.021298347248255387 When P = 849.9309 , temperature gradient = 0.021298347248255387 When P = 849.4378 , temperature gradient = 0.02555801669790707 When P = 848.4516 , temperature gradient = 0.029817686147557237 When P = 847.4654 , temperature gradient = 0.029817686147555724 When P = 846.9723 , temperature gradient = 0.029817686147557237 When P = 845.9861 , temperature gradient = 0.04685636394616245 When P = 844.5068 , temperature gradient = 0.05111603339581111 When P = 843.5206 , temperature gradient = 0.034077355597207405 When P = 843.0275 , temperature gradient = 0.021298347248255387 When P = 842.5344 , temperature gradient = 0.021298347248255387 When P = 842.0413 , temperature gradient = 0.021298347248255387 When P = 841.5482 , temperature gradient = 0.017038677798603703 When P = 841.0551 , temperature gradient = 0.025558016697905556 When P = 840.562 , temperature gradient = 0.029817686147557237 When P = 840.069 , temperature gradient = 0.02555801669790707 When P = 839.5759 , temperature gradient = 0.06389504174476464 When P = 837.6035 , temperature gradient = 0.059635372295114474 When P = 837.1104 , temperature gradient = 0.017038677798605215 When P = 836.6173 , temperature gradient = 0.029817686147557237 When P = 835.6311 , temperature gradient = 0.034077355597207405 When P = 835.138 , temperature gradient = 0.021298347248253875 When P = 834.6449 , temperature gradient = 0.017038677798603703 When P = 834.1518 , temperature gradient = 0.021298347248255387 When P = 833.1656 , temperature gradient = 0.017038677798605215 When P = 832.6725 , temperature gradient = 0.008519338899301851 When P = 832.1794 , temperature gradient = 0.008519338899301851 When P = 831.6863 , temperature gradient = 0.008519338899301851 When P = 831.1932 , temperature gradient = 0.008519338899301851 When P = 830.207 , temperature gradient = 0.004259669449651683 When P = 829.7139 , temperature gradient = -0.0 When P = 828.7277 , temperature gradient = -0.0 When P = 828.241 , temperature gradient = -0.0 When P = 827.2696 , temperature gradient = -0.004259669449651683 When P = 826.784 , temperature gradient = -0.004259669449651683 When P = 826.2983 , temperature gradient = -0.0 When P = 825.8126 , temperature gradient = -0.0 When P = 825.3269 , temperature gradient = -0.0 When P = 824.8413 , temperature gradient = -0.004259669449650169 When P = 824.3556 , temperature gradient = -0.004259669449650169 When P = 823.8699 , temperature gradient = -0.0 When P = 823.3842 , temperature gradient = -0.0 When P = 822.4129 , temperature gradient = -0.0 When P = 821.9272 , temperature gradient = -0.0 When P = 821.4415 , temperature gradient = -0.0 When P = 820.9559 , temperature gradient = -0.0 When P = 819.9845 , temperature gradient = -0.0 When P = 819.0132 , temperature gradient = -0.0 When P = 818.5275 , temperature gradient = 0.004259669449650169 When P = 817.5561 , temperature gradient = 0.004259669449650169 When P = 817.0705 , temperature gradient = 0.004259669449651683 When P = 816.0991 , temperature gradient = 0.004259669449651683 When P = 815.6134 , temperature gradient = -0.0 When P = 815.1278 , temperature gradient = 0.004259669449650169 When P = 814.6421 , temperature gradient = 0.004259669449650169 When P = 814.1564 , temperature gradient = -0.0 When P = 812.6994 , temperature gradient = 0.004259669449651683 When P = 812.2137 , temperature gradient = 0.004259669449651683 When P = 811.7281 , temperature gradient = -0.0 When P = 811.2424 , temperature gradient = -0.0 When P = 810.7567 , temperature gradient = -0.0 When P = 810.271 , temperature gradient = -0.0 When P = 809.2997 , temperature gradient = -0.0 When P = 808.8165 , temperature gradient = -0.0 When P = 807.8641 , temperature gradient = -0.0 When P = 807.3879 , temperature gradient = -0.0 When P = 806.9117 , temperature gradient = -0.0 When P = 805.9594 , temperature gradient = -0.0 When P = 805.4832 , temperature gradient = -0.0 When P = 805.007 , temperature gradient = -0.0 When P = 804.5308 , temperature gradient = -0.0 When P = 804.0546 , temperature gradient = -0.0 When P = 803.5784 , temperature gradient = -0.0 When P = 803.1022 , temperature gradient = -0.0 When P = 802.626 , temperature gradient = -0.0 When P = 802.1498 , temperature gradient = 0.004259669449651683 When P = 801.1975 , temperature gradient = 0.004259669449651683 When P = 800.7213 , temperature gradient = 0.004259669449650169 When P = 799.7689 , temperature gradient = 0.004259669449650169 When P = 799.2927 , temperature gradient = 0.008519338899301851 When P = 798.3479 , temperature gradient = 0.008519338899301851 When P = 797.8766 , temperature gradient = 0.004259669449651683 When P = 797.4053 , temperature gradient = 0.008519338899301851 When P = 796.934 , temperature gradient = 0.008519338899301851 When P = 796.4628 , temperature gradient = 0.008519338899303366 When P = 795.9915 , temperature gradient = 0.008519338899301851 When P = 795.5202 , temperature gradient = 0.012779008348952021 When P = 795.0489 , temperature gradient = 0.021298347248255387 When P = 794.1063 , temperature gradient = 0.017038677798603703 When P = 793.635 , temperature gradient = 0.017038677798603703 When P = 792.6924 , temperature gradient = 0.042596694496510774 When P = 791.2786 , temperature gradient = 0.03833702504685909 When P = 790.8073 , temperature gradient = 0.021298347248255387 When P = 790.336 , temperature gradient = 0.01703867779860446 When P = 789.8647 , temperature gradient = 0.012779008348952778 When P = 789.3934 , temperature gradient = 0.017038677798603703 When P = 788.9251 , temperature gradient = 0.021298347248255387 When P = 788.4586 , temperature gradient = 0.046856363946161696 When P = 787.0592 , temperature gradient = 0.06815471119441632 When P = 785.6598 , temperature gradient = 0.04259669449651002 When P = 785.1933 , temperature gradient = 0.025558016697905556 When P = 784.7268 , temperature gradient = 0.029817686147557237 When P = 784.2604 , temperature gradient = 0.025558016697906312 When P = 783.7939 , temperature gradient = 0.025558016697905556 When P = 783.3274 , temperature gradient = 0.021298347248255387 When P = 782.8609 , temperature gradient = 0.046856363946161696 When P = 781.4615 , temperature gradient = 0.04685636394616094 When P = 780.9951 , temperature gradient = 0.04259669449651002 When P = 780.0621 , temperature gradient = 0.04685636394616094 When P = 779.5956 , temperature gradient = 0.02129834724825463 When P = 779.1312 , temperature gradient = 0.01703867779860446 When P = 778.6692 , temperature gradient = 0.025558016697906312 When P = 778.2073 , temperature gradient = 0.029817686147557237 When P = 777.7453 , temperature gradient = 0.021298347248255387 When P = 777.2833 , temperature gradient = 0.05111603339581187 When P = 775.8973 , temperature gradient = 0.05963537229511372 When P = 775.4353 , temperature gradient = 0.03833702504685909 When P = 774.5113 , temperature gradient = 0.042596694496510774 When P = 773.5873 , temperature gradient = 0.03407735559720816 When P = 773.1253 , temperature gradient = 0.04685636394616094 When P = 772.2014 , temperature gradient = 0.04259669449651002 When P = 771.7394 , temperature gradient = 0.017038677798603703 When P = 771.2774 , temperature gradient = 0.01703867779860446 When P = 770.8154 , temperature gradient = 0.038337025046859846 When P = 769.8914 , temperature gradient = 0.04685636394616094 When P = 769.4299 , temperature gradient = 0.02981768614755648 When P = 768.9726 , temperature gradient = 0.029817686147557237 When P = 768.5154 , temperature gradient = 0.03407735559720816 When P = 768.0581 , temperature gradient = 0.025558016697906312 When P = 767.6008 , temperature gradient = 0.021298347248255387 When P = 767.1436 , temperature gradient = 0.02981768614755648 When P = 766.6863 , temperature gradient = 0.05111603339581187 When P = 765.7718 , temperature gradient = 0.051116033395812624 When P = 765.3145 , temperature gradient = 0.03407735559720816 When P = 764.8572 , temperature gradient = 0.03833702504685909 When P = 764.4 , temperature gradient = 0.042596694496510774 When P = 763.4854 , temperature gradient = 0.04259669449651002 When P = 763.0282 , temperature gradient = 0.03833702504685909 When P = 762.5709 , temperature gradient = 0.03833702504685909 When P = 762.1136 , temperature gradient = 0.055375702845462796 When P = 761.1991 , temperature gradient = 0.05537570284546355 When P = 760.7419 , temperature gradient = 0.03407735559720816 When P = 760.2846 , temperature gradient = 0.025558016697906312 When P = 759.8273 , temperature gradient = 0.029817686147557237 When P = 759.3725 , temperature gradient = 0.04259669449651002 When P = 758.9199 , temperature gradient = 0.04259669449651002 When P = 758.4673 , temperature gradient = 0.03833702504685909 When P = 758.0148 , temperature gradient = 0.03833702504685909 When P = 757.5622 , temperature gradient = 0.029817686147557237 When P = 757.1096 , temperature gradient = 0.021298347248255387 When P = 756.657 , temperature gradient = 0.04259669449651002 When P = 755.7518 , temperature gradient = 0.04685636394616094 When P = 755.2992 , temperature gradient = 0.029817686147557237 When P = 754.8467 , temperature gradient = 0.051116033395812624 When P = 753.4889 , temperature gradient = 0.04685636394616094 When P = 753.0363 , temperature gradient = 0.025558016697905556
(b) Newton finite difference
In [3]:
Copied!
def Newt(x, y, n, x_miss):
fdd = np.zeros((n, n))
fdd[0:n, 0] = y[0:n]
for j in range(1, n, 1):
for i in range(0, n-j, 1):
fdd[i, j] = (fdd[i+1, j-1] - fdd[i, j-1]) / (x[i+j] - x[i])
y_interp = y[0]
xterm = 1.0
for order in range(1, n):
xterm = xterm * (x_miss - x[order - 1])
y_interp = y_interp + fdd[0, order] * xterm
return y_interp
T_975 = Newt(P, T, 1, 975.0)
print('At the level of 975hpa, the temperature =', T_975, '(Newton)')
def Newt(x, y, n, x_miss):
fdd = np.zeros((n, n))
fdd[0:n, 0] = y[0:n]
for j in range(1, n, 1):
for i in range(0, n-j, 1):
fdd[i, j] = (fdd[i+1, j-1] - fdd[i, j-1]) / (x[i+j] - x[i])
y_interp = y[0]
xterm = 1.0
for order in range(1, n):
xterm = xterm * (x_miss - x[order - 1])
y_interp = y_interp + fdd[0, order] * xterm
return y_interp
T_975 = Newt(P, T, 1, 975.0)
print('At the level of 975hpa, the temperature =', T_975, '(Newton)')
At the level of 975hpa, the temperature = 19.56 (Newton)
(c)
In [4]:
Copied!
from scipy.interpolate import CubicSpline
P_inv = []
T_inv = []
#將P從小排到大
for i in range(0, len(P)):
P_inv.append(P[len(P) - i - 1])
T_inv.append(T[len(P) - i - 1])
cs = CubicSpline(P_inv, T_inv) #cubic spline
xs = np.arange(750, 1000, 0.1)
fig, ax = plt.subplots(figsize = (6.5, 4))
plt.title('Cubic Spline')
ax.plot(T, P, 'o', label = 'data')
ax.plot(cs(xs), xs, label = "S")
plt.gca().invert_yaxis()
ax.legend(loc = 'lower left', ncol = 2)
plt.show()
print('At the level of 975hpa, the temperature =', cs(975.0), '(Cubic)')
from scipy.interpolate import CubicSpline
P_inv = []
T_inv = []
#將P從小排到大
for i in range(0, len(P)):
P_inv.append(P[len(P) - i - 1])
T_inv.append(T[len(P) - i - 1])
cs = CubicSpline(P_inv, T_inv) #cubic spline
xs = np.arange(750, 1000, 0.1)
fig, ax = plt.subplots(figsize = (6.5, 4))
plt.title('Cubic Spline')
ax.plot(T, P, 'o', label = 'data')
ax.plot(cs(xs), xs, label = "S")
plt.gca().invert_yaxis()
ax.legend(loc = 'lower left', ncol = 2)
plt.show()
print('At the level of 975hpa, the temperature =', cs(975.0), '(Cubic)')
At the level of 975hpa, the temperature = 21.261616119949423 (Cubic)
(d) 拉格朗日
In [5]:
Copied!
def Lagrage(x, y, f, n, x_miss):
sum = 0.0
for i in range(f, n):
product = 1
for j in range(f, n):
if i != j:
product = product * (x_miss - x[j]) / (x[i] - x[j])
sum = sum + product * y[i]
return sum
for cou in range(11, len(P)-10):
forw = Lagrage(P, T, cou-2, cou+9, (float(P[cou-2])+float(P[cou+10]))/2)
midd = Lagrage(P, T, cou-5, cou+6, (float(P[cou-5])+float(P[cou+5]))/2)
back = Lagrage(P, T, cou-8, cou+3, (float(P[cou-8])+float(P[cou+2]))/2)
if max(forw, midd, back) == midd or min(forw, midd, back) == midd:
print('The depth of inverse:', (float(P[cou-5])+float(P[cou+5]))/2)
#anssss = Lagrage(P, T, 40, 46, 975.0)
#print('test2',anssss)
def Lagrage(x, y, f, n, x_miss):
sum = 0.0
for i in range(f, n):
product = 1
for j in range(f, n):
if i != j:
product = product * (x_miss - x[j]) / (x[i] - x[j])
sum = sum + product * y[i]
return sum
for cou in range(11, len(P)-10):
forw = Lagrage(P, T, cou-2, cou+9, (float(P[cou-2])+float(P[cou+10]))/2)
midd = Lagrage(P, T, cou-5, cou+6, (float(P[cou-5])+float(P[cou+5]))/2)
back = Lagrage(P, T, cou-8, cou+3, (float(P[cou-8])+float(P[cou+2]))/2)
if max(forw, midd, back) == midd or min(forw, midd, back) == midd:
print('The depth of inverse:', (float(P[cou-5])+float(P[cou+5]))/2)
#anssss = Lagrage(P, T, 40, 46, 975.0)
#print('test2',anssss)
The depth of inverse: 993.6958999999999 The depth of inverse: 992.83705 The depth of inverse: 992.26265 The depth of inverse: 991.3994 The depth of inverse: 954.52215 The depth of inverse: 953.6986999999999 The depth of inverse: 952.8752999999999 The depth of inverse: 952.3272 The depth of inverse: 950.9594 The depth of inverse: 828.996 The depth of inverse: 828.0172500000001 The depth of inverse: 824.11275 The depth of inverse: 822.89855 The depth of inverse: 822.17005 The depth of inverse: 821.6844000000001 The depth of inverse: 820.4702 The depth of inverse: 819.01315 The depth of inverse: 815.6134500000001 The depth of inverse: 813.18505 The depth of inverse: 811.97215 The depth of inverse: 810.77215 The depth of inverse: 809.08655 The depth of inverse: 808.1247 The depth of inverse: 807.64375 The depth of inverse: 807.1628000000001 The depth of inverse: 806.43905 The depth of inverse: 805.24505 The depth of inverse: 803.34035 The depth of inverse: 802.14985
Last update:
2024-04-27