Solving system of linear equations - matlab

My goal is to solve the system of equation known as Lyapunov equation, that is finding x in the following equation:
A*X + X*transpose(A) +Q = 0
plus another linear constraint that is X*v = 0
where all matrices A, X ,Q are n by n matrices and v is a vector with length n.
How can I find such X in matlab?

Solution
Solving Lyapunov equations in Matlab is very easy. Both the continuous and discrete Lyapunov equation have a built-in function:
Continuous Lyapunov equation: lyap (see Matlab documentation here)
Discrete Lyapunov equation: dlyap (see Matlab documentation here)
Extra note: if the links would not work, or you want a quick way to check the documentation of a Matlab function offline, every built-in Matlab function has a short help page reachable by help NameOfTheFunction.
Furthermore the extended help page, as also visible on the web, with examples can also be retrieved offline by typing doc NameOfTheFunction in the Matlab terminal.
Example
Given the following continuous Lyapunov equation:
A*X + X*transpose(A) + Q = 0
The solution in Matlab for a stable A and positive definite Q is given as:
X = lyap(A,Q)
In some cases the equation is slightly different:
A*X + X*B + C = 0
This equation calls the Sylvester equation and is again solvable with the built-in Lyapunov function of Matlab:
X = lyap(A,B,C)
The same analogue solution steps exist for the discrete case, where the Lyapunov and Sylvester equation look slightly different:
A*X*transpose(A) -X + Q = 0 -> X = dlyap(A,Q)
A*X*B - X + C = 0 -> X = dlyap(A,B,C)

Related

matlab solving numerical differential equations [pic]

I'm trying to solve this numerical differential equations, can someone help?
clc;
clear;
syms A1(z) A2(z)
lamda1 = 1560*(10^-9);
c=3*(10^8);
d_eff=27*(10^-12);
omga1=(2*pi*c)/(lamda1);
omga2=omga1*2;
n=2.2;
k1=(n*omga1)/c;
k2=(n*omga2)/c;
ode1 = diff(A1) == (2*i*(omga1^2)*d_eff*A2*conj(A1)*exp(-i*(2*k1-k2)*z))/(k1*(c^2));
ode2 = diff(A2) == (i*(omga2^2)*d_eff.*(A1.^2).*exp(i*(2*k1-k2)*z))/(k2*(c^2));
odes = [ode1; ode2];
cond1 = A1(0) == 1;
cond2 = A2(0) == 0;
conds = [cond1; cond2];
M = matlabFunction(odes)
sol = ode45(M(1),[0 20],[2 0]);
in this question both ODE are coupled, hence there's only 1 ODE to solve:
1.- use 1st equation to write
A1=f(A2,dA2/dz)
and feed this expression into 2nd equation.
2.- regroup
n1=1j*k1^4/k2^2*1/deff*.25
n2=1j*3*(k1-k2)
now the ODE to solve is
y'=n1/y^2*exp(n2*z)
3.- It can obviously be done in MATLAB, but for this particular ODE in my opinion the Wolfram online ODE Symbolic Solver does a better job.
Input the obtained ODE of previous point into the ODE solver available in this link
https://www.wolframalpha.com/input?i=y%27%27+%2B+y+%3D+0
and solve
4.- The general (symbolic) solutions for A2 are
Note that I used k1 instead of n1 and k2 instead of n2 just in the Wolfram ODE Solver.
Rewording ; the k1 k2 expressions of the general solutions are not the wave numbers k1 k2 of the equations in the question. Just replace accordingly.
5.- Now get A1 using expression in point 2.
6.- I have spotted 2 possible errors in the MATLAB code posted in the question that shouldn't be ignored by the question originator:
In the far right side of the posted MATLAB code, the exponential expressions
6.1.- both show
exp(-i(2*k1-k2)z))/(k1(c^2))
there's this (k1*(c^2)) dividing the exponent wheras in the question none of the exponentials show such denominator their respective exponents.
6.2.- the dk or delta k expression in the exponentials of the question are obviously k2-k1 or k1-k2 , here there may be room for a sign ambiguity, that may shoot a wave solution onto the opposite direction, yet the point here is where
*exp(-1i*(2*k1-k2)*z)
should probably be
*exp(-1i*2*(k1-k2)*z)
or just
exp(-1i*(k1-k2)*z)
6.3.- and yes, in MATLAB (-1)^.5 can be either expressed with 1j or 1i but as written in the MATLAB code made available in the question, since only a chunk of code has been made available, it's fair to assume that no such i=1j has been done.

Linear Regression in MATLAB without fitlm

