0
$\begingroup$

In short, my question is the same as my previous one except that everything is now wrapped up in a module.

The relevant code I'm working with is:

getinter[a_, b_, u0_, k_, m_, hbar_, Nu_, Np_, up_] :=

Module[{ekp, ms, LUs, env, eenv, envpart, f, kppart, g, approx, 
   approx1, papprox, approx2, hard, ereal, psiparts, real, real1, 
   realp, real2, er, inter},

  ekp = energies[a, b, u0, k, 1, m, hbar, 0.001, 10^-15][[1]];

  ms = effmass[a, b, u0, k, 0.2, 1, m, hbar, 0.001, 10^-15];

  LUs = BuildLUs[a, b, Nu, Np, u0, up];

  env[x_] := Abs[Det[envfunc[ms, hbar, x, up, Nu, Np, a, b]]]^2;

  eenv = zeros[env, up + 0.000001, 0.01, 0, 1, 10^-15, 0.02][[1]];

  envpart = getpsipieces[LUs[[1]], LUs[[2]], eenv, ms, hbar];

  f[x_] := Evaluate@Piecewise[envpart];

  Return[f[0.231]];

]

envpart is a properly formatted object for piecewise and the variable it uses is named x. When I evaluate this, it's returning the structure I put into piecewise but now formatted as a case structure as opposed to an array.

What I'd like it to do is to return f[0.231]='some number' like you would expect. Also, f[x_]:= Evaluate@Piecewise[envpart]; works fine if I take it out of the module.

Thanks for the help. Also, I get the sense that the solution to this problem, if I fully understand it, will allow me to work out a lot of other problems I encounter, so I'd really appreciate any suggestions on where I can learn about the issues at play.

Edited for slightly less awful formatting.

$\endgroup$
5
  • $\begingroup$ When you evaluate it with what values for the input parameters? Does your output actually look like the output you've posted? (TagBox, GridBox stuff is usually hidden). $\endgroup$
    – Ymareth
    Commented Feb 25, 2014 at 8:49
  • $\begingroup$ Do you mean the a,b,hbar,etc.? They're just regular, small, real numbers. The argument of f is just a real number also. $\endgroup$
    – user12308
    Commented Feb 25, 2014 at 8:52
  • $\begingroup$ Wait, how can it be that you define env[x_]as a function of x, but are using env without parameters on the next line? $\endgroup$ Commented Feb 25, 2014 at 9:25
  • $\begingroup$ It's a good idea to reduce your problem to a Minimal Working Example that people can copy and paste into Mathematica to work on. This has 3 benefits, firstly it makes it easier for people to help you, secondly it makes the question and answers more useful to future visitors who might have the same problem, and thirdly the process of stripping the code down to the core of the problem will often help you to solve it yourself. $\endgroup$ Commented Feb 25, 2014 at 12:33
  • $\begingroup$ In this example you could reduce the problem to something like myfunc[] := Module[{rhs, f}, rhs = 2 x; f[x_] := Evaluate[rhs]; f[5]] This is small enough that you could use Trace[myfunc[]] to see exactly what is happening. $\endgroup$ Commented Feb 25, 2014 at 12:34

2 Answers 2

3
$\begingroup$

Use pure functions as a return value :

fOne[x_] :=(*just return a piecewise*)
            Piecewise[{{#^2, # < x}, {#^2 + (x - #)^3 Sin[3 #], # > x}}] &

fTwo[y_, z_, w_] := Module[{vars},
                    Plot[fOne[y][x], {x, z, w}]]

fTwo[3, -3, 8]

Mathematica graphics

$\endgroup$
0
$\begingroup$

You are having a scoping problem here, because inside a Module your variable x will be renamed internally.

Just replace your temporary definition of f with a ReplaceAll. I don't have your definitions for energies, effmass, etc, so I'm not 100% sure but normally this should work:

getinter[a_, b_, u0_, k_, m_, hbar_, Nu_, Np_, up_] :=
    Module[{ekp, ms, LUs, env, eenv, envpart, f, kppart, g, approx, approx1, papprox,
        approx2, hard, ereal, psiparts, real, real1, realp, real2, er, inter},
        ekp = energies[a, b, u0, k, 1, m, hbar, 0.001, 10^-15][[1]];
        ms = effmass[a, b, u0, k, 0.2, 1, m, hbar, 0.001, 10^-15];
        LUs = BuildLUs[a, b, Nu, Np, u0, up];
        env[x_] := Abs[Det[envfunc[ms, hbar, x, up, Nu, Np, a, b]]]^2;
        eenv = zeros[env, up + 0.000001, 0.01, 0, 1, 10^-15, 0.02][[1]];
        envpart = getpsipieces[LUs[[1]], LUs[[2]], eenv, ms, hbar];

        Piecewise[envpart] /. x -> 0.231
]

You don't need to use a Return in the end of a Module, just leave out the last ;

$\endgroup$
1
  • $\begingroup$ Probably there are more elegant ways to get the requested, but then we'd need to now your definition for getpsipieces, and anyway, now it should work as is without too much modification. $\endgroup$ Commented Feb 25, 2014 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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