I have the equation AX * C = AXB where all of the variables are square gf matrices of size n and the values are 0 or 1. AXB and AX are known, while C should be solved to B or an equivalent of it (there should be multiple solutions). Because X most likely isn´t regular (rank < n) not all of the variables in C can be calculated and i have to guess the rest after solving.
Question is: Can Matlab solve this equation (as far as possible), and in a better way than "manually" splitting the equation into n^2 equations?
I already tried to tell matlab that C is a symbol of, let´s say [8,8] and to solve AX*C==AXB, but solve doesn´t seem to work with galois fields.
Any hints on how to do this would be appreciated.
Related
I have an equation of the form A*X = X*A', where A and X are real, square matrices (3x3 in that case) and A is known and A' represent the transpose of A. How to solve for X using MATLAB? (up to a scale factor)
This is a Sylvester equation. However, it is singular because the eigenvalues of A and A' are the same. But you can use the formulae
[I⊗A-A'⊗I]X(:)=C(:):
m=kron(eye(3),a)+kron(-a,eye(3))
v=null(m)
x1=reshape(v(:,1),[3 3])
x2=reshape(v(:,2),[3 3])
x3=reshape(v(:,3),[3 3])
Now the solution is span{x1,x2,x2}, i.e. any matrix of the form
b x1 + c x2 +d x3, where b,c,d are any real numbers
I don't think Matlab has facilities for symbolic algebra.
If you expand A and X, and work through the expression, you obtain an 3x3 matrix with equation in several unknowns, all of which are zero. You then solve that.
But I don't think Matlab allows you to set a matrix to a symbol, rather than an value and expand it for you. For this simple case, you could easily write such a function, that multiples a string matrix by a numerical matrix. The snag is it's hard to scale it up to the general case without throwing the entire Maple / Mathematica engine at it.
I want to obtain the natural frequencies of a simple mechanical system with mass matrix M and stiffness matrix K (.mat-file -> Download):
Mx''(t)+Kx(t)=0 (x= Position).
It means basically, that I have to solve det(K-w^2*M)=0. But how can I solve it in Matlab (or if necessary reduce it to a standard eigenvalue problem and solve it then)? The matrices are definitely solvable with Abaqus (FEM Software), but I have to solve it in Matlab.
I tried the following without success: det(K-w^2*M)=0 => det(M^-1*K-w^2*I)=0 (I := unity matrix)
But solving this eigenvalue problem with
sqrt(eigs(K*M^-1))
delivers wrong values and the warning:
"Matrix is singular to working precision.
In matlab.internal.math.mpower.viaMtimes (line 35)"
Other wrong values can be obtained via det(K-w^2*M)=0 => det(I/(w^2)-M*K^-1)=0:
1./sqrt(eigs(M*K^-1))
Any hint would help me. Thanks in advance.
As #Arpi mentioned, you actually want to solve the generalized eigenvalue problem:
K*x = w^2*M*x
Since your matrices K and M are apparently singular (or just one of them), it is not possible to use eigs, but you have to use eig:
V = eig(K,M);
w = sqrt(V);
I've been looking around but I can't seem to figure out how I should use fsolve to solve my system of nonlinear equations.
so I have a function k, and I know that I want .01 as the starting value (required input for fsolve I believe). I also know that k(1000) =12
Lastly, I have a formula for k,
k(N) = (1/(k(N).^.5 + .9*k(N) -k(N+1))) - ((.94 * .5 *k(N+1)^(1-.5) + .9)/(k(N+1)^.5 +
.9*k(N+1) - k(N+2)))
with N 1:1000. I figured I could just say fsolve(k(N), .01), but with the formula in place of k(N) (or is defining k(N) in a separate function necessary?), but I exceed the dimensions of N and also have issues with dimensions matching since there is N, N+1, and N+2 in the equation.
I think my issue may be stemming from the fact that I have defined vector valued functions where in reality I want them to be single valued, from 1:1000,but I don't know how to represent this in matlab (obviously I can't type in 1000 equations).
Any ideas, suggestions, or comments?
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.)
x=[1;2;3]
x =
1
2
3
y=[4;5;6]
y =
4
5
6
x\y
ans =
2.2857
How did Matlab find that result ? (I searched many forums but I did not understand what they told.I would like to know the algorithm which gave this result.)
From MATLAB documentation of \:
If A is an M-by-N matrix with M < or > N and B is a column vector with M components, or a matrix with several such columns, then X = A\B is the solution in the least squares sense to the under- or overdetermined system of equations A*X = B.
Here your system is not under/over-determined. Since both have 3 rows. So you can visualize your equation as:
xM=y
M=inv(x)*y
Now, since your matrix is not square, it will calculate the pseudo-inverse using SVD. Therefore,
M=pinv(x)*y;
You will get value of M as 2.2857.
Another explanation can be: It will give you the solution of xM=y in the sense of least squares. You can verify this as follows:
M=lsqr(x,y)
This will give you the value of M = 2.2857.
You can always do help \ in MATLAB command window to get more information.
You are encouraged to check more details about the least squares and pseudo-inverse.
This documentation should explain it
http://www.mathworks.com/help/matlab/ref/mrdivide.html
Here is a link to the algorithm
http://www.maths.lth.se/na/courses/NUM115/NUM115-11/backslash.html
You can see the source inside matlab much more easily though. (I don't have it locally so I can't check but the source of a lot of matlab functions is available inside matlab)