问题
This may be very basic, but I can't seem to figure it out: I am trying to use the output of a procedure directly as a function. The Procedure is a prefabricated one, namely the dsolve option in MAPLE. Specifically, I would like to say
dsolve({diff(y(t), t) = y(t)*t, y(1) = 1}, y(t), series, t = 1, order = 7)
The result is
y(t) = t+1-1+(t-1)^2+(2/3)*(t-1)^3+(5/12)*(t-1)^4+(13/60)*(t-1)^5+(19/180)*(t-1)^6+O((t-1)^7)
Which is great, but I can't use this as a function directly, i.e., when I type in y(3), I get y(3). I'm sure this is because the procedure is returning a statement instead of a function. I guess the most basic way around this would be to copy and paste the expression and say y:=t-> whatever, but this is inelegant. How can I get around this? Thank you
回答1:
Yes, you are getting back an equation of the form y(t)=<some series>
from your dsolve
call.
dsol := dsolve({diff(y(t), t) = y(t)*t, y(1) = 1}, y(t), series, t = 1, order = 5);
2 2 3 5 4 / 5\
dsol := y(t) = 1 + (t - 1) + (t - 1) + - (t - 1) + -- (t - 1) + O\(t - 1) /
3 12
You can also convert the series structure on the right-hand side of that to a polynomial (ie. get rid of the big-O term).
convert( dsol, polynom );
2 2 3 5 4
y(t) = t + (t - 1) + - (t - 1) + -- (t - 1)
3 12
You can also evaluate the expression you want, y(t)
, at that equation. (Or you could just use the rhs
command. For sets of equations in the multivariable case the eval
approach is more robust and straighforward.)
eval( y(t), convert( dsol, polynom ) );
2 2 3 5 4
t + (t - 1) + - (t - 1) + -- (t - 1)
3 12
And, finally, you can also produce an operator from this expression.
Y := unapply( eval( y(t), convert( dsol, polynom ) ), t );
2 2 3 5 4
Y := t -> t + (t - 1) + - (t - 1) + -- (t - 1)
3 12
That operator is a procedure, and can be applied to whatever point you want.
Y(3);
19
The way I have it above, the statement which assigns to Y
happens to contain all the individual steps. And that's the only statement above which you'd need to execute, to get the operator that I assigned to Y
.If you prefer you could do each step separately and assign each intermediate result to some name. It just depends on whether you want them for any other purpose.
restart:
dsol := dsolve({diff(y(t), t) = y(t)*t, y(1) = 1}, y(t), series, t = 1, order = 5):
peq := convert( dsol, polynom ):
p := eval( y(t), peq ):
Y := unapply( p, t ):
Y(3);
19
来源:https://stackoverflow.com/questions/28816700/how-can-i-use-the-return-of-a-maple-procedure-as-a-function-directly