Search code examples
wolfram-mathematicasubstitutionsimplification

Mathematica, simplify equation with subsitutions


There is the following formula:

s = R*(lat - lat0)
rho = R/Tan[lat]
f = (x^2 + (rho + s - y)^2 - rho^2)*(Sin[lat])^2

and its derivative

fd = D[f, lat]

where

Output[1] = 2 Cos[lat] (x^2 - R^2 Cot[lat]^2 + ((lat - lat0) R - y + R Cot[lat])^2) Sin[lat] + 
(2 R^2 Cot[lat] Csc[lat]^2 + 2 ((lat - lat0) R - y + R Cot[lat]) (R - R Csc[lat]^2)) Sin[lat]^2

I would like to express fd using the substitutions of s, rho

FullSimplify[fd, TransformationFunctions -> {Automatic, # /. R (lat - lat0) -> s &, # /. R/Tan[lat] -> rho &}]

However, only the simplified formula without any substitution appears:

Output[2] = 2 Cos[lat] ((1 + (lat - lat0)^2) R^2 + x^2 + 2 (-lat + lat0) R y +   y^2 + R (lat R - lat0 R - y) Cot[lat]) Sin[lat]

Thanks for your help.


Solution

  • Try the substitution and confirm that the result is unchanged

    s = R*(lat - lat0);
    rho = R/Tan[lat];
    f = (x^2 + (rho + s - y)^2 - rho^2)*(Sin[lat])^2;
    fd = D[f, lat];
    FullSimplify[fd, TransformationFunctions->{Automatic,
        #/.R(lat-lat0)->s&, #/.R/Tan[lat]->rho &}];
    Simplify[% == fd]
    

    and the output is True

    Notice that you previously defined s=R*(lat-lat0) so it appears that you are replacing R(lat-lat0) with R(lat-lat0)

    Try undefining s before you do the replacement

    s =.;
    FullSimplify[fd, TransformationFunctions -> {Automatic,
        #/.R(lat-lat0)->s&, #/.R/Tan[lat]->rho&}]
    

    and the result is 2 Cos[lat](R(s-y)Cos[lat]+(R^2+x^2+(s-y)^2)Sin[lat])

    Now why didn't the R/Tan[lat] replacement work in the original fd?

    D[f, lat]==2 Cos[lat](x^2-R^2 Cot[lat]^2+((lat-lat0)R-y+R Cot[lat])^2) Sin[lat]+(2 R^2 Cot[lat]Csc[lat]^2+2((lat-lat0)R-y+R Cot[lat])(R-R Csc[lat]^2)) Sin[lat]^2

    Notice there is no R/Tan[lat] in that. Mathematica pattern matching is very literal and has no ability to understand that R/Tan[lat]==R Cot[lat] Many years ago there was a package that a guy wrote which did "mathematical substitutions" instead of the Mathematica "literal substitutions", but that is out of date and I don't know enough to make that work with current versions.

    Let's try the R Cot[lat] substitution instead and undefine rho so it doesn't undo any substitution.

    s =.; rho =.
    fd /. {R Cot[lat] -> rho}
    

    The result is 2 Cos[lat](x^2+((lat-lat0)R+rho-y)^2-R^2 Cot[lat]^2) Sin[lat]+(2 R^2 Cot[lat]Csc[lat]^2+2 ((lat-lat0)R+rho-y)(R-R Csc[lat]^2))Sin[lat]^2

    Notice that R^2 Cot[lat]^2 remains and again literal substitution doesn't know that you probably expect R Cot[lat]->rho to change that into rho^2 so add that rule

    s=.; rho=.
    fd /. {R Cot[lat] -> rho, R^2 Cot[lat]^2 -> rho^2}
    

    Notice that R^2 Cot[lat] remains and you probably meant that to be replaced by R rho so add that rule.

    s =.; rho =.
    fd /. {R Cot[lat]->rho, R^2 Cot[lat]^2->rho^2, R^2 Cot[lat]->R rho}
    

    Are you beginning to get the idea that pattern substitution in Mathematica can become a dark and twisting hallway leading to a door labeled "Frustration."

    There is one trick that you might be able to use.

    Simplify[fd, R Cot[lat] == rho]
    

    That will try to simplify fd and usually attempt to replace R Cot[lat] with rho. And, in this particular example, that will even work with

    Simplify[fd, R/Tan[lat] == rho]
    

    But there are NO guarantees that this will always work or will do what you want and in some cases this will do even more strange and bizarre things.

    Perhaps this gives you enough hints that you can make some progress.