I am tasked to perform a prediction analysis. This requires performing a linear regression on several (~10) predictor variables and coming up with intercepts for all and a constant.
so final equation will be of this format y = c + c1x1 + c2x2 + c3x3....
Now I know that you can use fitlm function in MATLAB that is available with Statistics and Machine Learning Toolbox however at this point I don't know if we will be purchasing it. How do I perform linear regression on them ?
You can use the closed form solution of linear least squares.
C=inv(transpose(X)*X)*transpose(X)*y
In the above, make the first row of X all ones, and the following rows are x1, x2,...
C will contain the corresponding constants. The first entry in C is c.
From: https://www.mathworks.com/help/matlab/data_analysis/linear-regression.html
You can write your predictor variables as a matrix X using X = [ones(length(x1),1),x1,x2,x3,...,xn] and formulating the response variables Y as the equation Y = XB and doing a matrix inverse operation using mldivide as B = X\Y to find your regression coefficients.

How can I solve equation systems in MATLAB?

This is my code to solve equation systems.
Something is wrong with my code but I can't fix it.
D=1
E=2
F=3
syms a b c;
S= solve('a+b-c=D','2*+b-3*c=E','a-2*b=F')
S = [S.a S.b S.c]
Fixing your problem with symbolic solving
Looking at the documentation for solve, you can see an example for how to input equations to the solve function. They should not be strings (like you have done), but instead they should be equations using == in place of =.
Docs example:
syms x
solx = solve(sin(x) == 1,x)
Applying this to your system:
D=1; E=2; F=3;
syms a b c;
S = solve(a+b-c==D, 2*b-3*c==E, a-2*b==F);
S = [S.a S.b S.c];
% Ouput:
% S = [5/7, -8/7, -10/7]
Note, in the above example I have replaced *+ in your second equation with *. Use either * or +, but not both! I'm assuming this was a typo, it is not the root of your problems.
Another option without using symbolic maths
You can solve this without the symbolic math toolbox too. Your equations can be written as
1a + 1b - 1c = D
0a + 2b - 3c = E
1a - 2b + 0c = F
Which, in matrix form, is the same as
1 1 -1 a D
0 2 -3 * b = E
1 -2 0 c F
Using matrix operations, this can be solved by pre-multiplying both sides by the inverse of the 3x3 matrix. In MATLAB, getting this result is easy:
% Set up 3x3 matrix of coefficients
coeffs = [1 1 -1; 0 2 -3; 1 -2 0];
% Set up left hand side vector
DEF = [1; 2; 3];
% Solve
S = coeffs\DEF;
% Ouput
% S = [0.7143, -1.1429, -1.4286]
This output is the same as before, although clearly not in exact fractional form, since that is inherited from a,b and c being symbolic.
Edit:
Some things to consider about solving matrix equations in MATLAB, as prompted by Dev-iL's comment...
The backslash \ operator I've used above is shorthand for MATLAB's mldivide function
As noted in the above linked docs, they are interchangeable:
x = mldivide(A,B) is an alternative way to execute x = A\B
Also in the docs are notes on how \ works:
If A is a square matrix, A\B is roughly equal to inv(A)*B, but MATLAB processes A\B differently and more robustly.
In particular, MATLAB automatically chooses the most efficient algorithm for solving the system. Example decision flows can be seen in the docs, I will not replicate here as it is beyond the scope of the question.
The versatility of mldivide in solving linear systems stems from its ability to take advantage of symmetries in the problem by dispatching to an appropriate solver.
In summary:
Whilst mathematically "pre-multiplying by the inverse of the coeffs matrix" gives the solution to the system, using the backslash operator (a.k.a mldivide) is the MATLAB-esque way to solve this problem.
How does this extension relate to your original question? Hopefully you are better informed about which methods can be used for this problem. In particular, know that (unless needed for your particular situation) you can do this task easily (in less lines, quicker) without relying on MATLAB's symbolic math toolbox.

Matlab Matrix Minimization

I have the following matrix
R=(A-C)*inv(A+B-C-C')*(A-C');
where A and B are n by n matrices. I want to find n*n matrix C such that the determinant of R is minimized, SO:
C=arg min (det(R));
Is there any function in MATLAB that can handle this problem?
It seems like you are trying to find the minimum of an unconstrained multivariable function. This can probably be achieved with fminunc
fun = #(x)x(1)*exp(-(x(1)^2 + x(2)^2)) + (x(1)^2 + x(2)^2)/20;
x0 = [1,2];
[x,fval] = fminunc(fun,x0)
Note that there are no examples in the documentation where a matrix is used, this is probably because horrendous performance could be expected when trying to solve this problem for a matrix of any nontiny size. (This is not because of matlab, but because of the nature of the problem).
It is also good to realize that this method does not (cannot) guarantee an optimum, only a local optimum.

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.