MATLAB failed to compute the fft of this function:
syms t;
x= 1/(1+t^2);
X= fft(x)
And threw this error:
Undefined function 'fft' for input arguments of type 'sym'.
Why didn't it take the Fourier transform of the symbolic variable? I think it should because we may want to obtain the result as a symbolic value.
I also tried to solve the problem with non-symbolic variable.
t= -10:0.01:10;
x= zeros(2001);
x= 1/(1+t.^2);
fft(x)
this time my error is:
Matrix dimensions must agree.
However, they have the same dimensions.
Where is my fault?
Source: http://www.mathworks.com/matlabcentral/newsreader/view_thread/315950
FFT is a method for numerical discrete Fourier Transform (DFT).
Basically, what you're asking doesn't make sense. FFT is designed to work numerically on discrete data (a sequence of numbers).
What you want is the Fourier Transform of your symbolic expression. For that, I believe you want fourier.
The documantation on fft says:
Y = fft(X) computes the discrete Fourier transform (DFT) of X using a fast Fourier transform (FFT) algorithm.
Symbolic functions are continuous, not discrete. Hence, the algorithm fails.
With regards to your second question: use element-wise operators, by adding a dot:
t= -10:0.01:10;
x= zeros(2001);
x= 1./(1+t.^2);
fft(x)
MATLAB complains about matrix dimensions, because you are trying to divide a scalar (i.e. a 1 x 1 matrix) by a vector of length 2001. element-wise division solves that problem.
Related
E,M,F,lambda,x are Matrix or vector. The equation is:
E*x+F+Transpose(M)*lambda=0;
M*x-gamma=0;
Transpose(lambda)*(M*x-gamma)=0
The solutions of X and lambda are
lambda=-(M*E^(-1)*Transpose(M))*(gamma+M*Transpose(E)*F)
x=-E^(-1)*(F+Transpose(M)*Transpose(lambda))
How can I use Matlab's symbolic operation to obtain the X and lambda solutions of the above matrix equation? I don't want to deduce the formula myself. I want Matlab to deduce the formula automatically.
i have vin which is the input voltage of circuit .
(𝜏=taw)
vin[(-taw/4)-->(taw/4)]=0.75
vin[(taw/4)-->(3*taw/4)]=-0.25
and the voltage is repeated,this is a square input voltage.
𝑉𝑖𝑛(𝑡) = ∑𝑎𝑘 cos (2𝜋𝑘t/𝜏)
now i need to find Fourier coefficients:
ak= 2/𝜏*∫ 𝑉𝑖𝑛(𝑡) cos (2𝜋𝑘𝑡/𝜏) 𝑑𝑡 k>0.
ak=1/𝜏∫ 𝑉𝑖𝑛(𝑡)𝑑t k<0.
so basicly i need to write a function with the input n, and the function finds fourier coefficients, and returns a vector with (n+1) first fourier coefficients
now my problem is i how can i right vin in the integral ?
i wrote t-nhis and i know it is wrong i will aprciate a little help
[coefs]=a_n_coefs(n)
syms t k
taw=(i still don't know what to do here but i know that it have no effect)
coefs= 1:(n+1);
a0=2*int(0.75,t,0,taw/4)+2*int(-0.25,t,0,taw/4);
end
obviuosly thats nothing but i basicly want to know how i can write the integral then i will continue and write the function.
I am doing a project where i find an approximation of the Sine function, using the Least Squares method. Also i can use 12 values of my own choice.Since i couldn't figure out how to solve it i thought of using Taylor's series for Sine and then solving it as a polynomial of order 5. Here is my code :
%% Find the sine of the 12 known values
x=[0,pi/8,pi/4,7*pi/2,3*pi/4,pi,4*pi/11,3*pi/2,2*pi,5*pi/4,3*pi/8,12*pi/20];
y=zeros(12,1);
for i=1:12
y=sin(x);
end
n=12;
j=5;
%% Find the sums to populate the matrix A and matrix B
s1=sum(x);s2=sum(x.^2);
s3=sum(x.^3);s4=sum(x.^4);
s5=sum(x.^5);s6=sum(x.^6);
s7=sum(x.^7);s8=sum(x.^8);
s9=sum(x.^9);s10=sum(x.^10);
sy=sum(y);
sxy=sum(x.*y);
sxy2=sum( (x.^2).*y);
sxy3=sum( (x.^3).*y);
sxy4=sum( (x.^4).*y);
sxy5=sum( (x.^5).*y);
A=[n,s1,s2,s3,s4,s5;s1,s2,s3,s4,s5,s6;s2,s3,s4,s5,s6,s7;
s3,s4,s5,s6,s7,s8;s4,s5,s6,s7,s8,s9;s5,s6,s7,s8,s9,s10];
B=[sy;sxy;sxy2;sxy3;sxy4;sxy5];
Then at matlab i get this result
>> a=A^-1*B
a =
-0.0248
1.2203
-0.2351
-0.1408
0.0364
-0.0021
However when i try to replace the values of a in the taylor series and solve f.e t=pi/2 i get wrong results
>> t=pi/2;
fun=t-t^3*a(4)+a(6)*t^5
fun =
2.0967
I am doing something wrong when i replace the values of a matrix in the Taylor series or is my initial thought flawed ?
Note: i can't use any built-in function
If you need a least-squares approximation, simply decide on a fixed interval that you want to approximate on and generate some x abscissae on that interval (possibly equally spaced abscissae using linspace - or non-uniformly spaced as you have in your example). Then evaluate your sine function at each point such that you have
y = sin(x)
Then simply use the polyfit function (documented here) to obtain least squares parameters
b = polyfit(x,y,n)
where n is the degree of the polynomial you want to approximate. You can then use polyval (documented here) to obtain the values of your approximation at other values of x.
EDIT: As you can't use polyfit you can generate the Vandermonde matrix for the least-squares approximation directly (the below assumes x is a row vector).
A = ones(length(x),1);
x = x';
for i=1:n
A = [A x.^i];
end
then simply obtain the least squares parameters using
b = A\y;
You can clearly optimise the clumsy Vandermonde generation loop above I have just written to illustrate the concept. For better numerical stability you would also be better to use a nice orthogonal polynomial system like Chebyshev polynomials of the first kind. If you are not even allowed to use the matrix divide \ function then you will need to code up your own implementation of a QR factorisation and solve the system that way (or some other numerically stable method).
I have to calculate:
gamma=(I-K*A^-1)*OLS;
where I is the identity matrix, K and A are diagonal matrices of the same size, and OLS is the ordinary least squares estimate of the parameters.
I do this in Matlab using:
gamma=(I-A\K)*OLS;
However I then have to calculate:
gamma2=(I-K^2*A-2)*OLS;
I calculate this in Matlab using:
gamma2=(I+A\K)*(I-A\K)*OLS;
Is this correct?
Also I just want to calculate the variance of the OLS parameters:
The formula is simple enough:
Var(B)=sigma^2*(Delta)^-1;
Where sigma is a constant and Delta is a diagonal matrix containing the eigenvalues.
I tried doing this by:
Var_B=Delta\sigma^2;
But it comes back saying matrix dimensions must agree?
Please can you tell me how to calculate Var(B) in Matlab, as well as confirming whether or not my other calculations are correct.
In general, matrix multiplication does not commute, which makes A^2 - B^2 not equal to (A+B)*(A-B). However your case is special, because you have an identity matrix in the equation. So your method for finding gamma2 is valid.
'Var_B=Delta\sigma^2' is not a valid mldivide expression. See the documentation. Try Var_B=sigma^2*inv(Delta). The function inv returns a matrix inverse. Although this function can also be applied in your expression to find gamma or gamma2, the use of the operator \ is more recommended for better accuracy and faster computation.
I am wondering how to draw samples in matlab, where I have precision matrix and mean as the input argument.
I know mvnrnd is a typical way to do so, but it requires the covariance matrix (i.e inverse of precision)) as the argument.
I only have precision matrix, and due to the computational issue, I can't invert my precision matrix, since it will take too long (my dimension is about 2000*2000)
Good question. Note that you can generate samples from a multivariant normal distribution using samples from the standard normal distribution by way of the procedure described in the relevant Wikipedia article.
Basically, this boils down to evaluating A*z + mu where z is a vector of independent random variables sampled from the standard normal distribution, mu is a vector of means, and A*A' = Sigma is the covariance matrix. Since you have the inverse of the latter quantity, i.e. inv(Sigma), you can probably do a Cholesky decomposition (see chol) to determine the inverse of A. You then need to evaluate A * z. If you only know inv(A) this can still be done without performing a matrix inverse by instead solving a linear system (e.g. via the backslash operator).
The Cholesky decomposition might still be problematic for you, but I hope this helps.
If you want to sample from N(μ,Q-1) and only Q is available, you can take the Cholesky factorization of Q, L, such that LLT=Q. Next take the inverse of LT, L-T, and sample Z from a standard normal distribution N(0, I).
Considering that L-T is an upper triangular dxd matrix and Z is a d-dimensional column vector,
μ + L-TZ will be distributed as N(μ, Q-1).
If you wish to avoid taking the inverse of L, you can instead solve the triangular system of equations LTv=Z by back substitution. μ+v will then be distributed as N(μ, Q-1).
Some illustrative matlab code:
% make a 2x2 covariance matrix and a mean vector
covm = [3 0.4*(sqrt(3*7)); 0.4*(sqrt(3*7)) 7];
mu = [100; 2];
% Get the precision matrix
Q = inv(covm);
%take the Cholesky decomposition of Q (chol in matlab already returns the upper triangular factor)
L = chol(Q);
%draw 2000 samples from a standard bivariate normal distribution
Z = normrnd(0,1, [2, 2000]);
%solve the system and add the mean
X = repmat(mu, 1, 2000)+L\Z;
%check the result
mean(X')
var(X')
corrcoef(X')
% compare to the sampling from the covariance matrix
Y=mvnrnd(mu,covm, 2000)';
mean(Y')
var(Y')
corrcoef(Y')
scatter(X(1,:), X(2,:),'b')
hold on
scatter(Y(1,:), Y(2,:), 'r')
For more efficiency, I guess you can search for some package that efficiently solves triangular systems.