问题
I am working on my project of graduating, particularly, about fluid dynamics and I have a system of non-linear equations to solve, I choose the Newton's method so I have to pass through the Jacobian of the matix (actually 12x12 matrix). Every element in this matrix is the derivative of the function evaluated at some point, it's very difficult to write all of these manually and calculate each derivative; the system looks like:
f1 (x1, x2, x3, ..., x12) = 0f2 (x1, x2, x3, ..., x12) = 0
.
.
.
f12 (x1, x2, x3, ..., x12) = 0
Where x1, x2, x3 are the variables (Temperature, pressure ...etc)
Can I automate this operation? If it's not possible in Fortran, can I use other scripting languages as Python (sympy module)?
回答1:
Yes, by use of an appropriate algorithmic differentiation package. This is a method which can evaluate (in principle) arbitrary order derivatives of any function you have expressed as a computer program, and a number of packages exist for Fortran. Take a look at
http://en.wikipedia.org/wiki/Automatic_differentiation
and
http://www.nag.co.uk/pss/nag-and-algorithmic-differentiation
to get started
Disclaimers:
1) I have never used it "in anger"
2) Until recently I worked for NAG
回答2:
Yes, you can use sympy for symbolic differentiation and for printing out Fortran code. Other system such as Maple can also do this. Be aware that you can spend quite a lot of time calculating derivatives, especially if these are not optimized, and numerical differentiation could be faster.
来源:https://stackoverflow.com/questions/29265164/automatic-differentiation