I have some codes to turn a set of equations into a matrix. Suppose the matrix Lis expressed in terms of variables $\epsilon$, $K$, $M$, and I want to make a function which takes the numerical values of $\epsilon$, $K$, $M$ and return the matrix L in numerical form. How should I do it? I know I can use replacement like L/.{M->1,epsilon->0.1,K->10}, but I think having L[0.1,10,1] will make the subsequent codes look nicer.


I generate L as follow:

I use DSolve to find the general solutions of a few vriables in terms of the constants C[1], C[2], C[3], etc. Then I define the equations I'm solving:

eq1=Simplify[...], and extract the coefficient of each underdetermined constants row1=Map[Simplify,Table[Coefficient[Substract@@eq1,C[n]],{n,1,5}]]. Finally, I construct the matrix for the linear system of equations as L={row1,row2,row3...}.

  • 2
    $\begingroup$ You may also try L1[e1_, k1_, m1_] := With[{e1=e1,k1=k1,m1=m1},L]. Might be a bit faster than approaches based on ReplaceAll. $\endgroup$ – Henrik Schumacher Dec 5 '19 at 19:44

some more code/examples would help to understand better what you need, but one (not very elegant) solution might be:

L1[e1_, k1_, m1_] := L /. {e -> e1, k -> k1, m -> m1}

For a nice solution, I'd probably look into making L a function from the start, but as you didn't provide details about how you generate/define it it is hard to help you with that.


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