How do I solve a determinant in MATLAB? - matlab

As a simple example, let's say you have this matrix:
M = [omega 1;
2 omega];
and you need to solve for the values of omega that satisfy the condition det M = 0.
How do you do this in MATLAB?
It is surely something simple, but I haven't found the function yet.

For the general case where your matrix could be anything, you would want to create a symbolic representation of your matrix, compute the determinant, and solve for the variable of interest. You can do this using, respectively, the functions SYM, DET, and SOLVE from the Symbolic Math Toolbox:
>> A = sym('[w 1; 2 w]'); % Create symbolic matrix
>> solve(det(A),'w') % Solve the equation 'det(A) = 0' for 'w'
ans =
2^(1/2)
-2^(1/2)
>> double(ans) % Convert the symbolic expression to a double
ans =
1.4142
-1.4142
There are also different ways to create the initial matrix A. Above, I did it with one string expression. However, I could instead use SYMS to define w as a symbolic variable, then construct a matrix as you normally would in MATLAB:
syms w
A = [w 1; 2 w];
and now A is a symbolic matrix just as it was in the first example.

If you don't have the symbolic toolbox, then use the sympoly toolbox, found on the file exchange.
sympoly omega
roots(det([omega 1;2 omega]))
ans =
-1.4142
1.4142

Well the determinate is:
om * om - 1*2 = 0
So you would get: om*om = 2
The formal definition is: [a b ; c d] = ad - bc
I would look into simplifying the determinate, and finding a solver to solve for the unknowns.

Related

MatLab Quadratic Equation With ln

How to solve the function f(x)=ln(x^2)-0.7=0 with a known Matlab command?
clc;clear all;close all;
f(x)=ln(x^2)-0.7=0
B=sqrt f(x)
You can use symbolic variables together with the solve function:
syms x;
eqn = log(x^2) - 0.7 == 0;
solve(eqn,x)
The above code will output:
ans =
exp(7/20)
-exp(7/20)
Since the equation is quadratic, the solver returns two distinct solutions (often people forget that quadratic equations may have two specular solutions, one positive and one negative).
If you want to retrieve the numerical values (for example, in order to calculate their sqrt value):
sol = solve(eqn,x);
num = double(sol)
num =
1.4191
-1.4191
Put the following code into a MATLAB script, name it "main.m".
function b=main
clc
x=solveF()
y=f(x)
b=sqrt(y)
end
function y=f(x)
y=log(x^2)-0.7
end
function x=solveF()
g = #(x) abs(f(x)-0)
x = fminsearch(g, 1.0)
end
Then run it as:
main
You will get the results:
x =
1.4190
y =
-3.4643e-05
b =
0.0000 + 0.0059i
ans =
0.0000 + 0.0059i
You can define equations in matlab as such:
f = #(x) log(x^2)-0.7;
B = #(x) sqrt(f(x));
If you want to find the value of x satisfying a constraint you can design a function that will be equal to zero when the constraint is respecte, then call fminsearch to find x:
f_constraint = #(x) abs(f(x)-0);
x_opt = fminsearch(f_constraint, 1.3); % function handle, initial estimate
In your example, B(x_opt) should be equal to zero. This is not exactly the case as fminsearch estimated a solution.

Evaluate symbolic matrix in MATLAB

I defined 2 symbolic matrix in MATLAB, for example
w = sym('w',[10,10])
Then I do some operations on it and get function E dependent to symbolic matrix w and v. Now, I want to evaluate E numerically with numerical w and v.
How can I do this?
Simple example on R2017a:
>> syms E(v,w)
>> E(v,w) = v*w + v;
>> E(3,4)
ans =
15
On earlier versions, I believe symfun is the command to use.
We can use such code:
>> w=sym('w',[10 10]);
>> d=sym('d',[10 1]);
>> E=W*d + ...(some other operations)
>> define a numerical matrix f and vector x)
>> subs(subs(E,w,f),d,x)
This code performed in R2014a and had a correct answer.

How to use GMRES to Matrices rather then vectors?

