How can I create a symbolic integer for use with my Fourier transform? - matlab

I am computing a Fourier transformation in MATLAB, when computing coefficients C[0] and C[n*f0], I got pretty nasty result because MATLAB doesn't recognize my variable "n" as integer. I currently compute with "n" as a symbolic variable (syms n;). How to change symbolic n to symbolic integer n?

Looking at the MATLAB documenation, to add the assumption "n is integer" in R2008b or later, you have to write
evalin(symengine,'assume(n,Type::Integer)')
This answers your question, however, I'm not really sure it solves your problem.
When you do a Fourier transform, you are performing a heavy numeric operation on your data, consequently all variables involved in that need to have concrete values. Your n probably should be an integer, but not just by type, it should contain an actual number. If you declare it using syms, it will potentially not contain a number, so you be sure you really need the symbolic toolbox!
If you do, and n is the result of a calculation that yield one specific integer, you can convert it to normal numerical form using uint32(n) or similar, see the help on conversions, e.g.
Y = fft(X,uint32(n))
Update: The error message you give in the comment implies that your n is in fact not an integer... I doubt you will be able to use it with fft.

Related

MATLAB: Multiplying a matrix by an unknown scalar (variable.)

I am trying to multiply a 3x2 matrix with an unknown scalar ( a number in terms of an unknown variable (t).
For instance 10t [<3x2 matrix>]. The variable t has no value and should always appear as a "t" instead of any numeric value. How do I get MATLAB to compute the result, while leaving the "t"'s as characters?
as David suggested use symbolic toolbox
type the following in matlab: syms t
however if you would describe what you are trying to do in more length, we might find out that you dont even need to use the symbolic toolbox. there might be other alternatives.

Summing a series in matlab

I'm trying to write a generic function for finding the cosine of a value inputted into the function. The formula for cosine that I'm using is:
n
cosx = sum((-1)^n*x^(2n)/(2n)!)
n=1
I've looked at the matlab documentation and this page implies that the "sum" function should be able to do it so I tried to test it by entering:
sum(x^n, n=1..3)
but it just gives me "Error: The expression to the left of the equals sign is not a valid target for an assignment".
Is summing an infinite series something that matlab is able to do by default or do I have to simulate it using a function and loops?
Well if you want to approximate it to a finite number of terms you can do it in Matlab without toolboxes or loops:
sumCos = #(x, n)(sum(((-1).^(0:n)).*(x.^(2*(0:n)))./(factorial(2*(0:n)))));
and then use it like this
sumCos(pi, 30)
The first parameter is the angle, the second is the number of terms you want to take the series to (i.e. effects the precision). This is a numerical solution which I think is really what you're after.
btw I took the liberty of correcting your initial sum, surely n must start from 0 if you are trying to approximate cos
If you want to understand my formula (which surely you do) then you need to read up on some essential Matlab basics namely the colon operator and then the concept of using . to perform element-wise operations.
In MATLAB itself, no, you cannot solve an infinite sum. You would have to estimate it as you suggested. The page you were looking at is part of the Symbolic Math toolbox, which is an add-on to MATLAB. In particular, you were looking at MuPAD, which is rather similar to Mathematica. It is a symbolic math workspace, whereas MATLAB is more of a numeric math workspace. If you own the Symbolic Math toolbox, you can either use MuPAD as you tried to above, or you can use the symsum function from within MATLAB itself to carry out sums of series.

Using the Pochhammer Symbol in Matlab

I've tried to use a script that evaluates the Pochhammer symbol (rising factorial) in Matlab, but it fails to evaluate pochhammer(x,n) whenever x is a negative number even though the expression is valid when x is negative (Wolfram Alpha and Mathematica give answers for Pochhammer(-3,2)).
Can anyone help me get pochhammer working in Matlab for negative arguments?
I assume that you're referring to this Pochhammer function. Note that pochhammer (not capitalized) is part of MuPAD, which is a separate environment available with Matlab's Symbolic Math Toolbox. You can access MuPAD by typing mupad in the Matlab command window.
If, however, like a normal Matlab user, you wish to use the pochhammer function from Matlab itself and program with it, you cannot run it from the regular command window or Editor in the normal fashion, as you discovered. Instead, you must use
evalin(symengine,'pochhammer(-3,2)')
or the more flexible
feval(symengine,'pochhammer',-3,2)
See more here. These both return symbolic numbers as results and only work for scalar inputs. If you require double-precision output and have vector inputs (only works for the the second one, n) use
mfun('pochhammer',-3,-3:3)
This is equivalent to using MuPAD's map function, so you could also write:
feval(symengine,'map',sym(-3:3),'n->pochhammer(-3,n)')
However, if you're not working with symbolic math at all, there may be no reason to use this function instead of a fully double-precision solution. The Pochhammer symbol is defined simply as the ratio of two gamma functions and can be implemented efficiently as (x and n must be the same dimensions or scalar – additionally, neither x nor x-n can be an integer less than or equal to zero, where the gamma function is singular):
poch = #(x,n)gamma(x+n)./gamma(x);
If n and x are integers you should use round to ensure that the output is exactly integer. The only pitfall is that for sufficiently large values of x and/or n this naïve implementation will overflow to Inf (or NaN). In these cases you'll need to do something else such as use the symbolic version (which may or may not return Inf when cast back to double). For integer values of n (and scalar n>=0), something like the following can be used
poch = #(x,n)prod(bsxfun(#plus,x(:),0:n-1),2);
Note that even for integers this can be up 20 times slower than the gamma version.
The numerical command pochhammer for Matlab (not MuPAD) was introduced in matlab version R2014b.

Minimizing error of a formula in MATLAB (Least squares?)

I'm not too familiar with MATLAB or computational mathematics so I was wondering how I might solve an equation involving the sum of squares, where each term involves two vectors- one known and one unknown. This formula is supposed to represent the error and I need to minimize the error. I think I'm supposed to use least squares but I don't know too much about it and I'm wondering what function is best for doing that and what arguments would represent my equation. My teacher also mentioned something about taking derivatives and he formed a matrix using derivatives which confused me even more- am I required to take derivatives?
The problem that you must be trying to solve is
Min u'u = min \sum_i u_i^2, u=y-Xbeta, where u is the error, y is the vector of dependent variables you are trying to explain, X is a matrix of independent variables and beta is the vector you want to estimate.
Since sum u_i^2 is diferentiable (and convex), you can evaluate the minimal of this expression calculating its derivative and making it equal to zero.
If you do that, you find that beta=inv(X'X)X'y. This maybe calculated using the matlab function regress http://www.mathworks.com/help/stats/regress.html or writing this formula in Matlab. However, you should be careful how to evaluate the inverse (X'X) see Most efficient matrix inversion in MATLAB

MuPad: Cannot work with symbolic expansion when handling matricial expressions

I am working MuPad in order to have a symbolic tool to find solution for an equation. But I am working with matrices.
Consider this:
blck := A -> matrix([
[A[1..linalg::matdim(A)[1]/2,1..linalg::matdim(a)[2]/2],
A[1..linalg::matdim(A)[1]/2,linalg::matdim(A)[2]/2+1..linalg::matdim(A)[2]]],
[A[linalg::matdim(A)[1]/2+1..linalg::matdim(A)[1],1..linalg::matdim(A)[2]/2],
A[linalg::matdim(A)[1]/2+1..linalg::matdim(A)[1],linalg::matdim(A)[2]/2+1..linalg::matdim(A)[2]]]
])
This function enables me to have a block representation of a matrix and it works. Now consider this function
myfun := A -> matrix([[blck(A)[1,1]*blck(A)[2,2]*blck(A)[2,1],blck(A)[1,1]],
[blck(A)[1,1],blck(A)[1,1]]])
This will manipulate a little a matrix and returns matrix whose components are combined somehow. The problem is that, considering that I cannot tell MuPad that matrix A and its components are matrices and not reals, it happens that MuPad will show me matrix products in different order
For example. Consider
myfun(matrix([[A11,A12],[A21,A22]]))
The first component of the returned matrix, element (1,1), is A11*A21*A22 which is incorrect being A11,A12,A21,A22 matrices!
How can i tell MuPad that A11,A12,A21 and A22 are matrices so that MuPad will expand products correctly?
You can have matrices in matrices in MuPAD, as long as you explicitly put them in there. Just telling the system to treat A1*A2 as non-commutative is more difficult and not well supported. You could go full-blown, create your own datatype and implement arithmetic accordingly, but that's not necessarily easy if you still want simplifications to happen.