Group Abstract Group Abstract

Message Boards Message Boards

Using a rule as an input to a function?

Posted 2 years ago
POSTED BY: Nir Livne
3 Replies
Posted 2 years ago

That's a lifesaver for me, thank you!! I'm not really sure why, but without the [[1, -1]] in the definition of f:

f[T_] := FindRoot[s[x] - T*x, {x, 5}][[1, -1]]

I was having trouble using the FindRoot function without the Inactivate function

Problem solved, thank you! .enter image description here

POSTED BY: Nir Livne
Posted 2 years ago

Given an expression like this, {x -> 3.07401}, we can extract the 3.07401 in a few ways.

Part[{x -> 3.07401}, 1, -1]
(* also written as {x -> 3.07401}[[1, -1]] *)
Part[Values[{x -> 3.07401}], 1]
(* also written as Values[{x -> 3.07401}][[1]] *)
Lookup[{x -> 3.07401}, x]
ReplaceAll[x, {x -> 3.07401}]
(* also written as x /. {x -> 3.07401} *)

Now we need to decide where to do this extraction. You can apply it to the argument of S or you can apply it to the output of f. So, instead of S[T_] := s[f[T]], do

S[T_] := s[f[T][[1, -1]]]

Or instead of f[T_] := FindRoot[s[x] - T*x, {x, 20}] do

f[T_] := FindRoot[s[x] - T*x, {x, 20}][[1, -1]]

Side note: get rid of all of the Activate and Inactive stuff

POSTED BY: Eric Rimbey
Posted 2 years ago

That's a lifesaver for me, thank you!! I'm not really sure why, but without the [[1, -1]] in the definition of f:

f[T_] := FindRoot[s[x] - T*x, {x, 5}][[1, -1]]

I was having trouble using the FindRoot function without the Inactivate function

Problem solved, thank you! .enter image description here

POSTED BY: Nir Livne
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard