Adriaan
Adriaan

Reputation: 715

Define a set of functions by means of for loops in Maple

In Maple, I want to define a set of functions by means of two for loops:

printlevel:=2;

# Node coordinates.
N_x:=5;
N_y:=4;
N_elx:=N_x-1;
N_ely:=N_y-1;
h_x:=(x_e-x_s)/N_elx;
h_y:=(y_e-y_s)/N_ely;
x_n:=[seq(x_s+j*h_x,j=0..N_elx)];
y_n:=[seq(y_s+j*h_y,j=0..N_ely)];

# Partition of unity.
for j from 2 by 1 to N_x-1 do
    for k from 2 by 1 to N_y-1 do
        phi[j,k]:=(x,y)->(x-x_n[j-1])*(x-x_n[j+1])*(y-y_n[k-1])*(y-y_n[k+1])/((x_n[j]-x_n[j-1])*(x_n[j]-x_n[j+1])*(y_n[k]-y_n[k-1])*(y_n[k]-y_n[k+1]));
    od;
od;

However, this gives output in which the [j,k] vary but the x_n[j-1], x_n[j+1], y_n[j-1] and y_n[j+1] are not evaluated. Consequently, the functions are not defined properly. For instance, calling the j=4,k=2 function with phi[4,2](x,y);, I get the output

phi[4, 2] called with arguments: x, y
#(phi[4,2],1): (x-x_n[j-1])*(x-x_n[j+1])*(y-y_n[k-1])*(y-y_n[k+1])/(x_n[j]-x_n[j-1])/(x_n[j]-x_n[j+1])/(y_n[k]-y_n[k-1])/(y_n[k]-y_n[k+1])
Error, (in phi[4, 2]) invalid subscript selector

instead of the desired output

$\phi_{4,2}:=\dfrac{(x-x_3)(x-x_5)(y-y_1)(y-y_3)}{(x_4-x_3)(x_4-x_5)(y_2-y_1)(y_2-y_3)}=\dfrac{(x-\dfrac{1}{2})(x-1)(y-0)(y-\dfrac{2}{3})}{(\dfrac{3}{4}-\dfrac{1}{2})(\dfrac{3}{4}-1)/(\dfrac{1}{3}-0)(\dfrac{1}{3}-\dfrac{2}{3})}$ enter image description here

How do I solve this?

Upvotes: 2

Views: 323

Answers (1)

Adriaan
Adriaan

Reputation: 715

Answer from acer:

If you want operators instead of expressions, use the function unapply:

N_x:=5;
N_y:=4;
N_elx:=N_x-1;
N_ely:=N_y-1;
h_x:=(x_e-x_s)/N_elx;
h_y:=(y_e-y_s)/N_ely;
x_n:=[seq(x_s+j*h_x,j=0..N_elx)];
y_n:=[seq(y_s+j*h_y,j=0..N_ely)];

# Partition of unity.
for j from 2 by 1 to N_x-1 do
    for k from 2 by 1 to N_y-1 do
        phi[j,k]:=unapply( (x-x_n[j-1])*(x-x_n[j+1])
                           *(y-y_n[k-1])*(y-y_n[k+1])
                           /((x_n[j]-x_n[j-1])*(x_n[j]-x_n[j+1])
                            *(y_n[k]-y_n[k-1])*(y_n[k]-y_n[k+1])),
                           [x,y]);
    od;
od;

Upvotes: 1

Related Questions