Question about reformulating a difference equation as a matrix equation - matlab

The difference equation and the boundary value are:
y(n)-10.1y(n-1)+y(n-2)=-1.35n,y(-1)=0,y(100)=101/6
I have managed to solve for y(n) with eigenvector to obtain the homogeneous solution and particular solution and solve the coefficients using the boundary value. However, I do not know how to reformulate the whole difference equation into a matrix equation. Can anyone provide me with an idea or hint?

Hint: You need a Y vector with 2 entries y(n), y(n-1)
A recurrence relation of the form:
y(n) = a_{n-1}y(n-1)+a_{n-2}y(n-2)+..+a_0y(0)
can be extended with n-1 identities of the form: y(n-k)=y(n-k) to obtain a linear system:
Y(n) = [y(n),y(n-1),..,y(1)]^T = A * [y(n-1),y(n-2),..,y(0)]^T = A * Y(n-1)

Related

How can I find the joint eigenvalues of two matrices in MATLAB?

If the joint eigenvalues of matrices A and B are defined as the roots of the equation
det(lambda * A - B) = 0,
how can I solve this in MATLAB?
In particular, I am not sure how exactly lambda is defined - it obviously needs to be a matrix or vector, as otherwise there would only be one joint eigenvalue. Also, I am not sure if there is any in-built function, or if, say, fzero for finding the roots of nonlinear functions needs to be used.
There is a built-in function for this.
http://www.mathworks.com/help/matlab/ref/eig.html
[V,D] = eig(B,A);
[V,D] = eig(A,B) solves the system det(A - lambda*B) == 0. However, the desired system to solve is det(A*lambda - B) == 0 and so the inputs are reversed to respect solving this system.

matlab differential equation

I have the following differential equation which I'm not able to solve.
We know the following about the equation:
D(r) is a third grade polynom
D'(1)=D'(2)=0
D(2)=2D(1)
u(1)=450
u'(2)=-K * (u(2)-Te)
Where K and Te are constants.
I want to approximate the problem using a matrix and I managed to solve
the similiar equation: with the same limit conditions for u(1) and u'(2).
On this equation I approximated u' and u'' with central differences and used a finite difference method between r=1 to r=2. I then placed the results in a matrix A in matlab and the limit conditions in the vector Y in matlab and ran u=A\Y to get how the u value changes. Heres my matlab code for the equation I managed to solve:
clear
a=1;
b=2;
N=100;
h = (b-a)/N;
K=3.20;
Ti=450;
Te=20;
A = zeros(N+2);
A(1,1)=1;
A(end,end)=1/(2*h*K);
A(end,end-1)=1;
A(end,end-2)=-1/(2*h*K);
r=a+h:h:b;
%y(i)
for i=1:1:length(r)
yi(i)=-r(i)*(2/(h^2));
end
A(2:end-1,2:end-1)=A(2:end-1,2:end-1)+diag(yi);
%y(i-1)
for i=1:1:length(r)-1
ymin(i)=r(i+1)*(1/(h^2))-1/(2*h);
end
A(3:end-1,2:end-2) = A(3:end-1,2:end-2)+diag(ymin);
%y(i+1)
for i=1:1:length(r)
ymax(i)=r(i)*(1/(h^2))+1/(2*h);
end
A(2:end-1,3:end)=A(2:end-1,3:end)+diag(ymax);
Y=zeros(N+2,1);
Y(1) =Ti;
Y(2)=-(Ti*(r(1)/(h^2)-(1/(2*h))));
Y(end) = Te;
r=[1,r];
u=A\Y;
plot(r,u(1:end-1));
My question is, how do I solve the first differential equation?
As TroyHaskin pointed out in comments, one can determine D up to a constant factor, and that constant factor cancels out in D'/D anyway. Put another way: we can assume that D(1)=1 (a convenient number), since D can be multiplied by any constant. Now it's easy to find the coefficients (done with Wolfram Alpha), and the polynomial turns out to be
D(r) = -2r^3+9r^2-12r+6
with derivative D'(r) = -6r^2+18r-12. (There is also a smarter way to find the polynomial by starting with D', which is quadratic with known roots.)
I would probably use this information right away, computing the coefficient k of the first derivative:
r = a+h:h:b;
k = 1+r.*(-6*r.^2+18*r-12)./(-2*r.^3+9*r.^2-12*r+6);
It seems that k is always positive on the interval [1,2], so if you want to minimize the changes to existing code, just replace r(i) by r(i)/k(i) in it.
By the way, instead of loops like
for i=1:1:length(r)
yi(i)=-r(i)*(2/(h^2));
end
one usually does simply
yi=-r*(2/(h^2));
This vectorization makes the code more compact and can benefit the performance too (not so much in your example, where solving the linear system is the bottleneck). Another benefit is that yi is properly initialized, while with your loop construction, if yi happened to already exist and have length greater than length(r), the resulting array would have extraneous entries. (This is a potential source of hard-to-track bugs.)

