Solve Linear Function from Set of n Points with Mathematica

  • Mathematica
  • Thread starter radou
  • Start date
  • Tags
    Mathematica
In summary, the problem is to find a linear function which connects all the points in a set. There are two types of solutions, the one which uses fitting functions and the one which uses a total function. The first solution is easier, but the second solution is more specific and can be used in another function.
  • #1
radou
Homework Helper
3,149
8
OK, here's a problem I'm trying to solve for my work.

Given a set of n points {(x1, y1), (x2, y2), ... (xn, yn)} I need to obtain a linear function f from the broken line which connects all these points, so that I can the value of f at any point.

I need to do this for two sets of points, i.e. obtain functions f and g, because I have an expression which involves both the values of f and g at specific points.

Thanks in advance for any help.
 
Physics news on Phys.org
  • #2
Use the functions Fit and FindFit or other related functions. See help on Fit function:

mydata = {{1, 1}, {2, 4}, {3, 8.5}}

myfunction = Fit[mydata, {1, x, x^2}, x]
N[myfunction /. x -> 2.1]
Plot[myfunction, {x, 0, 10}]
 
  • #3
jackmell, thanks for the reply.

The fitting functons are not what I need, I need to obtain a linear function which directly connects these given points, and not an interpolation for this set of points. Any ideas how to do this?
 
  • #4
radou said:
jackmell, thanks for the reply.

The fitting functons are not what I need, I need to obtain a linear function which directly connects these given points, and not an interpolation for this set of points. Any ideas how to do this?

Directly as in "exactly"? Well I know you can fit n points exactly to an n-degree polynomial. Gotta' solve a system of n equations in n unknowns for the coefficients I think. Be interesting to set that up in Mathematica. I don't think it has a built-in function to do this however but not sure.
 
  • #5
radou said:
jackmell, thanks for the reply.

The fitting functons are not what I need, I need to obtain a linear function which directly connects these given points, and not an interpolation for this set of points. Any ideas how to do this?

So like a bunch of lines together? One "linear" function cannot connect all of them unless they all lie on one line.

You can construct a total function that includes those, connecting with heaviside theta functions.

ASSUMING THEY'RE ORDERED in increasing X values:
Code:
data = {{1, 1}, {2, 3}, {3, 7}, {6, 10}};
slopes = Table[(data[[i + 1]][[2]] - data[[i]][[2]])/(data[[i + 1]][[1]] - data[[i]][[1]]), {i, 1, Length[data] - 1}];
yint = Table[(data[[i]][[2]] - slopes[[i]] (data[[i]][[1]])), {i, 1, Length[slopes]}];
functions = Table[slopes[[i]] x + yint[[i]], {i, 1, Length[slopes]}];
totalfunction[x_] =  Sum[HeavisideTheta[data[[i + 1]][[1]] - x] HeavisideTheta[
    x - data[[i]][[1]]] functions[[i]], {i, 1, Length[slopes]}];
Plot[totalfunction[x], {x, 0, 4}]

is something i just whipped up, you can obviously turn everything into just one equation

Code:
fullfunction[x_] = 
 Sum[HeavisideTheta[data[[i + 1]][[1]] - x] HeavisideTheta[x - data[[i]][[1]]] ((data[[i + 1]][[2]] -  data[[i]][[2]])/(data[[i + 1]][[1]] - data[[i]][[1]]) x + (data[[i]][[2]] - slopes[[i]] (data[[i]][[1]])))  , {i, 1, Length[data] - 1}]
 
  • #6
Hepth, thanks.

When evaluating, I get error messages of type:

"Plot::plnr: totalfunction[x] is not a machine-size real number at x = \
1.6666666666666665`*^-7."

Btw, how can I get the value of this function at a point? For example, the line

"N[totalfunction[2], 2]"

returns

"6.0 HeavisideTheta[0] HeavisideTheta[
1.0] + 6.0 HeavisideTheta[-1.0] HeavisideTheta[4.0]"
 
  • #7
Ah, its because heavisidetheta isn't defined at those points. Try piecewise instead:
replace the totalfunction in the above code with this
Code:
totalfunction[x_] = 
 Piecewise[Table[{functions[[i]], data[[i]][[1]] <= x <= data[[i + 1]][[1]]}, {i, 1, Length[slopes]}]]
 
  • #8
Hm, for some reason, I still get the same error message when trying to plot the functions.

