I am trying to do the the following: precomputing an expression and then making a function out of it. It has to be delayed evalution, because some of the parameters are only known when the function is called. A simplified example (the real expression is much more convoluted), is:
expr=x^2+y^2
test[y_]:=FindMinimum[expr,{x,1}]
What we get:
test[3]
FindMinimum::nrlnum: The function value {1.41421,1.41421 y} is not a list of real numbers with dimensions {2} at {x} = {1.}.
FindMinimum[expression, {x, 1}]
What I expected is
test[3]
{9,x->0}
If I remove delayed evaluation, it tries to run FindMinimum immediately and fails. Usually, Evaluate works, but here, it does not. If I both use immediate assignment = and Evaluate, it also tries minimizing before y is ready.
There numerous threads on this SE that almost work, but not for FindMinimum. I tried With, Block and others. If I missed the true answer, please point me to it.
It seems this type of converting expressions into functions should be straight forward. I usually resort to copy-pasting expressions into functions, but this is cumbersome and may lead to many mistakes when expressions are recomputed and the change must be propagated downstream.