1
$\begingroup$

I am a freshman working with Mathematica. I have written a package with a function like this:

SolidShapeFunction2d[Coords_List] := 
  Module[{u, p1, p2, p3, u1, u2, u3},
    u = p1 + p2 * x + p3 * y;
    x1 = Coords[[1, 1]];
    y1 = Coords[[1, 2]];
    x2 = Coords[[2, 1]];
    y2 = Coords[[2, 2]];
    x3 = Coords[[3, 1]];
    y3 = Coords[[3, 2]];
    solut1 = 
      Solve[{
        (u /. x -> x1 /. y -> y1) == u1, 
        (u /. x -> x2 /. y -> y2) == u2, 
        (u /. x -> x3 /. y -> y3) == u3},
        {p1, p2, p3}];
    u = u /. solut1;
    N1 = Coefficient[u, u1];
    N2 = Coefficient[u, u2];
    N3 = Coefficient[u, u3];
    {{N1, 0, N2, 0, N3, 0}, {0, N1, 0, N2, 0, N3}}]

This function is designed to obtain the shape function in FEM. But when I use this in a notebook, the answer doesn't seem correct:

<< MyFem`
Nshape = SolidShapeFunction2d[Coords]
{{{1 - MyFem`Private`x}, 0, {MyFem`Private`x - MyFem`Private`y}, 0, 
    {MyFem`Private`y}, 0}, 
 {0, {1 - MyFem`Private`x}, 0, {MyFem`Private`x - MyFem`Private`y}, 0, 
    {MyFem`Private`y}}}

What does the MyFem`Private` mean? And how can eliminate it?

$\endgroup$
  • $\begingroup$ Begin statement in your package makes new symbols to create in given context. You don't want to see this context or what? I rather think there is a problem in definition, unless you really want symbolic output. $\endgroup$ – Kuba Mar 30 '15 at 8:38
  • $\begingroup$ @ Kuba.In this function,I do want the symbolic output. Because in the next function(I don't post here),some value will be bound to the "X" and "Y".And the function will be evaluted to obtain numerical answer. $\endgroup$ – Yang Mar 30 '15 at 8:50
  • $\begingroup$ I think this is in fact a good question. $\endgroup$ – Kuba Mar 30 '15 at 8:58
  • 1
    $\begingroup$ It's your use of an undefined x and y while in the Private part of the context of your package. It's rather bad practice to use and return those. If you want to return symbols from a global context I guess it would be better to introduce those as parameters to functions that you call from this package. So, function template should look like SolidShapeFunction2d[Coords_List,x_,y_] $\endgroup$ – Sjoerd C. de Vries Mar 30 '15 at 9:00
  • $\begingroup$ @SjoerdC.deVries.Thanks for your answer. I will hava a try and report later. $\endgroup$ – Yang Mar 30 '15 at 9:04
1
$\begingroup$

This question has been solved with the help from comments above. In fact, I have to say,I don't really think how to use this function when I begin to edit.In the process of discussing with others, I think there are two ways to solve this problem.

If I really want a symbolic output, I must define or declare X and Y at the beginning of the Private part.But I don't think it is a good way because the system will give warnnings when you load the package, and two important and often-used symbolic variable will be shadowed.

The another way is rewriting the function just like following:

SolidShapeFunction2d[Coords_List,x_,y_]

From my experience, the second way is encouraged。

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.