5
$\begingroup$

I am dealing with the vibration of a multi degree of freedom system with driving force, and I need to find the relation between the amplitude and the frequency of the driving force.

I hope to get an accurate solution, So I used DSolve to get the analytical solution, then I used /. to replace symbols with numeric values. However, I can't simplify the anwser.

Here is a simple example that can explain what I want to do:

How can I simplify

Function[{t}, t + 1 + 1]

to

Function[{t}, t + 2]

$\endgroup$
2
  • $\begingroup$ Not that it's not a good question, but this smells like an XY problem. Have you tried DSolve[expr, y[t], t] instead of DSolve[expr, y, t] to get a non-pure function? If you're substituting the solution into an expression with derivatives of y (or other operators), then you're good, but otherwise it might be simpler to just get the image of t under y (i.e. y[t]) instead of the function itself. Not trying to dismiss your question, just trying to address the core of your issue as best as possible. $\endgroup$ Commented Oct 10, 2022 at 18:14
  • $\begingroup$ If you use DSolve[expr, y[x], t] instead of DSolve[expr, y, t], you can use the resource function SolutionRulesToFunctions to obtain a pure function as follows: DSolve[expr, y[t], t]//Simplify//SolutionRulesToFunctions. $\endgroup$ Commented Oct 10, 2022 at 21:06

5 Answers 5

7
$\begingroup$

Similar, but different, to @Alan's answer:

f = Function[{t}, t + 1 + 1];
MapAt[Evaluate, f, {2}]

Another one:

f = Function[{t}, t + 1 + 1];
ReplacePart[f, 2 -> f[[2]]]
$\endgroup$
1
  • $\begingroup$ As written, these leave f unchanged. $\endgroup$
    – Alan
    Commented Oct 12, 2022 at 0:31
7
$\begingroup$
f = Function[{t}, t + 1 + 1]
f[[2]] = f[[2]]
$\endgroup$
3
  • $\begingroup$ As written, this changes f. $\endgroup$ Commented Oct 12, 2022 at 8:01
  • $\begingroup$ @AntonAntonov My comment was meant to help the OP. Just in case. $\endgroup$
    – Alan
    Commented Oct 13, 2022 at 14:58
  • $\begingroup$ Same here. Thanks! $\endgroup$ Commented Oct 14, 2022 at 0:18
5
$\begingroup$

We can also use Map (/@). Also, if t is polluted, we need Block etc.:

f = Function[{t}, t + 1 + 1];
t = 111;
Block[{t}, Evaluate /@ f]

Alternatively:

f = Function[{t}, t + 1 + 1];
t = 111;
Block[{t}, Function @@ List @@ f]

Or directly use Block to disable Function:

f = Function[{t}, t + 1 + 1];
t = 111;
Block[{t, Function}, f]

Just for fun, an unnecessarily complicated but working solution:

f = Function[{t}, t + 1 + 1];
t = 111;
Block[{t}, Replace[f, a_ :> RuleCondition@a, {1}]]
$\endgroup$
1
  • 1
    $\begingroup$ Rule for constructing the Block: Verbatim[Function][v_List, body_] :> Block[v, < your favorite fix >]. If Function[t,...], then give a list of rules covering the cases, I guess, including Function[Null,...] (+1) $\endgroup$
    – Michael E2
    Commented Oct 10, 2022 at 16:15
3
$\begingroup$

Using DSolve[expr, y[t], t] instead DSolve[expr, y, t]:

solrules = First @@ DSolve[{y''[x] - x y[x] == 0, y[0] == 1, y[9] == 1}, y[x], x]

enter image description here

Using Simplify and the resource function SolutionRulesToFunctions:

solrules //Simplify //SolutionRulesToFunctions

enter image description here

$\endgroup$
2
$\begingroup$

Also similar to other answers (in-place modification):

f=Function[{t}, t + 1 + 1]

f[[2]]//=Evaluate;f

(* Function[{t}, 2 + t] *)

Alternatively:

f//=MapAt[Evaluate,2]

There is also Query

Query[{2->Evaluate}]@f

but 'under the hood' this appears to be identical to the answer given by Anton Antonov

Query[{2->Evaluate}]//Normal

(* MapAt[Evaluate, 2] *) 
$\endgroup$

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.