I am having difficulty setting up a function where I substitute x1[1]
and x2[1]
into it. Any help would be greatly appreciated.
My code is:
restart;
with(LinearAlgebra);
x1[1] := -2+1606*alpha;
x2[1] := 2+400*alpha;
f := proc (alpha) options operator, arrow; (-x1[1]^2+1)^2+100*(-x1[1]^2+x2[1])^2 end proc;
The output from f leaves x1[1]
and x2[1]
and does not replace them with -2+1600*alpha
and 2+400*alpha
as desired.
f(0);
then does not give the desired output of 409.
I can physically substitute -2+1606*alpha
and 2+400*alpha
in myself as shown by the function g
below. Though this is not what I would like as I would like it to take whatever x1[1]
and x2[1]
are set to, and not do it manually.
g := proc (alpha) options operator, arrow; (1-(1606*alpha-2)^2)^2+100*(2+400*alpha-(1606*alpha-2)^2)^2 end proc
g(0);
Here g(0);
gives 409 as desired.
I have attached a photo below of how the maple output appears on my screen.
When you apply the operator procedure on an expression, Maple does that before evaluation. In the expression
f := proc (alpha) options operator, arrow; (-x1[1]^2+1)^2+100*(-x1[1]^2+x2[1])^2 end proc;
which is the same as using the "arrow" notation
f2 := alpha -> (-x1[1]^2+1)^2+100*(-x1[1]^2+x2[1])^2;
then Maple is looking for any alpha
. There is none. When you then call f(0)
, the expression is evaluated with the (non-existing) alpha
from the call of the operator. Thus, f(x) = f(0)
for any x
. From the help page:
The use of option arrow also disables Maple simplification rules that add any non-local names in the operator expression to the parameter list. This option has no meaning for modules.
Instead, what you can do is use the unapply
operator, which returns an operator of an evaluated expression. This becomes a little more clear if you define a variable expr
as your function content.
expr := (-x1[1]^2+1)^2+100*(-x1[1]^2+x2[1])^2;
f := alpha -> expr;
h := unapply(expr, alpha);
h(0); # returns 409