2

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}}]

enter image description here

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!

Ulrich Neumann
  • 53,729
  • 2
  • 23
  • 55
  • 4
    (-1) 1. Please re-read this question of yours carefully: https://mathematica.stackexchange.com/q/203464/1871 See also: https://mathematica.stackexchange.com/q/285660/1871 There should be more. 2. The last question is quite different from the former two quesitons, please avoid asking distinctly different questions in one post. – xzczd Oct 25 '23 at 12:03
  • @xzczd Thanks for reminding me on this question. I don't understand your downvote. Usually a positve answer is much more helpful. – Ulrich Neumann Oct 25 '23 at 12:14
  • 3
    Because 1. I don't see enough effort in this question. As mentioned in my last comment, the first 2 questions are essentially the same question you asked 3 years ago, which suggests you haven't put enough effort in understanding the answer you've received. 2. The question doesn't meet the standard of this site. (Asking distinctly different questions in one post is enough for a closing vote. ) To sum up, this isn't a post that a 5-year member should post. – xzczd Oct 25 '23 at 12:30
  • 3
    See also: 1. https://mathematica.stackexchange.com/q/91180/1871 2. https://mathematica.stackexchange.com/a/26037/1871 And, please notice definition of galerkin cannot be cleared with galerkin =., because it's a function based on pattern matching. – xzczd Oct 25 '23 at 13:24
  • @xzczd As before, I find your comments completely dispensable. There exists many questions which additionaly ask for performance issues, no need to ask a new question. By the way only your first link is a question I posted. Which efforts I made the last years can certainly be judged from my many contributions in recent years, but surely not from one question. – Ulrich Neumann Oct 25 '23 at 14:59
  • 3
    As mentioned above, I don't see enough effort in this question, and I'm not talking about your effort in answering questions. The links are all references for you, once you've understood them (esp. the last two links), you should be able to figure out what's wrong with your code. And, please read the document of PatternTest (_?) and Condition (/;) carefully, your usage of /; just doesn't make sense. – xzczd Oct 25 '23 at 15:09
  • @xzczd The definition fkt[xi_ /; (VectorQ[#, NumericQ] & )] := xi . xi works as expected and only evaluates if argument is a numerical list! – Ulrich Neumann Oct 25 '23 at 15:18
  • 3
    I bet you haven't Clear[fkt]. As mentioned above, fkt=. won't clear the function definition. – xzczd Oct 25 '23 at 15:20

0 Answers0