# List with n elements where n is a variable

I defined a function with n terms where n is a variable:

LogLH[μ_, σ_] = n Log[a[σ]] - Sum[Log[y[[i]]] +
(Log[y[[i]]] - μ)^2 / (2 σ^2), {i, 1, n}]


In this function, the y[[i]] are (assumed to represent) observations of a quantity - picked from a list of n elements.

This statement multiply produces the error "The expression i cannot be used as a part specification". It seems however, that M. correctly evaluates the function. M. also computes the partial derivatives for μ and σ. But the above errors repeat and lateron, I can not apply the Solve command to the resulting system of equations. Hence my question: How can I define an open ended list of n elements in M.?

• Use SetDelayed (:=) instead of Set (=). Afterwards, once you provide a proper list y and a value for n, the sum will evaluate. E.g., n = 10; y = Range@10; LogLH[m, s]. The rest of your question is uncear: what is "M."? What inequalities? What system of equations? What about the derivatives of $\mu$ and $\sigma$? – corey979 Nov 14 '16 at 11:11

First, you need to use SetDelayed (:=) in your function definition. Next, if you use a list in the step definition then the Sum will step through its elements (rather than going over an integer range).

LogLH[μ_, σ_] := n Log[a[σ]] - Sum[
Log[y] +  (Log[y] - μ)^2 / (2 σ^2),
{i, y}
]


So now when you use LogLH the y will stay as a symbol like this:

LogLH[1, 2]
(* -y (1/8 (-1 + Log[y])^2 + Log[y]) + n Log[a] *)


until you set y to a list, then i will step through the values of y:

y = {1, 2, 3};
LogLH[1, 2]
(* {-(3/8) + n Log[a], -(3/8) (-1 + Log)^2 - 3 Log +
n Log[a], -(3/8) (-1 + Log)^2 - 3 Log + n Log[a]} *)


If Solve does not work for your system of equations, you could also try NSolve or FindRoot

• Your approach eliminates the sum - which is however essential for later analytical treadment of the problem. Think e. g. of the basic problem of calculating a mean. You have n observations with values y[[i]], but for the formula you do not have these y[[i]] nor the n. – Robert Brusa Nov 14 '16 at 15:56
• Now consider s2 to be a function of yq and you want to find the derivative of it. – Robert Brusa Nov 14 '16 at 16:04

First, use SetDelayed (:=) instead of Set (=). Next, I'd propose to incorporate the list y as an argument of the function, as "y[[i]] are (...) observations of a quantity - picked from a list of n elements", hence n = Length[y]:

LogLH[μ_, σ_, y_List] :=
Length[y] Log[a[σ]] - Sum[Log[y[[i]]] + (Log[y[[i]]] - μ)^2/(2 σ^2),
{i, 1, Length[y]}]


E.g.,

LogLH[2, 0.5, Range]


-18.8866 + 5 Log[a[0.5]]

The OP didn't specify neither a[σ], nor "the resulting system of equations". About the derivatives:

D[LogLH[μ, σ, Range], μ] D[LogLH[μ, σ, Range], σ] Mathematica won't handle a Sum with an unspecified n.

• You wrote: Mathematica won't handle a Sum with an unspecified n. Yes, that's exactly what bothers me. Why not? A function which is the sum of n terms can be differenciated - even if one does not know n. – Robert Brusa Nov 16 '16 at 9:50
• See this discussion. – corey979 Nov 16 '16 at 9:59