# defining a function using other functions

I have the following code:

c[theta_, xi_] = theta*xi + theta^xi^2;
xbar[theta_, xi_] = theta/xi +  xi^2 + theta^3;
f[x_, y_, theta_, xi_] = x*y*c[theta, xi]*theta*xi + xbar[theta, xi];


That is, I would like to define $f$ as a function $x,y,theta,xi$ using two predefined functions $c$ and $xbar$. But mathematica gives tag times is protected. How to deal with it? Many thanks!

• Evaluate Remove[c, xbar, f] before the function definitions. – Coolwater Jan 30 '16 at 21:00

You need to make sure there are no active definitions on the constituent parameters. You can do this with ClearAll as E.Doroskevic proposes, though I imagine you will not want to clear all Global definitions.

You can Block the Symbols in use as described in Scoping in assigning a derivative. To make this convenient I propose using the function localSet which I wrote for How to make a function like Set, but with a Block construct for the pattern names.

xi = "fail!";

localSet[c[theta_, xi_], theta*xi + theta^xi^2]
localSet[xbar[theta_, xi_], theta/xi + xi^2 + theta^3]
localSet[f[x_, y_, theta_, xi_], x*y*c[theta, xi]*theta*xi + xbar[theta, xi]]

?f


Globalf

f[x_, y_, theta_, xi_] = theta^3 + theta/xi + xi^2 + theta x xi (theta^xi^2 + theta xi) y

Observe that the definition is correct despite the definitions on xi and y.

Try:

ClearAll["Global*"]

c[theta_, xi_] = theta*xi + theta^xi^2;
xbar[theta_, xi_] = theta/xi +  xi^2 + theta^3;
f[x_, y_, theta_, xi_] = x*y*c[theta, xi]*theta*xi + xbar[theta, xi];
`