0
$\begingroup$

I am trying to build an expression by first writing it out as a string and then using ToExpression to convert it to an expression. However, I cannot figure out how to make expression recognize x as an argument. Here is my code building the expression:

functionbuilder := Module[{}, 
   functionstring = "B1";
   nosines = Input["How many sine curves?"];
   For[n = 2, n <= 3*nosines - 1, n = n + 3, 
      string=StringJoin["+B", ToString[n], "*Sin[B", ToString[n + 1],"*x+B",ToString[n+2],"]"];
      functionstring = StringJoin[functionstring, string]
   ];
   Return[ToExpression[functionstring]]
]

This returns exactly the expression I want. For example, when nosines=3, the output is:

B1 + B2 Sin[B4 + B3 x] + B5 Sin[B7 + B6 x] + B8 Sin[B10 + B9 x]

All I am looking for is how define this expression as a function with x as an argument. I have tried:

f[x_]:=functionbuilder[x]

But this does not work. What syntax do I need to define a new function f[x] from my expression? To be clear, I want to be able to evaluate the function f[2.] and get:

B1 + B2 Sin[2. B3 + B4] + B5 Sin[2. B6 + B7] + B8 Sin[B10 + 2. B9]
$\endgroup$

1 Answer 1

2
$\begingroup$

You can use Symbol[] to sidestep all that rigamarole with ToExpression[]:

With[{n = 3}, 
     B1 + Sum[Symbol["B" <> IntegerString[k - 1]]
              Sin[Symbol["B" <> IntegerString[k]] x +
                  Symbol["B" <> IntegerString[k + 1]]], {k, 3, 3 n, 3}]]
   B1 + B2 Sin[B4 + B3 x] + B5 Sin[B7 + B6 x] + B8 Sin[B10 + B9 x]

However, I see no justification for needing to generate so many symbols. One can just input a list of parameters if an undetermined number of them is needed, and just let the listability of the arithmetic operators and elementary functions take care of it:

1 + {2, 5, 8}.Sin[{3, 6, 9} x + {4, 7, 10}]
   1 + 2 Sin[4 + 3 x] + 5 Sin[7 + 6 x] + 8 Sin[10 + 9 x]
$\endgroup$
5
  • $\begingroup$ Thank you! That should work great for me. Just FYI, I need all of those parameters to remain unknowns so that I can take partial derivatives with respect to each of them. $\endgroup$
    – ahle6481
    Commented Feb 23, 2016 at 18:07
  • $\begingroup$ I see. Is this for a nonlinear fit, or something else? $\endgroup$ Commented Feb 23, 2016 at 18:13
  • $\begingroup$ Yes, I am going to use this to build a Jacobian matrix for least-squares fitting. $\endgroup$
    – ahle6481
    Commented Feb 23, 2016 at 18:15
  • $\begingroup$ You do know that there is the built-in function FindFit[], no? $\endgroup$ Commented Feb 23, 2016 at 18:17
  • $\begingroup$ Yes I am familiar with that. I am just writing my own fitting function to recreate a very specific algorithm that another person wrote so that I can verify their results. $\endgroup$
    – ahle6481
    Commented Feb 23, 2016 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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