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]