finding function by points Mathematica - wolfram-mathematica

I have two list x and y. I combine them and receive list of point {x,y}. Using ListLinPlot I build a graphic. My question is - Can I find the function using points? I mean find formula/function f(x) if I know only points.
For an example :
{{2,5},{3,7},{7,15},{9,19}}
So the answer will be:
F(x)=2x+1
It was easy example, but my graphic is not a liner function. The blue line is my function. I changed picture that better describe the problem

See https://reference.wolfram.com/language/ref/FindFormula.html
data = {{2, 5}, {3, 7}, {7, 15}, {9, 19}};
fit = FindFormula[data, x]
1. + 2. x

Related

Algorithms and data structures for comparing things and deciding how similar they are?

What algorithms and/or data-structures that can be applied to decide how similar two things are based on some common characteristics.
What area of knowledge deals with this type of problem?
One way of doing this could be:
** Where each int value represents some characteristic
** and each set of ints represent a group of characteristics within a feature for example
Object_1: {1, 2, 3}, {11, 14}, {88, 90}
Object_2: {4, 7}, {12, 16}, {81, 91}
Search Term: {2, 90}
Search should return 'Object_1' because {2, 90} is subset of {1, 2, 3, 11, 14, 88, 90}
Hope this example narrows the question down a bit.
There are many different types of similarity measures out there. To decide which one to use the first step is to specify your level of measurement and type of data. Here are a few similarity measures for categorical and cont data:
For categorical data:
Hamming distance
Sokal-Michener
Russel–Rao
For continuous data:
Minkowski-based distances e.g Euclidean distance, Manhattan distance
Mahalanobis distance
The general algorithm that is used for similarity-based learning is Nearest Neighbor algorithm. For more information you can refer to John.D Kelleher's book.

How do I apply Map[] to a function using two arguments in Mathematica?

