So i have been coding a numerical Lagrange Interpolating Polynomial solver. But i cant get a actual mathematical function output. Found a webpage https://www.dcode.fr/lagrange-interpolating-polynomial where i can just put in the numbers and get a function. How do they do it?
Related
I am working on Bayesian Model Averaging and need to calculate posterior model probabilities.
I want to use `bridge sampling' method recommended by Meng and Wong (1996) and Lopes and West (2004). I have posterior sample as mcmc.list class that I obtained using R2OPenBugs (90,000 samples, each sample has four values). To approximate the marginal likelihood, an approximation function g() to the joint posterior density is chosen. For simplicity, we
took g() as the tetravariate normal density function with mean set to the empirical mean
vector and variance set to the empirical variance-covariance matrix from the mcmc sample.
I dont know how to code in R this function nor how to use existing bridge_sampler function.
Bridge_sampler function has arguments that I dont know what they mean. Tutorial gives example how to use bridge_sampler function with jugs data but not with R2OpenBugs data.
Any help is appreciated!
I get some trouble when I try to get the derivative of phi(x) by using the FFT.
l1=nk(T,n0)
l2=nk(T,n0)
l3=tetak(T,n0)
l4=tetak(T,n0)
dn=np.fft.irfft(l1+complex(0,1)*l2)*(N-2)/4
dtk=np.fft.irfft(l3+complex(0,1)*l4)*(N-2)/4
phi_x=np.sqrt(dn)*np.exp(complex(0,1)*dtk)
Here it is how I get phi(x) : I create my function phi(k) in the fourier space, and it is a symmetric function such that his fft has to be real.
This is why I used np.fft.irfft.
So my function is defined as phi(x)= np.fft.irfft(np.real(phi(k))+ 1j*np.imag(phi_k))
(I have written here phi(k) just to be clear).
l1,l2,l3,l4 are just lists of my fourier coefficients with length L, and the number (N-2)/4 is just to normalize.
And N=1000 here
Then I want to compute the second derivative. Thus, I apply a fft; then I multiply by -k**2 and then i take the ifft :
derivative=np.fft.ifft(-k*k*np.fft.fft(phi_x))
I know I have to be careful because np.fft.irfft gives me a length of (2*N-2).
I've already tried :
k_tf1=np.arange(-999,999)*(2*np.pi/L)
999 because of the 2N-2 length of phi(x)
It doesn't work at all
k_tf1 = np.fft.ifftshift(np.arange(-999, 999))
it doesnt'work too
k_tf1=np.fft.fftfreq(phi_x.size,2*np.pi/(L))
It seems to work, but my values are too high.
I also try to add k_tf1=np.fft.fftshift(k_tf1), but it doesn't work.
If someone know the solution ,i would really appreciate !
how are you?.
I'm trying to create a function that calculates the z-transform of a transfer function using the residues method but for that, I need the factors of the characteristic equation and the powers of the factors, so, in order to do that I tried to factorize polynomials with non-integer coefficients but after trying everything that I read I couldn't factorize make maxima to factorize those polynomials the way I need it.
For giving an example, I have this characteristic equation: "s·(s^2+0.1·s)", the factors should be "s^2" and "s + 0.1" but maxima allways gives me "(s^2·(10·s + 1))/10".
Why I'm signalling this?, well, as I learned that maxima treates the outputs equation as list so I can have its dimension and separate the factos by its positions in the list to measure the powers of the factors and do what I need, but like maxima gives me the result that is shown above then the dimension of the list is different and it will make my function to work differently and possibly have errors.
The result that is shown is given by maxima no matter if I use factor, gfactor, or expand or whatever other function that I found and I know that result is because maxima are rationalizing the polynomial before working with it but I don't need that behavior, I only need the pure factors, so, how can I have the result that I want?.
Thanks in advance for the help.
I'm using
R=QQ['x'];
to declare x as variable such that I can calculate with polynomials in x. What I need is another variable, for example t, to represent an integer which can also be used as exponent. For example I want to consider the polynomial (t+1)x^t. Is that somehow possible in SageMath?
EDIT: I want to explain a little bit the reason why I'm looking for such a feature. I've got a couple of really complex rational functions in a few variables and want SageMath to help me to show that they are identical. They are written down in a different way and if you would do it with pen and paper you would need hours and have a huge change of making mistakes. Actually it are not only a few rational functions but infinitely many. But using variables in the coefficients you can cover infinitely many with only one rational function. A simple example is the polynomial 1+x+x^2+...+x^t. For every non-negative integer you get a different polynomial. But you can write (x^(t+1)-1)/(x-1) as rational function instead. With taking t as a variable you cover infinitely many cases with just one rational function.
Is there a way to do such stuff in SageMath?
To create the polynomial x^t as an element of a polynomial ring, Sage needs to know what integer t is equal to. If polynomials of undetermined degree were introduced, most of the Sage methods for polynomials would not work for them: no way to get the list of coefficients with coefficients(), or to find the factors, or the GCD of two polynomials, etc.
However, you can manipulate and simplify polynomials and rational functions just like any other symbolic expressions. For example:
x,t,k = var('x,t,k')
sum(x^k, k, 1, t)
returns (x^(t + 1) - x)/(x - 1).
The relevant articles are Symbolic Computation and Symbolic Expressions.
Basically, I have a set of up to 100 co-ordinates, along with the desired tangents to the curve at the first and last point.
I have looked into various methods of curve-fitting, by which I mean an algorithm with takes the inputted data points and tangents, and outputs the equation of the cure, such as the gaussian method and interpolation, but I really struggled understanding them.
I am not asking for code (If you choose to give it, thats acceptable though :) ), I am simply looking for help into this algorithm. It will eventually be converted to Objective-C for an iPhone app, if that changes anything..
EDIT:
I know the order of all of the points. They are not too close together, so passing through all points is necessary - aka interpolation (unless anyone can suggest something else). And as far as I know, an algebraic curve is what I'm looking for. This is all being done on a 2D plane by the way
I'd recommend to consider cubic splines. There is some explanation and code to calculate them in plain C in Numerical Recipes book (chapter 3.3)
Most interpolation methods originally work with functions: given a set of x and y values, they compute a function which computes a y value for every x value, meeting the specified constraints. As a function can only ever compute a single y value for every x value, such an curve cannot loop back on itself.
To turn this into a real 2D setup, you want two functions which compute x resp. y values based on some parameter that is conventionally called t. So the first step is computing t values for your input data. You can usually get a good approximation by summing over euclidean distances: think about a polyline connecting all your points with straight segments. Then the parameter would be the distance along this line for every input pair.
So now you have two interpolation problem: one to compute x from t and the other y from t. You can formulate this as a spline interpolation, e.g. using cubic splines. That gives you a large system of linear equations which you can solve iteratively up to the desired precision.
The result of a spline interpolation will be a piecewise description of a suitable curve. If you wanted a single equation, then a lagrange interpolation would fit that bill, but the result might have odd twists and turns for many sets of input data.