3N linear Equations - matlab

Given the following equation:
It will be 3N linear equations.
Each Aij is a 3x3 matrix. Xj s are 3x1 unknowns. And bi s are known 3x1 matrix.
How can I Combine 3x3 matrix to build a 3Nx3N matrix?
I'm trying to find a method to work out this question.

If you have created all of your matrices Aij and vectors bi as variables in MATLAB, you can put them all into one large system of equations AX = b by simple concatenation using square brackets and semicolons. For example, when N = 3, you can do the following:
A = [A11 A12 A13; A21 A22 A23; A31 A32 A33]; %# A 9-by-9 matrix
b = [b1; b2; b3]; %# A 9-by-1 vector
Then, once you solve your system of equations (using X = A\b; or some other method), you can break X up into its individual 3-by-1 parts. For the above example of N = 3, you can do the following:
X1 = X(1:3);
X2 = X(4:6);
X3 = X(7:9);

Related

MATLAB. How can we get a 2d multinomial by interpolation

We are given a n x n data. Approximate the funxtion.
For example. If we are given 2(1,1), 3(1,2), 4(2,1), 5(2,2)
Then we have to interpolate the 2 D- ultinomial as $0*x*y+y+2*x-1$.
This problem can be formulated as a set of linear equations, which are trivial to solve using mldivide function.
Let me illustrate this using the example you included.
% from the given example
in = [...
1,1;
1,2;
2,1;
2,2];
out = [...
2;
3;
4;
5];
% compute the variable terms in the polynomial
x = in(:,1);
y = in(:,2);
xy = x .* y;
c = ones(size(out)); % constant
% compute the coefficients of the polynomial
p = [xy,y,x,c] \ out;
% result: [0; 1; 2; -1]
The statementp = [xy,y,x,c] \ out computes the optimal coefficients (least-square error) when the problem is overconstrained (i.e. no solution exists to exactly satisfy all equations). But if there are only as many equations as there are variables (like in this example, there are 4 equations due to 4 input-output pairs and there are 4 coefficients that need to be estimated), then the coefficients can be computed simply by p = inv([xy,y,x,c]) * out.

surface plot in Matlab

I am trying to graph a surface with a diagonal matrix, the equation I am trying graph is f = x^TDx, x is a 2 by 1 vector and D is a 2 by 2 matrix.
Here is what have so far, but I keep getting error.
x = linspace(-10,10);
y = linspace(-10,10);
[X,Y] = meshgrid(x,y);
D = [1 0; 0 1];
f = #(x,y) [x,y]*D*[x,y].'; % [x,y] is 1 by 2
contour (X,Y,f(X,Y))
Can someone tell me how to get rid of the error? Thanks
Since x and y have the same length, your diagonal matrix D must be a square matrix of size n x n, with n equal to two times the length of your x or y vectors. The reason why you need to multiply the length by two is because the operation [x,y] concatenates the arrays horizontally thus duplicating one of the dimensions.
In this example D is the Identity matrix. See eye for more information.
x = linspace(-10,10); % x is 1x100
y = linspace(-10,10); % y is 1x100
[X,Y] = meshgrid(x,y); % X is 100x100 and Y is 100x100
D = eye(2*numel(x)); % D is 2*100x2*100 = 200x200
f = #(x,y) [x,y]*D*[x,y].'; % [X,Y] is 100x200 and [X,Y].' is 200x100
contour (X,Y,f(X,Y))
If you want D to be a random diagonal matrix, you can accomplish this combining diag with one of the Random Number Generation functions available, like for example randn.
On the previous example, replace D with the following instruction:
D = diag(randn(1,2*numel(x)));
You can also give the coefficients you choose to the diagonal matrix. To do so, you will need to create the vector of coefficients manually, making sure that it has the adequate length, so that it satisfies the conditions explained at the beginning of this post.
Try now replacing D with the following instructions:
v = 1:2*numel(x); % vector of coefficients: v = [1 2 ... 200]
D = diag(v);

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);

Computing an ODE in Matlab

Given a system of the form y' = A*y(t) with solution y(t) = e^(tA)*y(0), where e^A is the matrix exponential (i.e. sum from n=0 to infinity of A^n/n!), how would I use matlab to compute the solution given the values of matrix A and the initial values for y?
That is, given A = [-2.1, 1.6; -3.1, 2.6], y(0) = [1;2], how would I solve for y(t) = [y1; y2] on t = [0:5] in matlab?
I try to use something like
t = 0:5
[y1; y2] = expm(A.*t).*[1;2]
and I'm finding errors in computing the multiplication due to dimensions not agreeing.
Please note that matrix exponential is defined for square matrices. Your attempt to multiply the attenuation coefs with the time vector doesn't give you what you'd want (which should be a 3D matrix that should be exponentiated slice by slice).
One of the simple ways would be this:
A = [-2.1, 1.6; -3.1, 2.6];
t = 0:5;
n = numel(t); %'number of samples'
y = NaN(2, n);
y(:,1) = [1;2];
for k =2:n
y(:,k) = expm(t(k)*A) * y(:,1);
end;
figure();
plot(t, y(1,:), t, y(2,:));
Please note that in MATLAB array are indexed from 1.

Initialize MATLAB matrix based on indices

I'm trying to create a matrix M satisfying:
M(i,j) = f(i,j)
for some f. I can do elementwise initialization through say M = zeros(m,n) then looping. For example (in Octave):
M = zeros(m,n)
for i = 1 : m
for j = 1 : n
m(i, j) = (i+j)/2;
endfor
endfor
But AFAIK loops are not the optimal way to go with MATLAB. Any hints?
Sure!
xi = 1:m;
xj = 1:n;
Ai = repmat(xi',1,length(xj));
Aj = repmat(xj,length(xi),1);
M = f(Ai,Aj);
You can do this with any f() so long as it takes matrix arguments and does element-by-element math. For example: f = #(i,j) (i+j)/2 or for multiplication: f = #(i,j) i.*j The Ai matrix has identical elements for each row, the Aj matrix has identical elements for each column. The repmat() function repeats a matrix (or vector) into a larger matrix.
I also edited the above to abstract out vectors xi and xj -- you have them as 1:m and 1:n vectors but they can be arbitrary numerical vectors (e.g. [1 2 7.0 pi 1:0.1:20])