Replace private derivatives in sympy

I am studying outrage in SymPy. Let, say, have the expression U (X, Y) = 0, where U is a function of X and Y. The function does not have a closed solution for Y, so I approximate it by expanding Taylor around the known solution, taking the first, second, third and etc. derived from my expression about X, where I take into account that Y is a function of X:

dU (X, Y (X)) / dX = U_X + U_Y * Y_X

To find Y_X, I need to replace U_X and U_Y with their values ​​in a known solution. The following SymPy code, however ...

from sympy import * X, b = symbols('X b') U = Function('U') Y = Function('Y')(X) diff1 = diff(U(X,Y)) print diff1 

... gives me the following result:

 Derivative(U(X, Y(X)), X) + Derivative(Y(X), X)*Subs(Derivative(U(X, _xi_2), _xi_2), (_xi_2,), (Y(X),)) 

I know how to replace Derivative(Y(X), X) with, say, b variable:

 diff1.subs({diff(Y,X):b}) b*Subs(Derivative(U(X, _xi_2), _xi_2), (_xi_2,), (Y(X),)) + Derivative(U(X, Y(X)), X) 

If I had numerical values ​​U_X ( Derivative(U(X, y(X)), X) ) and U_Y ( Subs(Derivative(U(X, _xi_2), _xi_2), (_xi_2,), (Y(X),)) ), I could solve this for b , and then I have an estimate of Y_X, but how to replace these partial derivatives with some numerical value?

In response to the first comment: one solution really should use an explicit form of U, e.g.

 X, b = symbols('X b') Y = Function('Y')(X) U = (X**0.5)*(Y**0.5)-100 diff1 = diff(U,X) diff1 = diff1.subs({diff(Y,X):b,Y:100,X:100}) sol1 = solve(diff1,b)[0] diff2 = diff(U,X,2) diff2 = diff2.subs({diff(Y,X,2):b,diff(Y,X):sol1,Y:100,X:100}) sol2 = solve(diff2,b)[0] diff3 = diff(U,X,3) diff3 = diff3.subs({diff(Y,X,3):b,diff(Y,X,2):sol2,diff(Y,X):sol1,Y:100,X:100}) sol3 = solve(diff3,b)[0] 

Etc. Note that the first variable to be replaced is the derivative of interest; it is replaced by a dummy variable that allows the expression to be resolved. Then all other derivatives are replaced (for which the values ​​are known from the previous steps); then the parameters and variables are replaced. It really works, but I was hoping there would be a more elegant way to do it. Apparently not.

+5
source share

All Articles