How to use least squares method in Matlab?

I have 37 linear equations and 36 variables in the form of a matrix equation; A*X=B . The equations don't have an exact answer. I want to use Matlab least square method to find the answers with the least error. I am new to Matlab so any comments will help. Thank you
If A is of full rank, i.e. the columns of A are linearly independent, the least-squares solution of an overdetermined system of linear equations
A * x = b
can be found by inverting the normal equations (see Linear Least Squares):
x = inv(A' * A) * A' * b
If A is not of full rank, A' * A is not invertible. Instead, one can use the pseudoinverse of A
x = pinv(A) * b
or Matlab's left-division operator
x = A \ b
Both give the same solution, but the left division is more computationally efficient.
The two latter computation methods can also deal with underdetermined systems of linear equations, but they give different solutions in that case: The pseudoinverse gives the solution where x has the smallest sum of squares, while the left-division operator gives a solution with as many 0 coefficients as possible.
The most general way to solve this is to use the pseudoinverse:
X = pinv(A) * B;
You can calculate x by:
x = (A'*A)\A'*B

how to solve MMV sparse representation with CVX

I want to solve multiple measurement vector (MMV) sparse representation problem with CVX toolbox. I have a N*L matrix X. matrix X have only a few nonzero rows. I have system of equations Y=A*X. Y is M*L matrix of measurements (M
min Relax(X)
subject to Y=A*X
Realx(.) is a function that applies norm 1 to the vector t. (N*1)vector t consists of the norm 2 of each rows of matrix X. i.e. Relax(X)= norm_1(t) and t(i)=norm_2(X(i,:))
I can’t transform my objective function into a language that CVX can understand and solve.
Please tell me how I should change the problem objective and constraints that CVX could solve it.
'norms' is the cvx command you're looking for. Suppose sigma is some known parameter that allows Y to be only approximately equal to A*X (e.g. I tried it with sigma=10e-6). Then you could use this code:
cvx_begin separable
variable X(n,n)
minimize( norms(X,2,1) )
subject to
norm(Y - A*X,2)<= 10*sigma
cvx_end

How to solve the system of equation (simultanious) in matlab?

I have to solve the following equation in matlab but I have some requirement step which I must follow.
x+y=17
2x-y=10
"write the system of equation on Matrix form,
AX=B, and plug in the matrix A and the vector B in Matlab. Solve the system
of equations by calculating the inverse A-1, and then the product A-1 B using
Matlab. Also, report the determinant of A in each question".
Writing this as a matrix equation:
A * X = B
You have the following matrices:
A = [1 1; 2 -1];
B = [17; 10];
And you are looking for X. For this you need the matlab \ operator. I am going to leave it as an exercise for the student to figure out how to use that operator with this matrix and vector combination in order to produce X which is the solution of the equation.