# Subscripts use to make expression readable [duplicate]

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?

• You're better off using variables like x, x than subscripts, which are meant as a text formatting feature (rather than as varable names). Jun 16 '20 at 2:14
• 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. Jun 16 '20 at 2:51

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] expr // TraditionalForm Substituting values

expr /. {x -> 1, x -> π, x -> 1, x -> 4, x -> 1}

(* 5 *)


or

x = 1; x = π; x = 1; x = 4; x = 1;

expr

(* 5 *)

• 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.
– Moo
Jun 16 '20 at 13:19
• @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." Jun 16 '20 at 14:00
• @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 Jun 16 '20 at 15:04