In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use Python to do scientific computing", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Python to do scientific computing" this article.
Its core requirement is to do a function fitting, but the fitted function is an integral expression. The easiest way to do this is to use the functions in the scipy library. Here is the source code.
#-*-coding: utf-8-*-from scipy import integratefrom scipy import optimizefrom matplotlib import pyplotimport numpyimport pandasimport timeI = complex (0,1) def tmp (E, params): global I Z = params [1] delta = params [2] Gamma = params [3] complex_num = 0.5 + numpy.sqrt (pow (E+I*Gamma) 2)-delta*delta) / (2* (E+I*Gamma)) alpha = complex_num.real eta = complex_num.imag beta = 1-alpha gamma = numpy.sqrt (numpy.power (alpha+Z*Z* (alpha-beta), 2) + numpy.power (eta* (2*Z*Z+1), 2) return alpha, beta, gamma, etadef factor (E, params): P = params [0] Z = params [1] alpha, beta, gamma Eta = tmp (E, params) numerator1 = numpy.sqrt ((alpha*alpha+eta*eta) * (beta*beta+eta*eta)) denominator1 = gamma*gamma AE = numerator1/denominator1 numerator2 = ZambiZ* (numpy.power ((alpha-beta) * Z-2*eta, 2) + numpy.power (2roometailed Z+ (alpha-beta), 2) denominator2 = gamma*gamma BE = numerator2/denominator2 return 1 + (1Mup) * AE-BEdef dfdV_mod (E) V): variable = E-V if (variable > = 0): exp = numpy.exp (- variable) else: exp = numpy.exp (variable) numerator =-exp denominator = numpy.power (exp+1, 2) return numerator/denominator# Integrand def integrand (E, V, params): return dfdV_mod (E, V) * factor (E, params) # integral def integral (V, params): result = integrate.quad (integrand,-numpy.inf) Numpy.inf, args= (V, params) return result [0] # calculate the integral def integral_all (V, params): result = numpy.zeros (V.size) for i in range (0, V.size): res = integrate.quad (integrand,-numpy.inf, numpy.inf, args= (V [I], params)) result [I] = res [0] return result# deviation def residual (params) G, V): res = numpy.zeros (g.size) for i in range (0, g.size): res [I] = g [I]-integral (V [I], params) return res# read the experimental data into The header def ReadData (path) should be added to the experimental data: dataframe = pandas.read_excel (path, sheet_name=0) x = numpy.array (dataframe.iloc [:, 0]) y = numpy.array (dataframe.iloc [:, 1]) return y Xif _ name__ = ='_ main__': start = time.time () # initial value P = 0.5Z = 1 delta = 0 Gamma = 0 # read data g, V = ReadData ("data.xlsx") # least square fitting params0 = numpy.array ([P, Z, delta, Gamma]) result = optimize.least_squares (residual, params0, bounds= ([- 1,-5,-1) -1], [1,5,1,1]), args= (g, V)) print ("result:" + str (result)) # draw the result params = result.x V_test = numpy.linspace (V.min (), V.max (), 100) g_test = integral_all (V_test, params) pyplot.plot (V, g, 'oasis, markersize=1, label='data') pyplot.plot (V_test, g_test) Label='fitted curve') pyplot.xlabel ('V') pyplot.ylabel ('g') pyplot.show () end = time.time () print ("time elapsed:" + str (end-start) + "s")
From the code point of view is still very clear, mainly used two functions, one is integrate.quad, used to calculate the integral, the other is optimize.least_squares, using the least square method to give the function parameter values.
The above is all the contents of the article "how to use Python to do Scientific Computing". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.