fortran数值计算基础
- 格式:docx
- 大小:558.62 KB
- 文档页数:46
fortran教程Fortran是一种编程语言,用于科学和工程计算。
它具有高性能和可靠性,适用于大规模的数值计算和数据处理。
Fortran的基本语法是由一系列语句组成的程序。
每个语句都以一个数字标签开头,可用于控制程序的执行顺序。
以下是Fortran的一些基本语法和常用功能。
1. 变量和数据类型Fortran支持多种数据类型,包括整数(Integer)、实数(Real)和字符(Character)。
变量声明可以在程序的开头部分完成,例如:```fortranINTEGER :: iREAL :: xCHARACTER(len=10) :: name```注意,每个变量声明必须以两个冒号(::)开头。
2. 数学运算Fortran提供了一系列的数学运算符,用于执行基本的算术和逻辑运算,例如加法(+)、减法(-)、乘法(*)、除法(/)和求余(MOD)。
运算符的使用与其他编程语言相似。
3. 控制结构Fortran支持常用的控制结构,例如条件语句(IF-ELSE)和循环语句(DO LOOP)。
条件语句可以根据条件来执行不同的代码块,例如:```fortranIF (i > 0) THENPRINT*, 'i is positive'ELSEPRINT*, 'i is non-positive'END IF```循环语句可以根据条件重复执行一部分代码,例如:```fortranDO i = 1, 10PRINT*, iEND DO```上述代码将打印出1到10的数字。
4. 数组和矩阵运算Fortran支持多维数组和矩阵运算。
声明数组时可以指定维度和元素类型,例如:```fortranREAL, DIMENSION(3,3) :: matrix```然后,可以使用多个索引访问数组元素,例如:```fortranmatrix(1,2) = 3.14```这将给数组中的第1行第2列的元素赋值为3.14。
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).Computer Programs by Chapter and Section1.0flmoon calculate phases of the moon by date1.1julday Julian Day number from calendar date1.1badluk Friday the13th when the moon is full1.1caldat calendar date from Julian day number2.1gaussj Gauss-Jordan matrix inversion and linear equation solution2.3ludcmp linear equation solution,LU decomposition2.3lubksb linear equation solution,backsubstitution2.4tridag solution of tridiagonal systems2.4banmul multiply vector by band diagonal matrix2.4bandec band diagonal systems,decomposition2.4banbks band diagonal systems,backsubstitution2.5mprove linear equation solution,iterative improvement2.6svbksb singular value backsubstitution2.6svdcmp singular value decomposition of a matrix2.6pythag calculate(a2+b2)1/2without overflow2.7cyclic solution of cyclic tridiagonal systems2.7sprsin convert matrix to sparse format2.7sprsax product of sparse matrix and vector2.7sprstx product of transpose sparse matrix and vector2.7sprstp transpose of sparse matrix2.7sprspm pattern multiply two sparse matrices2.7sprstm threshold multiply two sparse matrices2.7linbcg biconjugate gradient solution of sparse systems2.7snrm used by linbcg for vector norm2.7atimes used by linbcg for sparse multiplication2.7asolve used by linbcg for preconditioner2.8vander solve Vandermonde systems2.8toeplz solve Toeplitz systems2.9choldc Cholesky decomposition2.9cholsl Cholesky backsubstitution2.10qrdcmp QR decomposition2.10qrsolv QR backsubstitution2.10rsolv right triangular backsubstitution2.10qrupdt update a QR decomposition2.10rotate Jacobi rotation used by qrupdt 3.1polint polynomial interpolation3.2ratint rational function interpolation3.3spline construct a cubic spline3.3splint cubic spline interpolation3.4locate search an ordered table by bisectionxxivComputer Programs by Chapter and Section xxv Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).3.4hunt search a table when calls are correlated3.5polcoe polynomial coefficients from table of values3.5polcof polynomial coefficients from table of values3.6polin2two-dimensional polynomial interpolation3.6bcucof construct two-dimensional bicubic3.6bcuint two-dimensional bicubic interpolation3.6splie2construct two-dimensional spline3.6splin2two-dimensional spline interpolation4.2trapzd trapezoidal rule4.2qtrap integrate using trapezoidal rule4.2qsimp integrate using Simpson’s rule4.3qromb integrate using Romberg adaptive method4.4midpnt extended midpoint rule4.4qromo integrate using open Romberg adaptive method4.4midinf integrate a function on a semi-infinite interval4.4midsql integrate a function with lower square-root singularity4.4midsqu integrate a function with upper square-root singularity4.4midexp integrate a function that decreases exponentially4.5qgaus integrate a function by Gaussian quadratures4.5gauleg Gauss-Legendre weights and abscissas4.5gaulag Gauss-Laguerre weights and abscissas4.5gauher Gauss-Hermite weights and abscissas4.5gaujac Gauss-Jacobi weights and abscissas4.5gaucof quadrature weights from orthogonal polynomials4.5orthog construct nonclassical orthogonal polynomials4.6quad3d integrate a function over a three-dimensional space5.1eulsum sum a series by Euler–van Wijngaarden algorithm5.3ddpoly evaluate a polynomial and its derivatives5.3poldiv divide one polynomial by another5.3ratval evaluate a rational function5.7dfridr numerical derivative by Ridders’method5.8chebftfit a Chebyshev polynomial to a function5.8chebev Chebyshev polynomial evaluation5.9chder derivative of a function already Chebyshevfitted5.9chint integrate a function already Chebyshevfitted5.10chebpc polynomial coefficients from a Chebyshevfit5.10pcshft polynomial coefficients of a shifted polynomial5.11pccheb inverse of chebpc;use to economize power series5.12pade Pad´e approximant from power series coefficients5.13ratlsq rationalfit by least-squares method6.1gammln logarithm of gamma function6.1factrl factorial function6.1bico binomial coefficients function6.1factln logarithm of factorial functionxxvi Computer Programs by Chapter and Section Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).6.1beta beta function6.2gammp incomplete gamma function6.2gammq complement of incomplete gamma function6.2gser series used by gammp and gammq6.2gcf continued fraction used by gammp and gammq6.2erf error function6.2erfc complementary error function6.2erfcc complementary error function,concise routine6.3expint exponential integral E n6.3ei exponential integral Ei6.4betai incomplete beta function6.4betacf continued fraction used by betai6.5bessj0Bessel function J06.5bessy0Bessel function Y06.5bessj1Bessel function J16.5bessy1Bessel function Y16.5bessy Bessel function Y of general integer order6.5bessj Bessel function J of general integer order6.6bessi0modified Bessel function I06.6bessk0modified Bessel function K06.6bessi1modified Bessel function I16.6bessk1modified Bessel function K16.6bessk modified Bessel function K of integer order6.6bessi modified Bessel function I of integer order6.7bessjy Bessel functions of fractional order6.7beschb Chebyshev expansion used by bessjy6.7bessik modified Bessel functions of fractional order6.7airy Airy functions6.7sphbes spherical Bessel functions j n and y n6.8plgndr Legendre polynomials,associated(spherical harmonics)6.9frenel Fresnel integrals S(x)and C(x)6.9cisi cosine and sine integrals Ci and Si6.10dawson Dawson’s integral6.11rf Carlson’s elliptic integral of thefirst kind6.11rd Carlson’s elliptic integral of the second kind6.11rj Carlson’s elliptic integral of the third kind6.11rc Carlson’s degenerate elliptic integral6.11ellf Legendre elliptic integral of thefirst kind6.11elle Legendre elliptic integral of the second kind6.11ellpi Legendre elliptic integral of the third kind6.11sncndn Jacobian elliptic functions6.12hypgeo complex hypergeometric function6.12hypser complex hypergeometric function,series evaluation6.12hypdrv complex hypergeometric function,derivative of7.1ran0random deviate by Park and Miller minimal standard 7.1ran1random deviate,minimal standard plus shuffleComputer Programs by Chapter and Section xxvii Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).7.1ran2random deviate by L’Ecuyer long period plus shuffle7.1ran3random deviate by Knuth subtractive method7.2expdev exponential random deviates7.2gasdev normally distributed random deviates7.3gamdev gamma-law distribution random deviates7.3poidev Poisson distributed random deviates7.3bnldev binomial distributed random deviates7.4irbit1random bit sequence7.4irbit2random bit sequence7.5psdes“pseudo-DES”hashing of64bits7.5ran4random deviates from DES-like hashing7.7sobseq Sobol’s quasi-random sequence7.8vegas adaptive multidimensional Monte Carlo integration7.8rebin sample rebinning used by vegas7.8miser recursive multidimensional Monte Carlo integration7.8ranpt get random point,used by miser8.1piksrt sort an array by straight insertion8.1piksr2sort two arrays by straight insertion8.1shell sort an array by Shell’s method8.2sort sort an array by quicksort method8.2sort2sort two arrays by quicksort method8.3hpsort sort an array by heapsort method8.4indexx construct an index for an array8.4sort3sort,use an index to sort3or more arrays8.4rank construct a rank table for an array8.5selectfind the N th largest in an array8.5selipfind the N th largest,without altering an array8.5hpselfind M largest values,without altering an array8.6eclass determine equivalence classes from list8.6eclazz determine equivalence classes from procedure9.0scrsho graph a function to search for roots9.1zbrac outward search for brackets on roots9.1zbrak inward search for brackets on roots9.1rtbisfind root of a function by bisection9.2rtflspfind root of a function by false-position9.2rtsecfind root of a function by secant method9.2zriddrfind root of a function by Ridders’method9.3zbrentfind root of a function by Brent’s method9.4rtnewtfind root of a function by Newton-Raphson9.4rtsafefind root of a function by Newton-Raphson and bisection 9.5laguerfind a root of a polynomial by Laguerre’s method9.5zroots roots of a polynomial by Laguerre’s method withdeflation9.5zrhqr roots of a polynomial by eigenvalue methods9.5qroot complex or double root of a polynomial,Bairstowxxviii Computer Programs by Chapter and Section Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).9.6mnewt Newton’s method for systems of equations9.7lnsrch search along a line,used by newt9.7newt globally convergent multi-dimensional Newton’s method9.7fdjacfinite-difference Jacobian,used by newt9.7fmin norm of a vector function,used by newt9.7broydn secant method for systems of equations10.1mnbrak bracket the minimum of a function10.1goldenfind minimum of a function by golden section search10.2brentfind minimum of a function by Brent’s method10.3dbrentfind minimum of a function using derivative information10.4amoeba minimize in N-dimensions by downhill simplex method10.4amotry evaluate a trial point,used by amoeba10.5powell minimize in N-dimensions by Powell’s method10.5linmin minimum of a function along a ray in N-dimensions10.5f1dim function used by linmin10.6frprmn minimize in N-dimensions by conjugate gradient10.6df1dim alternative function used by linmin10.7dfpmin minimize in N-dimensions by variable metric method10.8simplx linear programming maximization of a linear function10.8simp1linear programming,used by simplx10.8simp2linear programming,used by simplx10.8simp3linear programming,used by simplx10.9anneal traveling salesman problem by simulated annealing10.9revcst cost of a reversal,used by anneal10.9revers do a reversal,used by anneal10.9trncst cost of a transposition,used by anneal10.9trnspt do a transposition,used by anneal10.9metrop Metropolis algorithm,used by anneal10.9amebsa simulated annealing in continuous spaces10.9amotsa evaluate a trial point,used by amebsa11.1jacobi eigenvalues and eigenvectors of a symmetric matrix11.1eigsrt eigenvectors,sorts into order by eigenvalue11.2tred2Householder reduction of a real,symmetric matrix11.3tqli eigensolution of a symmetric tridiagonal matrix11.5balanc balance a nonsymmetric matrix11.5elmhes reduce a general matrix to Hessenberg form11.6hqr eigenvalues of a Hessenberg matrix12.2four1fast Fourier transform(FFT)in one dimension12.3twofft fast Fourier transform of two real functions 12.3realft fast Fourier transform of a single real function 12.3sinft fast sine transform12.3cosft1fast cosine transform with endpoints12.3cosft2“staggered”fast cosine transform12.4fourn fast Fourier transform in multidimensionsComputer Programs by Chapter and Section xxix Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).12.5rlft3FFT of real data in two or three dimensions12.6fourfs FFT for huge data sets on external media12.6fourew rewind and permutefiles,used by fourfs13.1convlv convolution or deconvolution of data using FFT13.2correl correlation or autocorrelation of data using FFT13.4spctrm power spectrum estimation using FFT13.6memcof evaluate maximum entropy(MEM)coefficients13.6fixrts reflect roots of a polynomial into unit circle13.6predic linear prediction using MEM coefficients13.7evlmem power spectral estimation from MEM coefficients13.8period power spectrum of unevenly sampled data13.8fasper power spectrum of unevenly sampled larger data sets13.8spread extirpolate value into array,used by fasper13.9dftcor compute endpoint corrections for Fourier integrals13.9dftint high-accuracy Fourier integrals13.10wt1one-dimensional discrete wavelet transform13.10daub4Daubechies4-coefficient waveletfilter13.10pwtset initialize coefficients for pwt13.10pwt partial wavelet transform13.10wtn multidimensional discrete wavelet transform14.1moment calculate moments of a data set14.2ttest Student’s t-test for difference of means14.2avevar calculate mean and variance of a data set14.2tutest Student’s t-test for means,case of unequal variances14.2tptest Student’s t-test for means,case of paired data14.2ftest F-test for difference of variances14.3chsone chi-square test for difference between data and model14.3chstwo chi-square test for difference between two data sets14.3ksone Kolmogorov-Smirnov test of data against model14.3kstwo Kolmogorov-Smirnov test between two data sets14.3probks Kolmogorov-Smirnov probability function14.4cntab1contingency table analysis using chi-square14.4cntab2contingency table analysis using entropy measure14.5pearsn Pearson’s correlation between two data sets14.6spear Spearman’s rank correlation between two data sets14.6crank replaces array elements by their rank14.6kendl1correlation between two data sets,Kendall’s tau14.6kendl2contingency table analysis using Kendall’s tau14.7ks2d1s K–S test in two dimensions,data vs.model14.7quadct count points by quadrants,used by ks2d1s 14.7quadvl quadrant probabilities,used by ks2d1s14.7ks2d2s K–S test in two dimensions,data vs.data14.8savgol Savitzky-Golay smoothing coefficients15.2fit least-squaresfit data to a straight linexxx Computer Programs by Chapter and Section Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).15.3fitexyfit data to a straight line,errors in both x and y15.3chixy used by fitexy to calculate aχ215.4lfit general linear least-squaresfit by normal equations15.4covsrt rearrange covariance matrix,used by lfit15.4svdfit linear least-squaresfit by singular value decomposition15.4svdvar variances from singular value decomposition15.4fpolyfit a polynomial using lfit or svdfit15.4flegfit a Legendre polynomial using lfit or svdfit15.5mrqmin nonlinear least-squaresfit,Marquardt’s method15.5mrqcof used by mrqmin to evaluate coefficients15.5fgaussfit a sum of Gaussians using mrqmin15.7medfitfit data to a straight line robustly,least absolute deviation15.7rofuncfit data robustly,used by medfit16.1rk4integrate one step of ODEs,fourth-order Runge-Kutta16.1rkdumb integrate ODEs by fourth-order Runge-Kutta16.2rkqs integrate one step of ODEs with accuracy monitoring16.2rkck Cash-Karp-Runge-Kutta step used by rkqs16.2odeint integrate ODEs with accuracy monitoring16.3mmid integrate ODEs by modified midpoint method16.4bsstep integrate ODEs,Bulirsch-Stoer step16.4pzextr polynomial extrapolation,used by bsstep16.4rzextr rational function extrapolation,used by bsstep16.5stoerm integrate conservative second-order ODEs16.6stiff integrate stiff ODEs by fourth-order Rosenbrock16.6jacobn sample Jacobian routine for stiff16.6derivs sample derivatives routine for stiff16.6simpr integrate stiff ODEs by semi-implicit midpoint rule16.6stifbs integrate stiff ODEs,Bulirsch-Stoer step17.1shoot solve two point boundary value problem by shooting17.2shootf ditto,by shooting to afitting point17.3solvde two point boundary value problem,solve by relaxation17.3bksub backsubstitution,used by solvde17.3pinvs diagonalize a sub-block,used by solvde17.3red reduce columns of a matrix,used by solvde17.4sfroid spheroidal functions by method of solvde17.4difeq spheroidal matrix coefficients,used by sfroid17.4sphoot spheroidal functions by method of shoot17.4sphfpt spheroidal functions by method of shootf18.1fred2solve linear Fredholm equations of the second kind 18.1fredin interpolate solutions obtained with fred218.2voltra linear V olterra equations of the second kind18.3wwghts quadrature weights for an arbitrarily singular kernel 18.3kermom sample routine for moments of a singular kernel 18.3quadmx sample routine for a quadrature matrixComputer Programs by Chapter and Section xxxi Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).18.3fredex example of solving a singular Fredholm equation19.5sor elliptic PDE solved by successive overrelaxation method19.6mglin linear elliptic PDE solved by multigrid method19.6rstrct half-weighting restriction,used by mglin,mgfas19.6interp bilinear prolongation,used by mglin,mgfas19.6addint interpolate and add,used by mglin19.6slvsml solve on coarsest grid,used by mglin19.6relax Gauss-Seidel relaxation,used by mglin19.6resid calculate residual,used by mglin19.6copy utility used by mglin,mgfas19.6fill0utility used by mglin19.6maloc memory allocation utility used by mglin,mgfas19.6mgfas nonlinear elliptic PDE solved by multigrid method19.6relax2Gauss-Seidel relaxation,used by mgfas19.6slvsm2solve on coarsest grid,used by mgfas19.6lop applies nonlinear operator,used by mgfas19.6matadd utility used by mgfas19.6matsub utility used by mgfas19.6anorm2utility used by mgfas20.1machar diagnose computer’sfloating arithmetic20.2igray Gray code and its inverse20.3icrc1cyclic redundancy checksum,used by icrc20.3icrc cyclic redundancy checksum20.3decchk decimal check digit calculation or verification20.4hufmak construct a Huffman code20.4hufapp append bits to a Huffman code,used by hufmak20.4hufenc use Huffman code to encode and compress a character20.4hufdec use Huffman code to decode and decompress a character20.5arcmak construct an arithmetic code20.5arcode encode or decode a character using arithmetic coding20.5arcsum add integer to byte string,used by arcode20.6mpops multiple precision arithmetic,simpler operations20.6mpmul multiple precision multiply,using FFT methods20.6mpinv multiple precision reciprocal20.6mpdiv multiple precision divide and remainder20.6mpsqrt multiple precision square root20.6mp2dfr multiple precision conversion to decimal base20.6mppi multiple precision example,compute many digits ofπ。
Fortran科学计算编程课件Fortran(即Formula Translation的缩写)是一种用于科学计算和数值分析的编程语言。
由于其高效性和可靠性,Fortran在科学计算领域广泛应用。
本课件将为您介绍Fortran编程语言,并提供相关的科学计算示例和实践。
第一部分:Fortran简介Fortran是一种面向过程的编程语言,由IBM在1950年代开发,旨在为科学计算提供高性能和可移植性。
Fortran以其强大的数学计算和数组处理能力闻名,是科学家和工程师们首选的编程语言之一。
第二部分:Fortran基本语法2.1 变量和数据类型在Fortran中,变量的类型必须在声明时指定,并且不同类型的变量可以存储不同类型的数据,如整数、实数和字符等。
这一部分将详细介绍Fortran的常用数据类型和变量声明方法。
2.2 控制结构Fortran支持常见的控制结构,例如条件控制语句(if-else语句)、循环语句(do循环)和跳转语句(goto语句)。
我们将逐一介绍这些结构的语法和使用方法,并给出实例演示。
第三部分:科学计算实例为了帮助您更好地理解Fortran的应用,本课件将提供一些科学计算的实例,包括线性代数运算、梯度下降算法等。
通过这些实例,您将学会如何用Fortran编写科学计算程序,并了解其在实践中的应用。
第四部分:Fortran编程工具和资源4.1 编译器Fortran编程需要一个符合语言标准的编译器来将代码转化为可执行文件。
我们将向您介绍几个常用的Fortran编译器,并给出安装和配置的指南。
4.2 开发环境为了提高编程效率,许多IDE(集成开发环境)和编辑器提供了对Fortran的支持。
我们将为您介绍几个常用的Fortran开发环境,并给出使用方法和建议。
4.3 学习资源如果您想深入学习Fortran,我们还准备了一些在线教程和学习资源的链接,方便您进一步扩展知识和技能。
结语通过本课件的学习,您将对Fortran科学计算编程有更深入的了解,并掌握基本的语法和应用技巧。
fortran科学计数法
Fortran是一种高级编程语言,用于科学计算和数据处理。
它含有许多内置函数和特性,其中之一就是科学计数法。
科学计数法是一种表示非常大或非常小的数字的方法,可以用于表示实数。
它由两部分组成:基数和指数。
基数是一个小于10的正实数,指数是一个整数。
在Fortran中,科学计数法可以用E来表示指数,例如:
1.23E+06 表示1.23乘以10的6次方
2.34E-05 表示2.34乘以10的负5次方
在Fortran中,科学计数法可以用于输入、输出和计算。
例如,下面的代码将输出1.23E+06:
write(*,*) 1234567.89
可以使用科学计数法进行算术运算,例如:
a = 1.23E+06 + 2.34E-05
Fortran还提供了一些内置函数,用于将数字从一种格式转换为另一种格式。
例如,REAL函数可以将整数或字符型数据转换为实数。
例如:
a = real(1234) 将整数1234转换为实数
b = real('1.23E+06') 将字符串'1.23E+06'转换为实数
1.23E+06
总之,Fortran的科学计数法功能使其成为一种非常方便的语言,适用于处理大量数值计算和数据处理任务。
fortran语言程序设计知识点Fortran语言是一种面向科学与工程计算的编程语言,因其在数值计算和科学应用方面的高效性而受到广泛使用。
本文将重点介绍Fortran语言程序设计的一些重要知识点。
一、基础语法1.1 变量与数据类型在Fortran中,变量是用于存储数据的容器。
常见的数据类型包括整型(INTEGER)、实型(REAL/DOUBLE PRECISION)、复数型(COMPLEX)、逻辑型(LOGICAL)等。
变量的命名需遵循一定规则,如以字母开头,长度不超过31个字符等。
1.2 运算符与表达式Fortran支持常见的算术运算符(如+、-、*、/)以及逻辑运算符(如. AND.、. OR.、. NOT.)等。
表达式由变量、常数和运算符组成,可以进行数值运算和逻辑判断。
1.3 控制结构Fortran提供循环结构(DO)、条件结构(IF-THEN-ELSE)和选择结构(SELECT CASE)等控制语句,用于控制程序的流程。
二、数组与数据处理2.1 数组定义与操作Fortran中数组是由相同类型的数据元素组成的集合。
可以使用DIMENSION语句定义数组的维度和大小,通过下标访问和修改数组元素。
2.2 数组运算与函数Fortran提供了许多对数组进行运算和处理的函数,如数组求和(SUM)、最大最小值(MAX、MIN)、数组重排(RESHAPE)等,方便对数据进行统计和处理。
2.3 文件读写操作Fortran支持对文件进行读写操作,可以通过OPEN语句打开文件,使用READ和WRITE语句读取和写入数据。
可以根据需要设置文件的访问模式和格式。
三、过程与模块3.1 过程(Subroutine/Function)过程是一段独立的代码块,可接受传入参数并返回值。
在Fortran中,过程可以是子程序(Subroutine)或函数(Function)。
子程序用于完成一系列操作,而函数则返回一个值。
3.2 模块(Module)Fortran中的模块是一个编译单元,用于组织和管理相关的过程和变量。
一阶常微分方程初值问题的数值解是微分方程数值求解中的基础问题,对于工程、物理、生物等领域的科学计算和数值模拟具有重要意义。
本文将从常微分方程初值问题的数值解的基本原理和数值方法入手,详细介绍使用Fortran语言进行一阶常微分方程初值问题的数值解的实现过程和注意事项。
1. 常微分方程初值问题的数值解基本原理常微分方程初值问题的数值解是通过数值方法来逼近微分方程的解。
对于一阶常微分方程初值问题:dy/dx = f(x, y)y(x0) = y0其中,f(x, y)是给定的函数,y(x0) = y0是给定的初值条件。
求解该初值问题即是要找到一个函数y(x)近似地满足该微分方程,并且在点x = x0处与给定的初值条件相符。
2. 常微分方程初值问题的数值解的数值方法常见的数值方法包括欧拉方法、改进的欧拉方法、四阶龙格-库塔方法等。
其中,四阶龙格-库塔方法是最常用和最经典的数值方法之一。
该方法通过取若干个函数值点上的斜率的加权平均值来逼近微分方程的解,具有较高的数值精度和稳定性。
3. 使用Fortran语言实现一阶常微分方程初值问题的数值解Fortran是一种古老但经典的科学计算语言,以其高效的数值计算和科学工程计算而闻名。
下面将结合Fortran语言的特点,介绍如何使用Fortran语言实现一阶常微分方程初值问题的数值解。
(1)定义常微分方程的函数f(x, y)在Fortran程序中,首先需要定义常微分方程的函数f(x, y),并将其定义为一个子程序或函数。
这里以一个简单的一阶线性常微分方程为例:f(x, y) = x + y则在Fortran程序中可以这样定义:```function f(x, y)real :: x, y, ff = x + yend function f```(2)实现四阶龙格-库塔方法在Fortran程序中,可以实现四阶龙格-库塔方法来数值解常微分方程初值问题。
具体做法是按照龙格-库塔方法的算法,在程序中编写相应的代码实现。
fortran除法Fortran是一种程序设计语言,它在科学计算领域有着广泛的应用。
本文将介绍Fortran除法操作的相关知识,包括除法运算符、数据类型、除数为零的处理以及实际应用案例。
一、Fortran除法运算符Fortran中用于除法运算的运算符是“/”。
它可以用于两个数值变量之间的相除。
例如,若要将变量A除以变量B并将结果存储在变量C中,可以使用以下语句:C = A / B在Fortran中,除法运算符可以应用于整数类型和实数类型的变量。
对于整数类型,除法运算符将执行整数除法,即结果将被截断为整数。
而对于实数类型,除法运算符将执行浮点数除法,即结果保留小数部分。
二、Fortran数据类型在进行除法运算时,需要了解Fortran中的数据类型。
Fortran提供了多种数据类型,包括整数类型(INTEGER)、实数类型(REAL)、双精度实数类型(DOUBLE PRECISION)等。
在进行除法运算时,需要注意操作数的数据类型以及结果的数据类型。
若要进行整数除法,需要使用整数类型的变量作为操作数,并将结果存储在整数类型的变量中。
若要进行浮点数除法,可以使用实数类型或双精度实数类型的变量作为操作数,并将结果存储在相应的实数类型或双精度实数类型的变量中。
以下是一个示例代码,展示了如何定义不同数据类型的变量并进行除法运算:```fortranINTEGER :: a, b, cREAL :: d, e, fa = 10b = 3c = a / bd = 10.0e = 3.0f = d / e```三、处理除数为零的情况在进行除法运算时,需要注意除数为零的情况。
若除数为零,除法操作将会导致错误或异常情况。
为了避免程序异常终止,可以在进行除法运算之前添加判断条件,检查除数是否为零。
若除数为零,可以采取相应的处理措施,例如输出错误信息或进行其他操作。
以下是一个示例代码,展示了如何处理除数为零的情况:```fortranINTEGER :: a, b, ca = 10b = 0IF (b /= 0) THENc = a / bELSEWRITE(*,*) "除数不能为零"END IF```四、实际应用案例Fortran除法在实际应用中有着广泛的应用。
数值计算基础目录实验一直接法解线性方程组的 (2)实验二插值方法 (11)实验三数值积分 (5)实验四常微分方程的数值解 (7)实验五迭代法解线性方程组与非线性方程 (9)实验一 直接法解线性方程组一、实验目的掌握全选主元消去法与高斯-塞德尔法解线性方程组。
二、实验内容分别写出Guass 列选主元消去法与追赶法的算法,编写程序上机调试出结果,要求所编程序适用于任何一解线性方程组问题,即能解决这一类问题,而不是某一个问题。
实验中以下列数据验证程序的正确性。
1、用Guass 列选主元消去法求解方程组⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡--5.58.37.33.47.11.85.16.93.51.53.25.2321x x x2、用追赶法求解方程组⎥⎥⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎢⎢⎣⎡-=⎥⎥⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎢⎢⎣⎡⎥⎥⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎢⎢⎣⎡-----000010210000210000210000210000254321x x x x x 三、实验仪器设备与材料主流微型计算机四、实验原理1、Guass 列选主元消去法 对于AX =B1)、消元过程:将(A|B )进行变换为)~|~(B A ,其中A ~是上三角矩阵。
即:⎪⎪⎪⎪⎪⎭⎫⎝⎛→⎪⎪⎪⎪⎪⎭⎫ ⎝⎛n nn nnn nnn n nn b a b a b a a b a a a b a a a b a a a0010122111221222221111211 k 从1到n-1a 、 列选主元选取第k 列中绝对值最大元素ik ni k a ≤≤max 作为主元。
b 、 换行ik ij kj b b n k j a a ⇔+=⇔,,1,c 、 归一化 kkk k kj kk kj b a b n k j a a a ⇒+=⇒/,,1,/d 、 消元nk i b b a b n k j n k i a a a a i k ik i ij kj ik ij ,,1,,,1;,,1, +=⇒-+=+=⇒-2)、回代过程:由)~|~(B A 解出11,,,x x x n n -。
1,2,,1,/1-=⇒-⇒∑+=n k x x ab x a b k nk j j kjk nnn n2、追赶法 线性方程组为:⎪⎪⎪⎪⎪⎪⎪⎪⎪⎭⎫⎝⎛=⎪⎪⎪⎪⎪⎪⎪⎪⎪⎭⎫ ⎝⎛⎪⎪⎪⎪⎪⎪⎪⎪⎪⎭⎫ ⎝⎛-----n n n n n nn n n f f f f f x x x x x a b c a b c a b c a b c a 132********33322211做LU 分解为:⎪⎪⎪⎪⎪⎪⎪⎪⎭⎫⎝⎛=⎪⎪⎪⎪⎪⎪⎪⎪⎭⎫⎝⎛=-1111,12133221n n n R L βββαγαγαγα分解公式:⎪⎪⎪⎩⎪⎪⎪⎨⎧-===-====-)1,,2,1(),,3,2(,),,3,2(111n i c n i b b n i a i i i i i i i i i αββγααγ 则⎩⎨⎧==⇒=⇒=y Ux fLy f LUx f Ax 回代公式:⎪⎪⎩⎪⎪⎨⎧=-==-),,3,2(1111n i y f y f y i i i i i αγα⎩⎨⎧--=-==+)1,,2,1(1n n i x y x y x i i i i n n β五、实验步骤1、理解并掌握全选主元消去法与高斯-塞德尔迭代法公式;2、画出全选主元消去法与高斯-塞德尔迭代法的流程图3、使用C 语言编写出相应的程序并调试验证通过六、实验报告要求1、统一使用《武汉科技大学实验报告》本书写,实验报告的内容要求有:实验目的、实验内容、程序流程图、源程序、运行结果及实验小结六个部分。
2、源程序需打印后粘贴在实验报告册内;3、运行结果以屏幕截图形式保存并打印后粘贴在实验报告册内。
七、实验注意事项注意如何定义数据结构以保存矩阵和解以降低算法的复杂性。
八、思考题若使用全主元消去法,在编程中应如何记录保存对于未知数的调换。
实验二 插值方法一、实验目的掌握拉格郎日插值法与牛顿插值法构造插值多项式。
二、实验内容分别写出拉格郎日插值法与牛顿插值法的算法,编写程序上机调试出结果,要求所编程序适用于任何一组插值节点,即能解决这一类问题,而不是某一个问题。
实验中以下列数据验证程序的正确性。
已知下列函数表求x=0.5635时的函数值。
三、实验仪器设备与材料主流微型计算机四、实验原理已知n 个插值节点的函数值,则可由拉格郎日插值公式与牛顿插值公式构造出插值多项式,从而由该插值多项式求出所要求点的函数值。
拉格郎日插值公式与牛顿插值公式如下:1、Lagrange 插值公式)()(...)()()(01100x l y y x l y x l y x l x L nk k k n n n ∑==+++=∏≠=+-+---=----------=n kj j jk j n k k k k k k k n k k k x x x x x x x x x x x x x x x x x x x x x x x x x l 011101110)())(())(()())(())(()( 2、Newton 插值公式)())(](,,[))(](,,[)](,[)()(11010102100100----++--+-+=n n n x x x x x x x x x f x x x x x x x f x x x x f x f x N五、实验步骤1、理解并掌握拉格郎日插值法与牛顿插值法的公式;2、画出拉格郎日插值法与牛顿插值法算法的流程图;3、使用C 语言编写出相应的程序并调试验证通过。
六、实验报告要求1、统一使用《武汉科技大学实验报告》本书写,实验报告的内容要求有:实验目的、实验内容、程序流程图、源程序、运行结果及实验小结六个部分。
2、源程序需打印后粘贴在实验报告册内;3、运行结果以屏幕截图形式保存并打印后粘贴在实验报告册内。
七、实验注意事项Newton插值法在编程时应注意定义何种数据结构以保存差商。
八、思考题比较Lagrange插值法与Newton插值法的异同。
实验三 数值积分一、实验目的掌握复化梯形法与龙贝格法计算定积分。
二、实验内容分别写出变步长梯形法与Romberge 法计算定积分的算法,编写程序上机调试出结果,要求所编程序适用于任何类型的定积分,即能解决这一类问题,而不是某一个问题。
实验中以下列数据验证程序的正确性。
求00001.0,sin 10≤⎰εdx x x。
三、实验仪器设备与材料主流微型计算机四、实验原理通过变步长梯形法与龙贝格法,我们只要知道已知n 个求积节点的函数值,则可由相应的公式求出该函数的积分值,从而不需要求该函数的原函数。
变步长梯形法与龙贝格法公式如下:1、变步长梯形法∑∑-=-=+++=+=11101)]()(2)([2)]()([2n i i n i i i n b f x f a f hx f x f hT∑-=++=12/12)(221n i i n nx f h T T用ε≤-n n T T 2来控制精度 2、龙贝格法∑-=++=12/12)(221n i i n nx f h T Tn n n T T S 31342-=nn n S S C 15115162-= nn n C C R 63163642-=用ε≤-n n R R 2来控制精度五、实验步骤1、理解并掌握变步长梯形法与龙贝格法的公式;2、画出变步长梯形法与龙贝格法的流程图3、使用C 语言编写出相应的程序并调试验证通过六、实验报告要求1、统一使用《武汉科技大学实验报告》本书写,实验报告的内容要求有:实验目的、实验内容、程序流程图、源程序、运行结果及实验小结六个部分。
2、源程序需打印后粘贴在实验报告册内;3、运行结果以屏幕截图形式保存并打印后粘贴在实验报告册内。
七、实验注意事项在⎰10sin dx x x积分中,被积函数在x=0点函数值为1,对该点在程序设计中应注意对其的定义。
八、思考题使用复化梯形法与复化Simpson 法来计算该问题有何缺点?实验四 常微分方程的数值解一、实验目的掌握改进欧拉法与四阶龙格-库塔求解一阶常微分方程的初值问题。
二、实验内容分别写出改进欧拉法与四阶龙格-库塔求解的算法,编写程序上机调试出结果,要求所编程序适用于任何一阶常微分方程的数值解问题,即能解决这一类问题,而不是某一个问题。
实验中以下列数据验证程序的正确性。
求⎩⎨⎧≤≤=-=')50(2)0(2x y xy y 步长h=0.25。
三、实验仪器设备与材料主流微型计算机四、实验原理常微分方程的数值解主要采用“步进式”,即求解过程顺着节点排列次序一步一步向前推进,在单步法中改进欧拉法和四阶龙格-库塔法公式如下:1、改进欧拉法),(1n n n n y x hf y y +=+)],(),([2111+++++=n n n n n n y x f y x f hy y2、四阶龙格-库塔法⎪⎪⎪⎪⎪⎩⎪⎪⎪⎪⎪⎨⎧++=++=++==++++=+),()2,2()2,2(),()22(6342312143211hk y h x f k k h y h x f k k h y h x f k y x f k k k k k h y y n n n n nn n n n n 五、实验步骤1、理解并掌握改进欧拉法与四阶龙格-库塔法的公式;2、画出改进欧拉法与四阶龙格-库塔法的流程图3、使用C 语言编写出相应的程序并调试验证通过六、实验报告要求1、统一使用《武汉科技大学实验报告》本书写,实验报告的内容要求有:实验目的、实验内容、程序流程图、源程序、运行结果及实验小结六个部分。
2、源程序需打印后粘贴在实验报告册内;3、运行结果以屏幕截图形式保存并打印后粘贴在实验报告册内。
七、实验注意事项⎩⎨⎧≤≤=-=')50(2)0(2x y xy y 的精确解为)1/(22x y +=,通过调整步长,观察结果的精度的变化八、思考题如何对四阶龙格-库塔法进行改进,以保证结果的精度。
实验五 迭代法解线性方程组与非线性方程一、实验目的掌握高斯-塞德尔迭代法求解线性方程组与牛顿迭代法求方程根。
二、实验内容分别写出高斯-塞德尔迭代法与牛顿迭代法的算法,编写程序上机调试出结果,要求所编程序适用于任何一个方程的求根,即能解决这一类问题,而不是某一个问题。
实验中以下列数据验证程序的正确性。
1、高斯-塞德尔迭代法求解线性方程组⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡-=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡----017413231511222315921274321x x x x 2、用牛顿迭代法求方程013=--x x 的近似根,00001.0≤ε,牛顿法的初始值为1。