Roots of bessel function of first kind for negative n in Matlab - matlab

There are some codes which compute the roots of
J_n(x), n > 0
I want to calculate the roots of:
J_n(x), n < 0
n is a real number.
Is there some algorithm or yet better some Matlab code which does this.

In this paper there is an algorithm to calculate the N first roots of Bessel function of the first kind. This works only if n > -1 in J_n(x).
The whole root calculation turns into an eigenvalue problem. The estimated errors have been calculated in the paper.

Related

Find roots of characteristic equation of a matrix function in MATLAB

I have a matrix that is a function of some parameter A=A(x). I would like to find the points x where this matrix becomes singular. Example (I have a large matrix though):
syms x
A=[x sin(x); cos(x^2) 2.5];
So far I have been symbolically computing the determinant of the matrix and then used fzero or newtzero to find the roots of that characteristic equation. I.e.
detA = det(A);
fzero(matlabFunction(detA),startingGuess)
Then I found this: How to find out if a matrix is singular?, where it is advocated to not use the determinant under any circumstances.
Indeed the symbolic determinant calculation is terribly slow. However I tried to use rank(A) instead as suggested in the link and it does not seem to work for symbolic matrices.
Is there any way to implement the suggestions in the link for finding the roots of a characteristic equation of a matrix that is given symbolically?
A possible approach would be the following: a square matrix A is singular if and only if the homogeneous linear (with respect to the vector y) system A*y = 0 has nontrivial solutions y <> 0 (which is equivalent to det(A) = 0 and rank(A) = 0 among others. So a more or less standard, as I recall from the past, technique to compute such points x is to solve the nonlinear system
A(x)*y = 0 (1)
||y|| = 1 (2)
This way you can compute a point x* and a vector y* such that A(x*) is singular and y* is an eigenvector corresponding to the zero eigenvalue of A(x*).
If I remember correctly, you can also solve the somewhat easier system
A(x)*y = 0 (1)
<y,c> = 1 (2a)
where c is "almost" any nonzero random vector (normalize it to 1 to avoid numerical problems).
As a matter of fact there is an enormous bibliography on the subject - you can look for saddle-node bifurcation computations (in case A(x) is the Jacobian of a vector field), or for "distance to instability".
From a discussion with Ander Biguri it seems that the determinant is actually a perfectly fine method of approaching this problem. The problem seems to be to solve the final equation in a stable manner, which would be a different question.

For what value of k are these 3 vectors linearly dependent

So I have these three vectors:
And I have to find out for what value of k these three vectors are linearly dependent. I have tried using rref and linsolve with syms for this but that did not work out. I'm relatively new to MatLab and matrices so please keep that in mind.
I know in order to check if vectors are linearly dependent that c1...cn have to be non-zero.
I also want to know how you can use variables in general when solving these types of equations in MatLab.
A set of vectors (at least if you have n vectors in n dimensions) is linearly dependent if the matrix constructed from them is singular, i.e. if its determinant is 0. If you have the Symbolic Math Toolbox, you can construct a symbolic matrix:
syms k;
M = [1 k 0; -1 1 2; 0 0 3];
det(M)
This will tell you that det(M)==3*k+3, which you can solve by hand. But generally, you can ask matlab to solve it:
solve(det(M)==0,k);
which will tell you the answer is -1. So unless k==-1, these vectors are linearly independent (i.e. they comprise a basis of the Euclidean space R^3).
Update: If you don't have the Symbolic Math Toolbox, you could still try to find a numerical solution. First define a function
detfun=#(k) det([1 k 0; -1 1 2; 0 0 3]);
that for any value of k will give you the determinant of your matrix, for instance detfun(3) gives 12. Then you can use fsolve to find a numerical solution to the equation detfun(k)==0, by calling
fsolve(detfun,0)
in which the second argument, 0, refers to the starting point of the search performed by fsolve. This will tell you that the answer is k==-1, but a single call to fsolve will only give you a single solution. If your function has multiple roots, you have to play around with the starting points to find more of them. In this case, you can know that your function (i.e. det(M(k)) is linear in k, so it has a unique root.

Minimize quadratic form energy using matlab. Which function should I use?

I'm new to matlab and try to do some energy minimization work with it. The energy function takes a 3-channel image as input. For every channel, there's a energy term looks like this:
E = x'Ax + ||Bx||^2 + w*||x-c||^2,
where x,c are vectors of length N, A is a matrix of size N*N. A is sparse and positive semi-definite and has 25 non-zero elements per row, giving constraints to all elements of x. B is of size M*N. B is sparse too and has 2 non-zero elements per row. N is about 850,000. M is about 1,000,000. Although B gives more than N constraints, some elements of x have nothing to do with ||Bx||^2 term. The weight w of term ||x-c||^ is quite small, say 1e-3.
I've searched matlab documentation. It looks like I should use lsqnonlin for this problem. Is there a special designed function or option for quadratic form minimization in matlab?
For those who are familiar with computer vision literature, I'm actually trying to implement the algorithm in "Coherent Intrinsic Images from Photo Collections". The authors said they use matlab backslash operator to minimize the energy, but I can't see how a backslash operator can be used in quadratic form problem.
Yes, there is a function specifically for optimizing quadratic cost functions: quadprog. However, if you don't have any linear constraints, then you should be able to write your cost function as
E = x'Mx/2 + vx + k
Finding the point of zero gradient (hopefully a minimum) can then be achieved by taking first derivatives:
dE/dx = Mx + v
setting them to zero giving the solution:
x = -M\v

fft matrix-vector multiplication

I have to solve in MATLAB a linear system of equations A*x=B where A is symmetric and its elements depend on the difference of the indices: Aij=f(i-j).
I use iterative solvers because the size of A is say 40000x40000. The iterative solvers require to determine the product A*x where x is the test solution. The evaluation of this product turns out to be a convolution and therefore can be done dy means of fast fourier transforms (cputime ~ Nlog(N) instead of N^2). I have the following questions to this problem:
is this convolution circular? Because if it is circular I think that I have to use a specific indexing for the new matrices to take the fft. Is that right?
I find difficult to program the routine for the fft because I cannot understand the indexing I should use. Is there any ready routine which I can use to evaluate by fft directly the product A*x and not the convolution? Actually, the matrix A is constructed of 3x3 blocks and is symmetric. A ready routine for the product A*x would be the best solution for me.
In case that there is no ready routine, could you give me an idea by example how I could construct this routine to evaluate a matrix-vector product by fft?
Thank you in advance,
Panos
Very good and interesting question! :)
For certain special matrix structures, the Ax = b problem can be solved very quickly.
Circulant matrices.
Matrices corresponding to cyclic convolution Ax = h*x (* - is convolution symbol) are diagonalized in
the Fourier domain, and can be solved by:
x = ifft(fft(b)./fft(h));
Triangular and banded.
Triangular matrices and diagonally-dominant banded matrices are solved
efficiently by sparse LU factorization:
[L,U] = lu(sparse(A)); x = U\(L\b);
Poisson problem.
If A is a finite difference approximation of the Laplacian, the problem is efficiently solved by multigrid methods (e.g., web search for "matlab multigrid").
Interesting question!
The convolution is not circular in your case, unless you impose additional conditions. For example, A(1,3) should equal A(2,1), etc.
You could do it with conv (retaining only the non-zero-padded part with option valid), which probably is also N*log(N). For example, let
A = [a b c d
e a b c
f e a b
g f e a];
Then A*x is the same as
conv(fliplr([g f e a b c d]),x,'valid').'
Or more generally, A*x is the same as
conv(fliplr([A(end,1:end-1) A(1,:)]),x,'valid').'
I'd like to add some comments on Pio_Koon's answer.
First of all, I wouldn't advise to follow the suggestion for triangular and banded matrices. The time taken by a call to Matlab's lu() procedure on a large sparse matrix massively overshadows any benefits gained by solving the linear system as x=U\(L\b).
Second, in the Poisson problem you end up with a circulant matrix, therefore you can solve it using the FFT as described. In this specific case, your convolution mask h is a Laplacian, i.e., h=[0 -0.25 0; -0.25 1 -0.25; 0 -0.25 0].

Matlab: Find the Range for Polynomial Equations

I have a polynomial, y(x)=a0+a1*x^1+a2*x^2+a3*x^3+a4*x^4+......+an*x^n. of the degree n, where ai is a real number.
My question is, is there a function in matlab that I can use to find the range of x for all y(x)>0?
No there is no explicit function that does this. However, as long as you desire a numerical solution, it is possible.
You can solve for the roots of y(x). (hint: roots)
What happens between any pair of roots? What happens above and below the largest and smallest real roots? What can you do with any roots that are complex?
I can't think of a function but I would do the following:
Find the roots of your polynomial with roots.
Find the gradient of y(x) at the smallest root.
If it is increasing, then it will decrease at the next root and vice versa.
Now you can create intervals where y(x) is positive.
Additionally, if you want visualize your answers, you can plot your polynomial with ezplot. E.g. ezplot('5*x^3 + 4*x^2 + 3*x + 2');