New answers tagged

3

Assuming that data is obtained from translated and/or rotated models Clear[a, c] p = {x, y, z}; P = {X, Y, Z}; Txyz = z^2 - a^2 + (c - (x^2 + y^2)^(1/2))^2 /. Thread[p -> RollPitchYawMatrix[{alpha, beta, gamma}].(P - {x0, y0, z0})]; now using the excellent script from @flinty for random data generation SeedRandom[1]; Torus = ResourceFunction["Torus&...


1

Modifying simulate to use Piecewise results in giving just a single value (rather than currently a list) seems to fix things: vec = {1, 3}; simulate[a_, b_, i_] := Piecewise[{{vec.{{2*a, b}, {b, a}}.vec, i == 1}, {vec.{{a^2, b^2}, {a^2, b^2}}.vec, i == 2}, {vec.{{b, a}, {a, b}}.vec, i == 3}, {vec.{{a^2 - b^2, b^2}, {a, b}}.vec, i == 4}}] data = {{1,...


2

Shape fitting is quite difficult and has robustness issues without taking special care, but you can get quite far without any fitting. If your torus data is not rotated and oriented directly up, then the BoundingRegion commands provide very good parameters: SeedRandom[1]; Torus = ResourceFunction["Torus"]; testTorus = Torus[{4, -2, 6}, {19, 4}]; (* ...


1

You can ask NDSolve to return the requested quantity straight away like this: model[c1_?NumericQ, c2_?NumericQ, k_?NumericQ] := NDSolveValue[ {g'[x] + (f[c1, c2, x]/k) Sin[k x + g[x]]^2 == 0, g[0] == 0}, g[15/c2], {x, 15/c2} ]; model[1, 2, 3] 0.313396 As you can see, this will return the value of g at the point 15/c2 instead of the full interpolation ...


0

This works for me: (* note I needed to add a Piecewise, because HeavsideTheta is non-numeric at zero *) f[c1_, c2_, x_] := c1^2 (1 - x c2) Piecewise[{{1, c2 - x > 0}}, 0] sol = g /. ParametricNDSolve[{g'[x] + (f[c1, c2, x]/k) Sin[k x + g[x]]^2 == 0, g[0] == 0}, g, {x, 15/c2}, {c1, c2, k}]; SeedRandom[1]; data = Table[{k, 2 k^2 - RandomReal[{-2, 2}]}, ...


2

Is this what you want? data = {{0.002, 2.51053}, {0.004, 2.54217}, {0.006, 2.55543}, {0.008, 2.54247}}; lm = NonlinearModelFit[data, a x + b, {a, b}, x] bands[x_] = lm["MeanPredictionBands", ConfidenceLevel -> .95] Show[ListPlot[data], Plot[{lm[x], bands[x]}, {x, 0, 0.01}, Filling -> {2 -> {1}}], Frame -> True, Axes -> False, ...


2

For simple applications of regression, there is no need to fit multiple dependent variables simultaneously. The results are the same as a regression of each dependent variable separately. There are caveats if you are doing further analysis, but if you just want the basic regression results, you can do separate fits.


8

You can build the regression yourself as an NMinimize of residuals which are squared distances to points. First let's build some synthetic noisy data: (* create some noisy data that follows a linear model *) n = 1000; datax = RandomReal[{-1, 1}, {n, 2}]; testmtx = {{3, 4}, {1/2, 1/6}}; testoffset = {3/2, 5/7}; fn[{x1_, x2_}] := testmtx.{x1, x2} + testoffset ...


11

LinearModelFit doesn't do multivariate regression as far as I'm aware. You can use my repository function BayesianLinearRegression instead. The first example in the "Scope" section shows you how. You can provide the data in the format data[[All, {1, 2}]] -> data[[All, {3, 4}]] or #[[{1, 2}]] -> #[[{3, 4}]]& /@ data For example: fitData =...


1

data = {{4.88281 10^(-5), 0.0788}, {9.76563 10^(-5), 0.1014}, {0.000195313, 0.12455}, {0.000390625, 0.15594}, {0.0015625, 0.18587}}; lm = LinearModelFit[data, Log[x], x] Show[ListLogLinearPlot[data], LogLinearPlot[lm[x], {x, Min[data[[All, 1]]], Max[data[[All, 1]]]}]] Show[ListPlot[data], Plot[lm[x], {x, 0, Max[data[[All, 1]]]}, PlotRange -> All]]


0

I have been working at this problem and have used RecurrenceTable to avoid the precision issues. It seems to work. The other concern is calculating the roots of a very large polynomial. In the example below I calculate the roots of a polynomial of order 1499. It seems to work! Here is a module I have constructed for approximating a time history of the form ...


1

I am not certain why there is a hiccup with the fitting, generally both FindFit and NonlinearModelFit are quite reliable if you set your conditions right. I found that the issue resides with the "or" conditionals. If you request only one of the 'or' conditions (j - Abs[l - s] == 0) then you can get your answer. The other conditions don't seem to ...


7

I'm not sure why FindFit can't manage it, but if I construct the objective function myself to minimize square residuals and I use Method -> "DifferentialEvolution" then I get the answer: objective = Total[(#[[2]] - b[#[[1]]])^2 & /@ data]; fit = Last[NMinimize[{objective, conds}, {l, j, s}, Method -> "DifferentialEvolution"]]/...


4

This is a bit slow but you could construct the fit yourself as a minimization of square residuals. This gives me: {A -> 18.9346, w -> 0.768869, xc -> 1583.96, ϕ -> -0.632702} dx = 0.01; resl = 0.15; vX = Table[i, {i, 1582, 1586, dx}]; L = Length[vX]; vX2 = PadRight[vX, 2 L - 1]; Length[vX2]; c = 2.99792458*10^5; f[x_, y0_, A_, w_, xc_, ϕ_] := ...


10

Use Quantile Regression: Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicQuantileRegression.m"] QRMonUnit[data]⟹ QRMonQuantileRegressionFit[{1, x}]⟹ QRMonLeastSquaresFit[{1, x}]⟹ QRMonPlot; (And, yes, that is a good example of Quantile Regression's robustness.) Update Instead ...


9

You can also try Theil–Sen which is less sensitive to outliers. Using the WL implementation from this answer on your data gives slope, intercept of {0.0037716, 1.07855}. Plot of your data and a line with that slope, intercept.


14

Maybe you could use RANSAC to find inliers by consensus. This implementation isn't exactly right but it finds a pretty decent fit: samplesize = 30; inliers[fit_, points_, d_] := Select[points, Abs[#[[2]] - (fit /. x -> #[[1]])] < d &] votes = Association[# -> 0 & /@ data]; Do[ sample = RandomSample[data, samplesize]; fit = Fit[sample, {...


23

Use PlotRange -> All. Most plot functions tend to throw away points that aren't nicely clustered with the bulk: Show[{ListPlot[data, PlotRange -> All], Plot[ab, {x, 0, 2}, PlotStyle -> Red]}] As you can see, there is a number of points that completely mess up the fit.


4

p = Predict[x -> y, Method -> "LinearRegression"] In versions 12.+, you can use the functions Information or PredictorInformation: Information[p, "Function"] 2.32989 + 2.1804 #1 & PredictorInformation[p, "Function"] 2.32989 + 2.1804 #1 & In version 11.0, you can use PredictorInformation PredictorInformation[p, ...


6

I implemented the algorithm from the Wikipedia page. I hope it can be of help. I decided to write it down as a sort of guide, but read on Wikipedia for the details and for ways to further tune it. A SOM has two types of data, weights and units. The weights are data points, we use the following: img = Import["https://i.stack.imgur.com/eoAEt.png"] // ...


5

Your data really does not only resemble a Lorentzian. There are definitely background perturbing functions there. Adding two terms, one linear and another cubic corrects for a lot though. Here the code with your model as well as a real, scaled Lorentzian: fit = NonlinearModelFit[data, A*PDF[CauchyDistribution[x0, b], x] + A0 + c*x + d*x^3, {A, {b, 0....


3

My idea here is to use a GradientFilter on the accumulated signal and spot the maxima using MaxDetect. This creates a run of 1's when the signal gains momentum in a straight line towards the peaks, and we can find positions of those 1's and split when there's a jump in the position > 1. acmg = Rescale[GradientFilter[Accumulate@signal, 1]]; mdacmg = ...


2

Looks like Piecewise can help here. above = {{-300, 2.1977}, {-600, 2.61518}, {-900, 5.48259}}; below = {{-900, 5.48259}, {-1200, 4.34585}, {-1500, 3.24366}, {-1800, 2.57632}, {-2100, 2.14541}, {-2400, 1.85425}, {-2700, 1.6508}, {-3000, 1.50487}, {-3300, 1.39795}, {-3600, 1.31817}, {-3900, 1.25768}, {-4200, 1.21115}, {-4500, 1.17488}, {-4800, 1....


3

f = Interpolation[data] Show[ListPlot[data, PlotStyle -> Red], Plot[f[x], {x, -6000, -300}]] The result shows that the data in dense enough to the define a nice interolating function between -6000 and -1000. The three data point with highest x-value are not dense enough to do so. The Interpolation works by fitting polynomial curves between successive ...


4

FindFormula Maybe you should try FindFormula before trying FindFit... SeedRandom[45] fs = FindFormula[data, x, 5, PerformanceGoal -> "Speed"] Function[{f}, ListPlot[{data, Transpose[{data[[All, 1]], f /. x -> # & /@ data[[All, 1]]}]}, Joined -> {False, True}]] /@ fs ResourceFunction["QuantileRegression"] Better ...


7

The problem is that you take arbitrary exponents of negative numbers which leads to complex numbers. A model very similar to yours would be (with an additional parameter) modeltwolevelsat = b/Abs[x]^a; eq = FindFit[data, modeltwolevelsat, {a, b}, x] (* {a -> 0.33341, b -> 26.156} *) The fit is not too good, though Show[ ListPlot[data, PlotStyle -> ...


1

You can do Fourier analysis and decide, which Sin and Cos frequences over a choosen threshold you want to take into account and see the effect on fit. (Thanks to the ideas from other contributors!) fou = Fourier[data[[All, 2]]]; tabc = Table[Cos[i x], {i, 1, Round[Length[data]/2]}]; tabs = Table[Sin[i x], {i, 1, Round[Length[data]/2]}]; Manipulate[ lp1 =...


5

Mikado's comment to the original question is on the money: this is a linear model; not a non-linear one. This makes the fit much easier and faster with LinearModelFit, since you don't have to worry about local minima. With linear models you normally don't have to fudge around nearly as much: basisFun = { 1, Cos[x], Cos[2 x], Cos[3 x], Cos[4 x], Cos[5 x], ...


2

If you're going to stick with pairs of sines and cosines, then using AICc is a reasonable approach to determine an appropriate number of pairs. Rewriting your code to allow for any number of pairs of sines and cosines: data = {{0., 4852.}, {0.0872665, 3128.}, {0.174533, 2686.}, {0.261799, 6450.}, {0.349066, 15532.}, {0.436332, 28730.}, {0.523599, 42838.}, {0....


Top 50 recent answers are included