Ask Your Question

Revision history [back]

I am not sure to understand your question, but you can define your own symbolic function, and how it can be numerically evaluated (which is required by the plot function), see the documentation of function factory.

So, you can define how your legendre function evaluates numerically:

sage: def evalf_my_legendre(self, a,b,c, parent=None, algorithm=None):
....:     return gen_legendre_P(ZZ(a),ZZ(b),RDF(c))

Note that i had to convert the input because the gen_legendre_P uses the mod function, see gen_legendre_P??.

Then, you can define your own symbolic my_legendre function, that can be numerically evaluated as before:

sage: my_legendre = function("my_legendre", nargs=3, evalf_func=evalf_my_legendre)

Then you can do:

sage: Y_fnc(x,y_max,l,m)=y_max*my_legendre(l,m,cos(x))
sage: Y_fnc
(x, y_max, l, m) |--> y_max*my_legendre(l, m, cos(x))

sage: f(x,y_max,l,m)=Y_fnc(x,y_max,l,m) * cos(x)^4 -2*sqrt(Y_fnc(x,y_max,l,m))*sin(x)*cos(x)^3
sage: f
(x, y_max, l, m) |--> y_max*cos(x)^4*my_legendre(l, m, cos(x)) - 2*sqrt(y_max*my_legendre(l, m, cos(x)))*cos(x)^3*sin(x)

sage: plot(f(x,1,2,0),0,2*pi)

You can see that the plot is not complete. This is due to the fact that taking the square root of a negative number does not makes sense:

sage: f(0.2,1,2,0).n()
0.505189228949794
sage: f(1.2,1,2,0).n()
NaN
sage: my_legendre(2,0,cos(1.2))
my_legendre(2, 0, 0.362357754476674)
sage: my_legendre(2,0,cos(1.2)).n()
-0.3030452866559341

I am not sure to understand your question, but you can define your own symbolic function, and how it can be numerically evaluated (which is required by the plot function), see the documentation of function factory.

So, you can define how your legendre function evaluates numerically:

sage: def evalf_my_legendre(self, a,b,c, parent=None, algorithm=None):
....:     return gen_legendre_P(ZZ(a),ZZ(b),RDF(c))

Note that i had to convert the input because the gen_legendre_P uses the mod function, see gen_legendre_P??.

Then, you can define your own symbolic my_legendre function, that can be numerically evaluated as before:

sage: my_legendre = function("my_legendre", nargs=3, evalf_func=evalf_my_legendre)

Then you can do:

sage: Y_fnc(x,y_max,l,m)=y_max*my_legendre(l,m,cos(x))
sage: Y_fnc
(x, y_max, l, m) |--> y_max*my_legendre(l, m, cos(x))

sage: f(x,y_max,l,m)=Y_fnc(x,y_max,l,m) * cos(x)^4 -2*sqrt(Y_fnc(x,y_max,l,m))*sin(x)*cos(x)^3
sage: f
(x, y_max, l, m) |--> y_max*cos(x)^4*my_legendre(l, m, cos(x)) - 2*sqrt(y_max*my_legendre(l, m, cos(x)))*cos(x)^3*sin(x)

sage: plot(f(x,1,2,0),0,2*pi)

You can see that the plot is not complete. This is due to the fact that taking the square root of a negative number does not makes sense:

sage: f(0.2,1,2,0).n()
0.505189228949794
sage: f(1.2,1,2,0).n()
NaN
sage: my_legendre(2,0,cos(1.2))
my_legendre(2, 0, 0.362357754476674)
sage: my_legendre(2,0,cos(1.2)).n()
-0.3030452866559341

EDIT

Reading your edits and comments, it seems that l and m are integer parameters that could be fixed in advance, while x and y_max are symbolic variables. So, you can express this as follows:

sage: var('x,y_max')
(x, y_max)
sage: Y_fnc = lambda l,m : y_max*gen_legendre_P(l,m,cos(x))
sage: f = lambda l,m : Y_fnc(l,m) * cos(x)^4 -2*sqrt(Y_fnc(l,m))*sin(x)*cos(x)^3

So, when you give values to integer parameters, you get the corresponding symbolic expression:

sage: f(1,1)
-sqrt(-cos(x)^2 + 1)*y_max*cos(x)^4 - 2*sqrt(-sqrt(-cos(x)^2 + 1)*y_max)*cos(x)^3*sin(x)
sage: f(2,0)
1/2*(3*(cos(x) - 1)^2 + 6*cos(x) - 4)*y_max*cos(x)^4 - 2*sqrt(1/2)*sqrt((3*(cos(x) - 1)^2 + 6*cos(x) - 4)*y_max)*cos(x)^3*sin(x)

You can also evaluate:

sage: f(2,0)(0.3,4.5)
-1.44117873676596*sqrt(1/2) + 3.25730637499042
sage: f(2,0)(1.2,4.5)
-(8.96876567884269e-18 + 0.146471059003904*I)*sqrt(1/2) - 0.0235109558634682

You can also plot, which will work only where the function takes real values:

sage: var('t')
t
sage: plot(f(2,0)(x=t,y_max=2),t,0,2*pi)