Example as following
g[x_]:=NIntegate[......];
f[x_]:=g[x]^2+g[x];
I need to have define f[x] through g[x], Does mathmetica NIntegrate g[x] twice when calculating f[x]? How to rewrite it to reduce calculation? My idea is to give the value of g[x] at some x to a variable and use it in f[x].
value=g[x];
f[x_]=value^2+value;
The value giving should be done inside f[x_]. How to build up the function definition in mathematica in such way?
Update:
After a bit of study myself, the new code I write is:
g[x_] := NIntegate[......];
f[x_] = #^2 + # &[g[x]]
The function definition gives correct answer. Does it calculate g[x] once or twice in this way?
g[x]
twice, since you called it twice. Why not use=
instead of:=
and this way, it will integrate once? But it is better to make a complete working small example, showing the actually integration you are doing. This way, one does not have to guess. $\endgroup$With[{value = g[x]}, (*some expression with value *)]
but I'm not sure how well this may work in NIntegrate $\endgroup$