Tag Info

New answers tagged

1

You can use Inactivate and Activate to control the evaluation of the right-hand side. expression = Inactivate[a + 1]; g[x_] := x Activate[expression /. a -> 1]; g[2] (* 4 *) expression (* a + 1 *) Inactivate prevents the execution while Activate executes without losing the inactivated expression. Hope this helps.


7

There are two possibilities you could be aiming for. First, I'll take your question literally and just inject expression into the Module: expression = a + 1; With[{expression = expression}, g[x_] := Module[ {a, b}, a = 1; b = expression; x*b]] expression = 1 (* ==> 1 *) g[xi] (* ==> (1 + a) xi *) As the result after changing ...


2

Actually, it turns out that what I was looking for was a simple use of Evaluate, i.e. the following code: var={v1,v2}; Block[Evaluate[var],Print[4]] does the trick, provided there are no values assigned to v1 and v2. If that is an important consideration, the more complicated code from the other answers can be used, but for me the above simple solution ...


2

Clear[integrand] With your definition that uses an unnecessary list and then takes a part integrand[t_?NumericQ] := {1000/Sqrt[t^2 - tMin0^2], 0}; integrand[t][[1]] t This occurs because integrand cannot evaluate with a symbolic input so the first part of the unevaluated expression is the function's argument. Clear[integrand] tMin0 = 1/10; tMax0 ...


2

I gave a method for handling this kind of problem in this previous answer. Here I show its application to your problem. expToF[exp_, vars : {(_Symbol | h_Symbol[_Integer]) ..}] := With[{body = exp /. Thread[Rule[vars, Slot /@ Range@Length[vars]]]}, Function[body]] calculateDerivativeAt[xpr_, var_, val_] := Derivative[1][expToF[xpr, {var}]][val] ...


4

The reason for your problem is the automatic renaming that Mathematica makes in cases where the names would clash. Let me give a very basic example (parenthesis for clarity; not required): assign[rhs_] := (f[x_] := rhs) What happens when you call assign[x^2] is that rhs contains the pattern variable x and Mathematica does a renaming so that no bad things ...


1

I'm not sure how general this is, but it works here. Add another argument stating what the variable will be. Then replace the variable with a local variable inside your block. calculateDerivativeAt3[func_, var_] := Block[ {f, fprime, blkVar}, f[blkVar_] = (func /. var -> blkVar); fprime = Derivative[1][f]; fprime[3] ] ...



Top 50 recent answers are included