Hot answers tagged

14

This is a good example of why one should never blindly trust the numerical results of systems like Mathematica, without thinking about numerical methods that these systems use. Mathematica won't ever make numerical analysis courses obsolete. Most interpolation methods use piecewise polynomials, and assume slowly varying smooth functions. Your data has ...


5

I do not know how to extract that information from an InterpolatingFunction object, but perhaps you could make your own Piecewise function using InterpolatingPolynomial: pts = RandomReal[{-10, 10}, {10, 2}]; piecewise = Piecewise[ {InterpolatingPolynomial[#, x], #[[1, 1]] <= x < #[[2, 1]]} & /@ Partition[SortBy[First][pts], 2, 1] ] ...


4

Here is a partial solution. Partial, because it seems to me that the mesh and the solution do not match. The mphtxt file is a structured representation of a mesh. This file is made up of several sections and comments which start with a # token. There is a coordinates section and then several element sections. To find the starting position of these element ...


4

I couldn't figure out a quick way to import the data how you had your paste formatted (with curly brackets for each element, but no outer curly brackets) so I reformatted it and repasted it. data = Import["http://pastebin.com/raw/V8807EsY", "Table"]; You say you'd like to average the duplicate points, so using Mean in combination with GatherBy should ...


4

Basing on the advice of @Szabolcs to whom I am very grateful, I think I can offer a workaround. First I create in Mma the domain in which the equation has been solved in Comsol: dom = DiscretizeGraphics[Graphics[Polygon[{{0, 0}, {0, 0.018}, {0.155, 0.018}, {0.155 + 0.015, 0.008}, {0.222, 0.008}, {0.222, 0}, {0,0}}]]] looking as follows: Then ...


3

You're experiencing the typical and, in the simple example, expected limitations of searching for roots. The two FindRoot results are easily understood in terms of Newton's method. The best way to proceed, assuming given the example is typical, is to use WhenEvent. sol = NDSolve[{y''[t] == -y[t], y[0] == 1, y'[0] == 0, WhenEvent[y[t] == 0, firstzero = ...


3

Partial derivatives should work on interpolation functions just fine, and I cannot see why your interpolating function should be any different. But it is. Here is a ridiculous workaround ufem2 = Interpolation[ Flatten[Table[{{x, t}, ufem[x, t]}, {x, 0, 10, .1}, {t, 0, 1, .02}], 1]]; loc2 = Derivative[1, 0][ufem2]; Plot[{ufem[x, .2], loc2[x, ...


3

SeedRandom[1]; pts = RandomReal[{-10, 10}, {10, 2}]; Clear[if, ip] If the InterpolationOrder is set to Length[pts] - 1 then the InterpolatingFunction is the InterpolatingPolynomial if[x_] = Interpolation[pts, InterpolationOrder -> Length[pts] - 1][x]; ip[x_] = InterpolatingPolynomial[pts, x] // HornerForm (* -18.7844 + x (6.55948 + x ...


2

This one is much faster then Plot (but the use depends on your quality needs) Graphics[Line@Transpose[{#, f[#] + g[#]}]] & @ Range[1.2, 10, 0.01]; Show[ ListLinePlot[{Transpose[list], Transpose[list2]}, PlotRange -> {0, 30}], Graphics[Line@Transpose[{#, f[#] + g[#]}]] &@Range[1.2, 10, 0.1] ] Some note on time Plot[f[x] + g[x], {x, ...


2

You can just multiply the random numbers by a windowing function that does go to zero in the way you want. One choice is a super-Gaussian, it's like a smooth version of a square windowing function (with n=6 below, but you can choose other values Plot[Exp[-(x/120)^6], {x, -210, 210}, PlotRange -> {0, 1}] Here is the initial data, bounds = 200; width ...


2

Notice that NConvolve is blue, this means it isn't one of the pre-defined functions. You have to define the function yourself (or just grab Andrew Moylan's version here) NConvolve[f_, g_, x_, y_?NumericQ] := NIntegrate[f (g /. x -> y - x), {x, -Infinity, Infinity}] Which makes the plot work just fine Ol3v22[y_?NumericQ] := NConvolve[Sd[t], ...


1

In a simplified 1D version my idea may look as follows. Here are two lists of the amplitudes, that I limited by 10 terms: lst1 = RandomReal[{-1, 1}, 10]; lst2 = RandomReal[{-1, 1}, 10]; Here are the arbitrary functions defined as the Fourier-polynomials with the above amplitudes: y1[x_] := Sum[lst1[[i]]*Sin[x*i], {i, 1, Length[lst1]}] y2[x_] := ...


1

bounds = 200; f[{x_, y_}] := CDF[GammaDistribution[4, 2], 15 Rescale[ Min@Outer[Abs[Subtract@##] &, {x, y}, {bounds, -bounds}], {0, bounds}, {0, 10}]]/2 // N func = Interpolation@Flatten[Table[{{x, y}, RandomReal[{-#, +#}] &@ f[{x, y}]}, {x, -bounds, +bounds}, {y, -bounds, +bounds}], 1]; DensityPlot[func[x, y], ...


1

With ffd[x_,y_,z_]:= D[ff[x,y,z],{{x,y,z}}] the values of x, y, and z are substituted as arguments causing differentation wrt. numbers, i.e. nonsense. Moreover, you are using SetDelayed, which differentiate once for every call, which rather should be once for all time. The solution to both problem is replacing SetDelayed with Set: ffd[x_,y_,z_]= ...


1

Use GatherBy to gather all points with the same abscissa coordinates and then take the Mean of all those points. Interpolation[ Map[ {#[[1, 1]], Mean[#[[All, 2]]]} &, GatherBy[{{#1, #2}, #3} & @@@ data, First] ] , InterpolationOrder -> 1 ]


1

From the documentation Interpolation expects multivariate data in the format { {{x1,y1,z1}, f1}, {{x2,y2,z2}, f2}, {{x3,y3,z3}, f3} } So you will need to first import the data data = Import[filepath, "Table"] then format it in the right way Map[{Most[#], Last[#]} &, data] { {{0.706, 0.688, 0.104}, 0.798}, {{0.001, 0.291, 0.744}, ...



Only top voted, non community-wiki answers of a minimum length are eligible