The GMRES algorithm and its matlab implementation are supposed to solve linear equations system, such as
%Ax = b
A = rand(4);
b = rand(4,1);
x = gmres(A,b);
One can also use a function handle
foo = #(x) A*x + conj(A)*5*x;
y = gmres(foo,b);
What I want is to solve the following
B = rand(4);
H = rand(4);
foo2 = H*B + B*H;
X = gmres(foo2, B) %Will not run!
--Error using gmres (line 94)
--Right hand side must be a column vector of length 30 to match the coefficient matrix.
Mathematically speaking I don't see why gmres couldn't apply to this problem as well.
Note: What I'm really trying to solve is an implicit euler method for a PDE dB/dt = B_xx + B_yy, so H is in fact a second derivative matrix using finite difference.
Thank you
Amir
If I've understood right you want to use GMRES to solve an a sylvester equation
A*X + X*A = C
for n-by-n matrices A, X and C.
(I asked a related question yesterday over at SciComp and got this great answer.)
To use GMRES you can express this matrix-matrix equation as a size n^2 matrix-vector equation. For convenience we can use the Kronecker product, implemented in MATLAB with kron:
A = randn(5);
X = randi(3,[5 5]);
C = A*X + X*A;
% Use the Kronecker product to form an n^2-by-n^2 matrix
% A*X + X*A
bigA = (kron(eye(5),A) + kron(A.',eye(5)));
% Quick check that we're getting the same answer
norm(bigA*X(:) - C(:))
% Use GMRES to calculate X from A and C.
vec_X_gmres = gmres(bigA,C(:));
X_gmres = reshape(vec_X_gmres,5,5);

Matlab quadprog parameters for L1 Regularization

I've been struggling with the parameterization of quadprog function in Matlab for a problem like this:
where x is a matrix of x 0 w 0 +x 1 w 1 +x 2 2 w 2 and y is the target vector containing a value for each row of x, w is the weight vector, lambda is a scalar value.
I have tried this, but I'm sure it's not correct:
N = size(x, 2);
Sigma = cov(x);
H = 2.0*Sigma;
c = zeros(N,1);
quadprog(H, c)
Could someone please guide me with what the parameters should be for the quadprog function?
This optimization problem is known as Lasso, and as you wrote it it is not formally a quadratic program. You can either convert it to a quadratic program, see this link, or you can use Matlab's builtin lasso (part of Statistics toolbox)

System of equations for Eigenvalues and Eigenvectors

An Eigenvalue and Eigenvector can be derived from the Tensor T by the below equation.
I am trying to get a system of equations for Eigenvalues, Eigenvectors and the Tensor T to derive T.
T matrix equation is:
(T(i,k)-L(r)*I) * A(r,k) = 0
The first entries should be:
[(T11-L1)*A11 T12*A12 T13*A13 T14*A14 ]
[T21*A11 (T22-L1)*A12 T23*A13 T24*A14 ]
[T31*A11 T32*A12 (T33-L1)*A13 T34*A14 ]
[T41*A11 T42*A12 T43*A13 (T44-L1)*A14]
First, let's declare the symbolics easier using sym:
T = sym('T%d%d', [4 4]);
A = sym('A%d%d', [4 4]);
L = sym('L', [4 1]);
There are several problems with the original code; 1. f is being replaced in each inner iteration. 2. The inner result should be scalar and thus I must not appear there. (Note that you can also define I like eye(4) instead of writing it manually.)
Here is the corrected version:
f = cell(4,1); % Initialize equation system
for r = 1:k
for k = 1:4
for i = 1:4
f{r}(i,k) = T(i,k) * A(r,k);
end
end
f{r} = f{r} - L(r)*diag(A(r,:));
end
f{i} would be the ith slice.
Note: As #Schorsch pointed out (and Matlab also shows a warning) always try to use another variable name other than i (or j), since they represent the imaginary unit.
Just for fun you can use repmat to remove the two inner loops:
for r = 1:4
f{r} = T .* repmat(A(r,:), [4 1]) - L(r)*diag(A(r,:));
end