modified
Here I try to realize a numerical fit (Galerkin method) using parametrized Interpolation function to fit examplary Exp[x] in a given grid. So far I tried
galerkin =.
galerkin[xii_List (* Grid*)] :=
Block[{yi = Array[y, Length[xii]], ipB, mini, J}
,
ipB = Function[{yi},Interpolation[Transpose[{Rationalize[xii], yi}]]] ;
J = Function[yi,NIntegrate[(ipB[yi][x] - Exp[x])^2, {x, Min[xii], Max[xii]},Method -> { "InterpolationPointsSubdivision", SymbolicProcessing -> 0} ] ];
mini = NMinimize[J[yi], yi] // Quiet (Fehlermeldung ignorierbar) ;
ipB[yi /. mini [[2]] ]
]
Function galerkin returns the optimal Interpolation and works quite well (unfortunately a little slow)
grid=Subdivide[0, 1, 10]
Plot[Evaluate[{Exp[x], galerkin[grid][x]}], {x, Min[grid], Max[grid]},PlotStyle -> {Automatic, {Black, Dashed}}]
In this version I need Quiet command, probably because the definition of J isn't restricted to numerical input.
I tried without success J = Function[yi,NIntegrate[(ipB[yi][x] - Exp[x])^2, {x, Min[xii], Max[xii]},Method -> { "InterpolationPointsSubdivision", SymbolicProcessing -> 0} ] /; (VectorQ[#, NumericQ] &)]
@xzczd stated in his comment , this form isn't allowed.
Alternatively I tried without success
J[yi_ /; (VectorQ[#, NumericQ] & )] :=
NIntegrate[(ipB[yi][x] - Exp[x])^2, {x, Min[xii], Max[xii]},
Method -> { "InterpolationPointsSubdivision",SymbolicProcessing -> 0} ]
My questions
- What's wrong in my definition
J? - Correct definition of
J? - How could I improve the speed of program
galerkin?
Thanks!

galerkincannot be cleared withgalerkin =., because it's a function based on pattern matching. – xzczd Oct 25 '23 at 13:24PatternTest(_?) andCondition(/;) carefully, your usage of/;just doesn't make sense. – xzczd Oct 25 '23 at 15:09fkt[xi_ /; (VectorQ[#, NumericQ] & )] := xi . xiworks as expected and only evaluates if argument is a numerical list! – Ulrich Neumann Oct 25 '23 at 15:18Clear[fkt]. As mentioned above,fkt=.won't clear the function definition. – xzczd Oct 25 '23 at 15:20