Btw:

"In[18]:=
totalfunction[3]

Out[18]=
Piecewise[{{5,False},{7,True},{7,True}}]"

Is there a way Mathematica can recognize that 3 is in the domain of the second function, and give its value at this function only?

This function will be a parameter in another function, so I only need the value of one of the piecewise it consists of, depending in which domain the value is.
 
  • #9
try resetting mathematica by
evaluation> quit kernel local (or whatever)

The FALSE, TRUE stuff means you have defined "x" somewhere. If you need to in all my code rename "x" to "X" or something.

or Clear[x]

From a clear start I have
Code:
data = {{1, 1}, {2, 3}, {3, 7}, {6, 10}};
slopes = Table[(data[[i + 1]][[2]] - 
      data[[i]][[2]])/(data[[i + 1]][[1]] - data[[i]][[1]]), {i, 1, 
    Length[data] - 1}];
yint = Table[(data[[i]][[2]] - slopes[[i]] (data[[i]][[1]])), {i, 1, 
    Length[slopes]}];
functions = Table[slopes[[i]] x + yint[[i]], {i, 1, Length[slopes]}];
totalfunction[x_] = 
 Piecewise[
  Table[{functions[[i]], 
    data[[i]][[1]] <= x <= data[[i + 1]][[1]]}, {i, 1, 
    Length[slopes]}]]
Plot[totalfunction[x], {x, 0, 4}]
totalfunction[3]
which gives
Out->
[tex]
\begin{cases}
2 x-1 & 1\leq x\leq 2 \\
4 x-5 & 2\leq x\leq 3 \\
x+4 & 3\leq x\leq 6
\end{cases}
[/tex]
and the plot
then
7
 
Last edited:
  • #10
Also, 3 is in the domain of both functions, I guess you can just change one of your less than equal to signs to less than, that way it won't be inclusive of both functions (though they're the same value), I just did it so it included the end points in each function as well, though you can prescribe it to only one if needed.:

Code:
data = {{1, 1}, {2, 3}, {3, 7}, {6, 10}};
slopes = Table[(data[[i + 1]][[2]] - 
      data[[i]][[2]])/(data[[i + 1]][[1]] - data[[i]][[1]]), {i, 1, 
    Length[data] - 1}];
yint = Table[(data[[i]][[2]] - slopes[[i]] (data[[i]][[1]])), {i, 1, 
    Length[slopes]}];
functions = Table[slopes[[i]] x + yint[[i]], {i, 1, Length[slopes]}];
totalfunction[x_] = 
 Piecewise[
  Table[{functions[[i]], 
    If[i == Length[slopes], data[[i]][[1]] <= x <= data[[i + 1]][[1]],
      data[[i]][[1]] <= x < data[[i + 1]][[1]]]}, {i, 1, 
    Length[slopes]}]]
Plot[totalfunction[x], {x, 0, 4}]
totalfunction[3]

That way the last function includes both the preceeding point as well as the final one, though every other segment only includes the leading point.
 
  • #11
I'll try this out tomorrow.

Thanks a lot for your help. :)
 

What is Mathematica?

Mathematica is a software program used for mathematical and scientific computations. It allows users to solve complex calculations, create visual representations of data, and perform various types of analyses.

How do I input a set of n points in Mathematica?

To input a set of n points in Mathematica, you can use the "List" command and enter the coordinates of each point in the form {x1,y1}, {x2,y2}, {x3,y3}, etc. Alternatively, you can use the "Table" command to generate a list of points based on a specific pattern or rule.

What is a linear function?

A linear function is a type of mathematical function that can be expressed in the form y = mx + b, where m is the slope of the line and b is the y-intercept (the point where the line crosses the y-axis).

How does Mathematica solve for a linear function from a set of points?

Mathematica uses various algorithms and methods to fit a linear function to a set of points. It first identifies the best-fitting line by minimizing the sum of the squared distances between the line and the points. It then calculates the slope and y-intercept of the line using the least squares method.

Can Mathematica solve for a linear function with non-numerical data?

Yes, Mathematica can solve for a linear function with non-numerical data. It has built-in functions for handling different types of data, such as dates, strings, and symbolic expressions. However, it is important to properly format and convert the data into numerical values before using Mathematica to solve for a linear function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
828
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
261
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
896
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • Programming and Computer Science
Replies
14
Views
635
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top