# Interpolating a function of many variables

I am trying to interpolate a function of $5$ variables

$$f[m,M,k,v,w]$$

by creating a 5D table of $n$ values in each dimension.

For[im = mbegin, im <= mend, im += (mend - mbegin)/n,
For[iM = Mbegin, iM <= Mend, iM += (Mend - Mbegin)/n,
For[iv = vbegin, iv <= vend, iv += (vend - vbegin)/n,
For[iw = wbegin, iw <= wend, iw += (wend - wbegin)/n,
For[ix = xbegin, ix <= xend, ix += (xend - xbegin)/n,
Module[{ik = g[im,ix], val},
val = f[im, iM, ik, iv, iw];
AppendTo[vals, val];

AppendTo[keysm, im];
AppendTo[keysM, iM];
AppendTo[keysv, iv];
AppendTo[keysw, iw];
AppendTo[keysx, ix];
]
]
]
]
]
];


No idea how to use that next.

I have tried using Interpolation, but even with $3$ dimensions it fails:

data = Flatten[
Table[{nm, nM, nv, f[nm, nM, kbegin, nv, wbegin]}, {nm, mbegin,
mend, (mend - mbegin)/count}, {nM, Mbegin,
Mend, (Mend - Mbegin)/count}, {nv, vbegin,
vend, (vend - vbegin)/count}], 1];
f = Interpolation[data];

• – Sjoerd C. de Vries May 31 '15 at 21:47
• Okay I now got a result using FunctionInterpolation, will report back. – user1581390 May 31 '15 at 21:58
• This answer contains important considerations on the practical use of FunctionInterpolation and comparison with other function interpolation methods. – Alexey Popkov Jul 1 '15 at 11:28

I managed to interpolate it using:

f =
FunctionInterpolation[
g[m, M, k, v, w],
{m, mbegin,mend}, {M, Mbegin, Mend},
{k, kbegin, kend}, {v, vbegin,vend}, {w, wbegin, wend}];


It's not perfect though. Here is a plot of $f$ versus the original function $g$, for the variable m and the others constant:

Print[
Plot[
{f[m, Mbegin, kbegin, vbegin, wbegin],
g[m, Mbegin, kbegin, vbegin, wbegin]}, {m, mbegin, mend},
PlotRange -> Full, PlotLegends -> "Expressions"]
]; 