I would like to compute numerically an integral of a function that is a product of matrices like this ([1 1 1] is a row matrix with 3 entries, A(t) is a 3x3 matrix with each entry a function of t and P(t),S(t) is a column matrix with each entry a function of t)
I then tried to plot the integral of this function with
g=#(upper)integral(#(t)f(t),0,upper)
and
t=0:0.1:10;
plot(g(t),t)
but this doesnt seem to work. For example to let matlab accept t in A(t) and P(t) I had to have
syms t
but then it complains about this symbol in the integration.
Related
How to make the surface plot of the three vectors with following dimentions in MATLAB using Surf function.
Dimension of vector x= 43x1
Dimension of vector y= 10x1
Dimension of vector z= 43x1
I have three vectors x, y, z with above dimensions. When I tried, it says dimension does not match. Please help.
I have a matrix W which is a block diagonal matrix with dimensions 2*4, and each of its two block diagonals is 1*2 vector. I want to find the values of its entries that minimize the difference between the following function:
( F = BH-AW )
Where: W is the required block diagonal matrix to be optimized, B is a 2*2 matrix, H is a given 2*4 matrix, and A is a 2*2 matrix. A and B are calculated using the functions used in the attached code.
I tried this attached code, but I think it is now in an infinite loop and I don't know what should I do?
%% My code is:
while ((B*H)-(A*W)~=zeros(2,4))
w1=randn(1,2);
% generate the first block diagonal vector with dimensions 1*2. The values of each entry of the block diagonal vector maybe not the same.
w2=randn(1,2);
% generate the second block diagonal vector with dimensions 1*2.
W=blkdiag(w1,w2);
% build the block diagonal matrix that I want to optimize with dimensions 2*4.
R=sqrtm(W*inv(inv(P)+(H'*inv(eye(2)+D)*H))*W');
% R is a 2*2 matrix that will be used to calculate matrix A using the LLL lattice reduction algorithm. The values of P (4*4 matrix), H (2*4 matrix) and D (2*2 matrix) are given. It's clear here that matrix R is a function of W.
A= LLL(R,3/4);
% I use here LLL lattice reduction algorithm to obtain 2*2 matrix A which is function of R.
B=A'*W*P*H'*inv(eye(2)+D+H*P*H');
% B is 2*2 matrix which is function of A and W. The values of P (4*4 matrix), H (2*4 matrix) and D (2*2 matrix) are given.
end
Numerical operations with floating point numbers are only approximate on a computer (any number is only ever represented with a finite number of bits, which means you cannot exactly represent Pi for example). For more info, see this link.
Consequently, it is extremely unlikely that the loop you wrote will ever terminate, because the difference between B*H and A*W will not be exactly zero. Instead, you need to use a tolerance factor to decide when you are satisfied with the similarity achieved.
Additionally, as suggested by others in comment, the "distance" between two matrices is typically measured using some sort of norm (e.g. the Frobenius norm). By default, the norm function in Matlab will give the 2-norm of an input matrix.
In your case, this would give something like:
tol = 1e-6;
while norm(B*H-A*W) > tol
% generate the first block diagonal vector with dimensions 1*2.
% The values of each entry of the block diagonal vector maybe not the same.
w1=randn(1,2);
% generate the second block diagonal vector with dimensions 1*2.
w2=randn(1,2);
% build the block diagonal matrix that I want to optimize with dimensions 2*4.
W=blkdiag(w1,w2);
% R is a 2*2 matrix that will be used to calculate matrix A using the LLL lattice reduction algorithm.
% The values of P (4*4 matrix), H (2*4 matrix) and D (2*2 matrix) are given.
% It's clear here that matrix R is a function of W.
R=sqrtm(W/(inv(P)+(H'/(eye(2)+D)*H))*W');
% I use here LLL lattice reduction algorithm to obtain 2*2 matrix A which is function of R.
A= LLL(R,3/4);
% B is 2*2 matrix which is function of A and W. The values of P (4*4 matrix),
% H (2*4 matrix) and D (2*2 matrix) are given.
B=A'*W*P*H'/(eye(2)+D+H*P*H');
end
Note that:
With regards to the actual algorithm, I am a little bit concerned that your loop never seems to update the value of W, but instead updates matrices A and B. This suggests that your description of the problem might be incorrect or incomplete, but that is beyond the scope of this forum anyway (ask on Maths.SE if you want to know more).
Using inv() directly is discouraged in many cases. This is because the algorithm to compute the inverse of a matrix is less reliable than the algorithm to solve systems of the type AX=B. Matlab should give you a warning to use / and \ where possible; I would advise following this recommendation unless you know what you are doing.
I have to solve following linear equation in Matlab
AX=B for X, where A - symetric positive definite upper triangular matrix (nxn), and B is matrix (mxn).
So far I have got code for solving such eqation where B is a vector. I have to change the code, to be able to calculate it for B as a (mxn) matrix
x(n)=b(n)/A(n,n);
for i=n-1: -1:1,
s=b(i);
for j=i+1:n,
s=s-A(i,j)*x(j);
end
x(i)=s/A(i,i);
end
I am using Matlab to Compute the rank of a matrix as:
r=rank(A, tol)
where A is the matrix, tol is tolerance. when the matrix is small there seems to be no issue. But when the matrix is large matlab often returns with error saying the SVD should not have NAN or INF as input.
As far as my understanding goes, the the rank computing algorithm should return a number for a matrix, but when I see such error I wonder if there are special matrices for which rank cannot be computed. Is there better way to compute the rank in Matlab ? I am looking for a reliable algo to compute the rank of a matrix! Why is the rank computation algo so sensitive to some matrices?
EDIT: please check this dependence on tol:
rank(magic(100), 10e-10)
ans =
3
rank(magic(100), 10e-30)
ans =
100
I am basically computing the controllability matrix of a linear system for which I am checking the rank condition. The matrix sizes are in the order of 100x100 to 200x200. So the input to the rank is as follows
A= ctrb (P, Q) % P, Q are matrices in the LTI system X[n+1]=P*x[n]+ Q*U[n]
r=rank(A, tol)
so, question is then can controllability function ctrb produces matrices with INF or NAN? Based on the definition of the controllability matrix :
A= [Q P*Q P^2*Q, ...P^n-1*Q]
If P and Q are any matrices with bounded values, can A have INF or NAN? I am expecting that the above computation of A using the ctrb for any bounded P, Q matrices can not produce a matrix output with NAN or INF.
For example i have 5 point like this,
(1,1) (2,-1) (3,2) (4,-2) (5,2)
Now,
1) I want a function to interpolation these points in Matlab.
2) I want to Plot this function.
3) Read a number from input and write F(x) to output.
How can I do this??
To fit a polynom to given datapoints you can use polyfit(x,y,n) where x is a vector with points for x, y is a vector with points for y and n is the degree of the polynom. See example at Mathworks polyfit documentation
In your case:
x=[1,2,3,4,5];
y=[1,-1,-2,-2,2];
n=3;
p = polyfit(x,y,n)
And then to plot, taken from example
f = polyval(p,x);
plot(x,y,'o',x,f,'-')
Or, to make a prettier plot of the polynomial (instead of above plot)
xx=0:0.1:5;
yy = erf(xx);
f = polyval(p,xx);
plot(x,y,'o',xx,f,'-')
If you are not sure what a good fit would be and want to try out different fit, use the curve fitting toolbox, cftool. You will need to create two vectors with x and y coordinates and then you can play around with cftool.
Another option would be to use interp1 function for interpolation. See matlab documentation for more details.
If you want polynomial interpolation, take a look at the polyfit function. It is used generally for least-squares polynomial approximation, but if you choose the degree+1 to be the same as the number of points you are fitting it will still work for you. For interpolation as you probably know, the degree of the interpolant is equal to the number of points you have -1. So for your example points above you need a degree 4 polynomial. Here is a link to the mathworks documentation
http://www.mathworks.co.uk/help/matlab/ref/polyfit.html
If you split your points into 2 vectors of respective x and y coordinates, you can simply obtain your interpolating polynomial coefficients in a vector b where
b = polyfit(x,y,4)
and based on your data above, your x and y vectors are
x = [1 2 3 4 5];
y = [1 -1 2 -2 2]