Lagrange polynomial: Unexpected interpolation results - math

I am trying to interpolate a series of data points using 2nd lagrange polinomial.
having
point1:(5;100)
point2: (9;17)
point3: (12;17)
and the formula
y=(x-x2)*(x-x3)/(x1-x2)*(x1-x3)*y1+
(x-x1)*(x-x3)/(x2-x1)*(x2-x3)*y2+
(x-x1)*(x-x2)/(x3-x1)*(x3-x2)*y3
It is obvious that a quadratic function might not fit the data.. It is just an example.
But i wonder why the value is surprisingly high for x=7.
If i am not wrong its y=1500.
Is the above formula correct?

answer:
In summary:
For the same x, you can't have two different y values; this violates the definition of a function.
you are missing brackets in your formula! Not (x-x2)*(x-x3)/(x1-x2)*(x1-x3), but ((x-x2)*(x-x3)) / ((x1-x2)*(x1-x3)).
back to 1>, note that the interpolation formula has x3-x2 in the denominator. If you have tied values, you will be dividing 0.
How can you make interpolation on such small data set? Yet you are asking for a quadratic interpolation!
follow-up:
1) fixed it. Accidentally i switched all the x and y values. So the points were in format (y,x).
Ah, haha, no wonder.
2) Thank you! The brackets improved the approximation. Regarding the missing brackets: I got the formula from the accepted answer here: Best way to find Quadratic Regression Curve in Java, but I don't understand this rule.
This is the famous, yet fundamental interpolation: Lagrange interpolation. You can verify its formula at Wolfram mathworld: Lagrange Interpolating Polynomial. I don't give you wikipedia link because this one looks more beautiful.
The link you found must contain a typo. Since you have suggest an edit to fix that, hopefully it will soon get approved.
3) It is a (significant larger (which answers your 4th question) time series. So it is impossible to have tied values.
Yes, time series won't have tied values.

formula should be correct.but when x=17,you have two different y value,it's might the cause of the trouble.you can try change anthor x.

Related

How to draw "non-function" graph: f(x) = g(y)

I want to know is there any fast way to draw a graph of a "non-function" curve. For example
x^2+3x = y^3-4y+1
I know for normal function, like y=x^2, we can iterate x and calculate y, then draw the points. But for non-function curve, it will take a lot of times to iterate x, then solve function of y (using Newton method or alike). So please suggest me the correct way to draw them.
Thanks & Regards.
I am afraid there is no "generic" way except for the method you describe yourself: iterate over one variable and solve for the other.
Complications
Note that you have to be careful to find all solutions, not just a solution. This is a major stumbling block in creating a working general algorithm.
Another stumbling block is the singularity points: when f'(x)=0, you will want to solve for y and, vice versa, when g'(y)=0, you will want to solve for x. What if both are 0 at the same time? You will need to do some paper-and-pencil analysis.
Special Cases
There are some problem-specific simplifications though.
In your specific case the equation for x is quadratic, so a well known simple closed formula exists. This means that iterating over y and solving for x is easier. (The equation for y is cubic, so a less well known and much more complicated formula exist too).
Another way is to find a parametric representation of your curve (e.g., x^2+y^2=1 is equivalent to x=cos(t); y=sin(t); 0<=t<2*pi).

How to calculate first derivative of time series

I would calculate the first derivative (dpH/dtime) of time series using two variables, time and pH.
Are there any kind of functions to do this in R or should I compute an extra function to do this?
Assuming pH and time are plain vectors try this:
library(pspline)
predict(sm.spline(time, pH), time, 1)
You might want to start with stats::deriv or diff.ts as Matt L suggested. Just keep in mind what a professor of mine used to tell all his students: numeric differentiation is known as "error multiplier."
EDIT:
To clarify -- what he was warning about was that any noise in your data can throw the derivative estimate way off. It's been said that integration is a low-pass filter and differentiation is a high-pass filter.
So, the important thing is to do some smoothing on your data before calculating a derivative. Hence Gabor's excellent suggestion to use predict.spline . But keep in mind that modifying the spline parameters will smooth your data to different levels, so always look at the results to make sure you removed apparent noise but not desired features.
Here's a link to "Numerical Differentiation".
http://en.wikipedia.org/wiki/Numerical_differentiation
Here's a link describing a method based on Taylor Series Expansions:
http://ocw.usu.edu/civil_and_environmental_engineering/numerical_methods_in_civil_engineering/ODEsMatlab.pdf

Indefinite Integral in R

I am looking to calculate the indefinite integral of an equation.
I have data from an accelerometer feed into R through a visual C program, and from there it was simple enough to come up with an equation to represent the acceleration curve. That is all well in good, however i need to calculate the impact velocity as well. From my understanding from the good ol' highschool days, the indefinite integral of my acceleration curve will yield the the equation for the velocity.
I know it is easy enough to perform numerical integration with the integrate() function, is there anything which is comparable for an indefinite integral?
library(Ryacas)
x <- Sym("x")
Integrate(sin(x), x)
gives
expression(-cos(x))
An alternative way:
yacas("Integrate(x)Sin(x)")
You can find the function reference here
If the NA's you mention are informative in the sense of indicating no acceleration input then they should be replace by zeros. Let's assume you have the data in acc.vec and the device recorded at a rate of rec_per_sec:
acc.vec[is.na(ac.vec)] <- 0
vel.vec <- cumsum(acc.vec)/recs_per_sec
I do not think constructing a best fit curve is going to improve your accuracy in this instance. To plot velocity versus time:
plot(1:length(acc.vec)/recs_per_sec, vel.vec,
xlab="Seconds", ylab="Integrated Acceleration = Velocity")
As Ben said, try the Ryacas package for calculating the antiderivative of a function. But you probably should ask yourself whether you really want to generate a continuous function which only approximates your data in the first place (fitting errors). I'd stick with numerical integration of your actual data. Keep in mind the uncertainty in each data point, of course.

Calculus, How can you find an equation from a series of numbers?

I'm analyzing financial data and would like to find the inflection points of a line. I know I can do this using derivatives, but first I need an equation. Is there a way to generate an equation based off of a series of numbers. I would need to do this programmaticly.
Spline interpolation is probably more useful for you than polynomial interpolation: if you fit a polynomial, it must inevitably head off to +/- infinity outside your data range.
You will also want a method which allows a slightly loose fit: financial data is often a bit noisy which can result in very weird curves if you try to fit it exactly.
There are established procedures for turning a set of existing data points into a polynomial; this is called Polynomial Interpolation. This article in Wikipedia: http://en.wikipedia.org/wiki/Polynomial_interpolation
explains it mathematically. You can probably Google for algorithms easily enough.
Given enough points, your polynomial tracks the original, unknown function reasonably well, so the polynomial's inflection points should roughly coincide with the peaks and troughs of your data.
On the other hand, we all know there's not really a function behind financial data. So if I were you I'd scan along those points and find every point that has a smaller value to either side of it, and declare that a high; and vice versa for lows. Force-fitting this data into a fictitious function isn't going to make it any more useful.
Update: Tom Smith advises that spline interpolation is to be preferred to polynomial interpolation for this kind of thing, and Wikipedia bears him out. Or rather, it's bullish on his answer.
What you are thinking is analytical calculus ... when having discrete data (e.g. points), you have to do it numerically. Now, a line usually doesn't have inflection points, so I guess you're thinking of a curve. You can either interpolate some kind of it through the points, then calculate the first derivative (also numerically, but for a larger number of points), or you can just calculate the first derivation from the points you have (which will be better depends on how many points you actually have).
But really, this is just theory since we don't know the nature of data, or the language or anything.
For more on the subject search: numerical analysis on wiki, and go from there.
I think curve fitting might help you in this case. Here is a discussion which might be handy.
cheers

How can I estimate the logarithmic form of data points using R?

I have data points that represent a logarithmic function.
Is there an approach where I can just estimate the function that describes this data using R?
Thanks.
I assume that you mean that you have vectors y and x and you try do fit a function y(x)=Alog(x).
First of all, fitting log is a bad idea, because it doesn't behave well. Luckily we have x(y)=exp(y/A), so we can fit an exponential function which is much more convenient. We can do it using nonlinear least squares:
nls(x~exp(y/A),start=list(A=1.),algorithm="port")
where start is an initial guess for A. This approach is a numerical optimization, so it may fail.
The more stable way is to transform it to a linear function, log(x(y))=y/A and fit a straight line using lm:
lm(log(x)~y)
If I understand right you want to estimate a function given some (x,y) values of it. If yes check the following links.
Read about this:
http://en.wikipedia.org/wiki/Spline_%28mathematics%29
http://en.wikipedia.org/wiki/Polynomial_interpolation
http://en.wikipedia.org/wiki/Newton_polynomial
http://en.wikipedia.org/wiki/Lagrange_polynomial
Googled it:
http://www.stat.wisc.edu/~xie/smooth_spline_tutorial.html
http://stat.ethz.ch/R-manual/R-patched/library/stats/html/smooth.spline.html
http://www.image.ucar.edu/GSP/Software/Fields/Help/splint.html
I never used R so I am not sure if that works or not, but if you have Matlab i can explain you more.

Resources