In general, I was trying to compute the norm of the difference between every set two elements in a list which looks something like
X = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}}
therefore evaluating
Norm[X[[1]]-X[[2]]]
Norm[X[[1]]-X[[3]]]
Norm[X[[2]]-X[[3]]]
Now, applying Outer[] is one possible way how to do this
Outer[Norm[X[[#1]] - X[[#2]]] &, {1,2,3}, {1,2,3}]
but unfortunately it results in a quite slow code if I increase the number of elements in X and the length of each element.
Is there any possible way to construct a Map[] operation? Something like
MapThread[Norm[X[[#1]] - X[[#2]]] &,{{1,2,3},{1,2,3}}]
does not work give the desired "currying" which I was looking for.
I'm using Mathematica Version 11.2.0.0, so I don't have access to Curry[].
Would be happy about any advice!
mX = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}
Apply[Norm#*Subtract, #] & /# Subsets[mX, {2}]
Two equivalent approaches:
(Norm#*Subtract) ### Subsets[mX, {2}]
Apply[Norm#*Subtract, Subsets[mX, {2}], {1}]

MiniMaxApproximation not working in Mathematica 7

I'm new to Mathematica and I'm trying to obtain a minimax rational function approximation to a certain expression. In particular, I'm using
mma = MiniMaxApproximation[x^2, {x, {8, 10}, 2, 2}]
Unfortunately, Mathematica 7 replies with the same expression I'm trying to calculate, namely
MiniMaxApproximation[x^2, {x, {8, 10}, 2, 2}]
Of course, I'm aware this is a very simple test, since x^2 is the rational function approximation of itself.
I'm also trying other possibilities like RationalInterpolation, EconomizedRationalApproximation etc., but none is working. Only PadeApproximant returns a result.
Anyone has an idea on why this happens?
Thank you very much in advance.
likely your first problem is that you havent loaded the function approximation package,
start a new kernel and try this:
Needs["FunctionApproximations`"]
mma = MiniMaxApproximation[Exp[x], {x, {0, 1}, 1, 2}]
Your specific example (x^2) throws a slew of (to me) nonsensical errors. I guess MiniMaxApproximation is not robust in handling trivial degenerate cases.
evidently requesting a numerator equal to the order for your expression is causing the error:
a[x_] = MiniMaxApproximation[x^2, {x, {8, 10}, 1, 2}][[2, 1]]
Plot[ {a[x] , x^2}, {x, -10, 30}]

How to form a BSpline function from a 3DS/OBJ import in Mathematica

Here is an example 3D geometry.
dat=Import["ExampleData/747.3ds.gz", ImageSize -> Medium]
Now if one wants to get a BSplineFunction for this 3D geometry what is the easiest way to do it?
I can see the parts in Mathematica using the following command.
parts = Length[(dat // First // Last)];
and here comes the 3D points after extraction.
ListPointPlot3D[Flatten[Map[((dat // First // Last)[[#]] /.
GraphicsComplex[a_, b_] -> List[a]) &, Range[parts]], 1]]
I hope there is a general method so that we can form a BSpline function from any 3D graphics complex.
I suppose the general method will be able to convert Mathematica 3D representations in continuous BSplines representation.
Now we will elaborate according to the example given by belisarius.
v={{0,0,0},{2,0,0},{2,2,0},{0,2,0},{1,1,2}};
i={{1,2,5},{2,3,5},{3,4,5},{4,1,5}};
Graphics3D[{Opacity[.5],GraphicsComplex[v,Polygon[i]]}]
We can simply form the input for the BSpline surface for this example.
dat = Table[Map[v[[#]] &, i[[j]]], {j, 1, Length[i]}];
Now let's see the surface that comes out if we consider the underlying vertices.
Show[
(* Vertices *)
ListPointPlot3D[v,PlotStyle->{{Black,PointSize[.03]}}],
(* The 3D solid *)
Graphics3D[{Opacity[.4],GraphicsComplex[v,Polygon[i]]}],
(* The BSpline surface *)
Graphics3D[{Opacity[.9],FaceForm[Red,Yellow],
BSplineSurface[dat, SplineDegree-> {1,2},SplineClosed->{True,False}]}
],
Boxed-> False,Axes-> None
]
Once this surface is formed I thought it will be possible to make a BSplineFunction in some way. But what I get is completely different from the above surface.
func = BSplineFunction[dat, SplineDegree -> {1, 2},SplineClosed -> {True, False}];
Plot3D[func[x, y], {x, 0, 1}, {y, 0, 1}, Mesh -> None,PlotRange -> All]
So am I making some conceptual mistake here?
I think your question needs further clarification.
The .3DS are mainly Polygon sets like this one:
v = {{0, 0, 0}, {2, 0, 0}, {2, 2, 0}, {0, 2, 0}, {1, 1, 2}};
i = {{1, 2, 5}, {2, 3, 5}, {3, 4, 5}, {4, 1, 5}};
Graphics3D[{Opacity[.5], GraphicsComplex[v, Polygon[i]]}]
So, it is not obvious how to get Spline surfaces to model this.
Perhaps you can elaborate a little with this example.
HTH!
Minor detail: Your spline is a bit warped and that's because of your choice of SplineDegree. For the pyramid case I'd choose {2,1} instead of {1,2}.
That will give you a cone instead of the soft-ice cone you now have. Of course, that's all rather arbitrary and beauty is in the eye of the beholder.
Now for your question why a 3D plot of the BSplineFunction doesn't give the same results as a Graphics3D of a BSplineSurface with the same control points. The problem is that you assume that the two parameters in the BSplineFunction correspond to x and y of a Cartesian coordinate system. Well, they don't. Those parameters are part of an internal parametric description of the surface, in which varying these two parameters yields a set of 3D points, so you have to use ParametricPlot3D here.
So, if you change your Plot3D into ParametricPlot3D you'll see all is fine.
I hope this answers you final question. Does this also answer your question how to convert a 3D polygon based model to a spline based model? One of the problems you face is that a spline doesn't usually go through its control points, as a kind of interpolating function.

How to reshape matrices in Mathematica

When manipulating matrices it is often convenient to change their shape. For instance, to turn an N x M sized matrix into a vector of length N X M. In MATLAB a reshape function exists:
RESHAPE(X,M,N) returns the M-by-N matrix whose elements are taken columnwise from X. An error results if X does not have M*N elements.
In the case of converting between a matrix and vector I can use the Mathematica function Flatten which takes advantage of Mathematica's nested list representation for matrices. As a quick example, suppose I have a matrix X:
With Flatten[X] I can get the vector {1,2,3,...,16}. But what would be far more useful is something akin to applying Matlab's reshape(X,2,8) which would result in the following Matrix:
This would allow creation of arbitrary matrices as long as the dimensions equal N*M. As far as I can tell, there isn't anything built in which makes me wonder if someone hasn't coded up a Reshape function of their own.
Reshape[mtx_, _, n_] := Partition[Flatten[mtx], n]
ArrayReshape does exactly that.
Reshape[list_, dimensions_] :=
First[Fold[Partition[#1, #2] &, Flatten[list], Reverse[dimensions]]]
Example Usage:
In: Reshape[{1,2,3,4,5,6},{2,3}]
Out: {{1,2,3},{4,5,6}}
This works with arrays of arbitrary depth.
I know this is an old thread but for the sake of the archives and google searches I've got a more general way that allows a length m*n*... list to be turned into an m*n*... array:
Reshape[list_, shape__] := Module[{i = 1},
NestWhile[Partition[#, shape[[i]]] &, list, ++i <= Length[shape] &]
]
Eg:
In:= Reshape[Range[8], {2, 2, 2}]
Out:= {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}
There is now also a new function ArrayReshape[].
Example:
{{1, 2, 3}, {4, 5, 6}} // MatrixForm
ArrayReshape[{{1, 2, 3}, {4, 5, 6}}, {3, 2}] // MatrixForm

Resources