Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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). –  Ymareth Feb 25 at 8:49
    
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. –  user12308 Feb 25 at 8:52
    
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? –  freddieknets Feb 25 at 9:25
    
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. –  Simon Woods Feb 25 at 12:33
    
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. –  Simon Woods Feb 25 at 12:34

2 Answers 2

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

share|improve this answer

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 ;

share|improve this answer
    
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. –  freddieknets Feb 25 at 9:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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