0
$\begingroup$

I am trying to write a function in mathematica which depends on different parameters. For example

$$f(x_1, x_2, x_3, x_4, x_5)= 2x_1+\sin(x_2)+x_3*x_4-x_5^{-1}$$

(My actual function has 12 such parameters $x_i$'s and the expression on the rhs is very long and complicated looking). I would like to use subscripts to make the expressions readable and also easy for me to catch typos. My goal is to investigate the behavior of a function like $f$ above using different values for $x_i$.

However, mathematica does not seem to work with subscripts well. I tried "Symbolize" using Notation package. However that does not let me evaluate the function when I plug in different values for $x_i$ such as $x_1=1, x_2=\pi,x_3=1, x_4=4, x_5=1$. It just gives me back $f[1, \pi, 1, 4, 1]$ instead of $5$. I can use "Subscript[]" but that would add more "words" to my expression and make it unreadable.

Thus is there another way of using variables with subscripts that can be evaluated using a number?

$\endgroup$
  • 3
    $\begingroup$ You're better off using variables like x[1], x[2] than subscripts, which are meant as a text formatting feature (rather than as varable names). $\endgroup$ – bill s Jun 16 at 2:14
  • $\begingroup$ The normal form is f[x1_, x2_, x3_, x4_, x5_] := 2 x1 + Sin[x2] + x3*x4 - x5^-1. Especially if your subscripts are numbers I encourage you to use this instead as this form is easier to input and work with. $\endgroup$ – Mr.Wizard Jun 16 at 2:51
4
$\begingroup$

See Defining Output Formats (tutorial/TextualInputAndOutput#9464). "whenever the Wolfram Language is given an expression to format for output, it first calls Format[expr] to find out whether any special rules for formatting the expression have been defined. By assigning a value to Format[expr] you can therefore tell the Wolfram Language that you want a particular kind of expression to be output in a special way."

Define the display Format for the indexed variables to be subscripted variables

Format[x[n_]] := Subscript[x, n]

f[x1_, x2_, x3_, x4_, x5_] := 2 x1 + Sin[x2] + x3*x4 - x5^-1

Apply f to the indexed variables

expr = f @@ Array[x, 5]

enter image description here

Or in TraditionalForm

expr // TraditionalForm

enter image description here

Substituting values

expr /. {x[1] -> 1, x[2] -> π, x[3] -> 1, x[4] -> 4, x[5] -> 1}

(* 5 *)

or

x[1] = 1; x[2] = π; x[3] = 1; x[4] = 4; x[5] = 1;

expr

(* 5 *)
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ How does expr know how to use the Format function you defined? I tried it and it works, but am not seeing how it knows to use it. $\endgroup$ – Moo Jun 16 at 13:19
  • 1
    $\begingroup$ @Moo - See Defining Output Formats (tutorial/TextualInputAndOutput#9464). "whenever the Wolfram Language is given an expression to format for output, it first calls Format[expr] to find out whether any special rules for formatting the expression have been defined. By assigning a value to Format[expr] you can therefore tell the Wolfram Language that you want a particular kind of expression to be output in a special way." $\endgroup$ – Bob Hanlon Jun 16 at 14:00
  • $\begingroup$ @BobHanlon: This seems like a nice solution. However I was looking more into subscript use when you define a function (and not during output necessarily). For e.g. for the function you define , f[x_1_, x_2_, x_3_, x_4_, x_5_] := 2 x_1 + Sin[x_2] + x_3*x_4 - x_5^-1 is my expectation. The reason for this mainly is to make my ugly expressions readable and visually better to look at. Sorry if my question was unclear $\endgroup$ – CouchPotato Jun 16 at 15:04

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