How to solve the system of equation (simultanious) in matlab? - 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.

Related

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.

A\b or b\A for A*x=b in MATLAB

A solution of A*x = b can be obtained by x = A\b. But I did x = b\A as I did not look for exact syntax. I got different vectors x in with A\b and b\A. I understood that A\b is more or less equivalent to inv(A)*b, but I don't understand what exactly is happening if I do b\A. Does anyone know about it?
From the MATLAB documentation:
If A is a rectangular m-by-n matrix with m ~= n, and B is a matrix with m rows, then A\B returns a least-squares solution to the system of equations A*x= B.

How to solve A*X - X*A' = 0

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.

Solving linera equation AX=B in matlab

I have to solve following linear equation in Matlab
AX=B for X, where A - symetric positive definite upper triangular matrix (nxn), and B is matrix (mxn).
So far I have got code for solving such eqation where B is a vector. I have to change the code, to be able to calculate it for B as a (mxn) matrix
x(n)=b(n)/A(n,n);
for i=n-1: -1:1,
s=b(i);
for j=i+1:n,
s=s-A(i,j)*x(j);
end
x(i)=s/A(i,i);
end

generation of normally distributed random vector with covariance matrix

In matlab it is easy to generate a normally distributed random vector with a mean and a standard deviation. From the help randn:
Generate values from a normal distribution with mean 1 and standard
deviation 2.
r = 1 + 2.*randn(100,1);
Now I have a covariance matrix C and I want to generate N(0,C).
But how could I do this?
From the randn help:
Generate values from a bivariate normal distribution with specified mean
vector and covariance matrix.
mu = [1 2];
Sigma = [1 .5; .5 2]; R = chol(Sigma);
z = repmat(mu,100,1) + randn(100,2)*R;
But I don't know exactly what they are doing here.
This is somewhat a math question, not a programming question. But I'm a big fan of writing great code that requires both solid math and programming knowledge, so I'll write this for posterity.
You need to take the Cholesky decomposition (or any decomposition/square root of a matrix) to generate correlated random variables from independent ones. This is because if X is a multivariate normal with mean m and covariance D, then Y = AX is a multivariate normal with mean Am and covariance matrix ADA' where A' is the transpose. If D is the identity matrix, then the covariance matrix is just AA' which you want to be equal to the covariance matrix C you are trying to generate.
The Cholesky decomposition computes such a matrix A and is the most efficient way to do it.
For more information, see: http://web.as.uky.edu/statistics/users/viele/sta601s03/multnorm.pdf
You can use the following built-in matlab function to do your job
mvnrnd(mu,SIGMA)