Exploring Inverting an nxn Matrix with MATLAB

In summary, the convolution in MATLAB can be done by using the conv() function. The input signals, represented by X(z) and Y(z), can be defined as arrays and then passed into the function. The output of the convolution will be a new array representing the result of the convolution.
  • #1
Nusc
760
2

Homework Statement



I need to find out how fast it takes MATLAB to invert an nxn matrix and:
plot t(n) vs n
plot log(time) vs. log(n)

Homework Equations





The Attempt at a Solution



for n = 1:10
A=rand(n);
tic
b=inv(A);
t(n)=toc;
end
plot(t(n),n);

Nothing shows up, what am I missing or doing incorrectly?
 
Physics news on Phys.org
  • #2
I think the semicolon after the plot command might prevent it from being displayed.

Also, when plotting y vs. x, you should use plot(x, y) and not plot(y, x)
 
  • #3
function time(n)
for i=1:n
N(i)=i;
A=rand(n,n);
tic
inv(A);
t(i)=toc;
end
plot(log(N), log(t))

I should get a straight line but I do not, do you know what's wrong?
 
  • #4
Nusc said:
function time(n)
for i=1:n
N(i)=i;
A=rand(n,n);
tic
inv(A);
t(i)=toc;
end
plot(log(N), log(t))

I should get a straight line but I do not, do you know what's wrong?

The small problem with the above code is that you're always finding the time required to invert an array of size n x n (you should probably have A=rand(i, i) instead). The big problem with your methodology is that it executes too fast! See the following MATLAB online documentation page regarding analysis of program performance:

http://www.mathworks.com/access/hel...pdesk/help/techdoc/matlab_prog/f8-790895.html
 
  • #5
If the code is executing too fast to get an accurate time, I suggest an additional loop and finding the time it takes to invert an nxn matrix 100 or 1000 times, and then just divide the total time by 100 or 1000.

I don't know the MATLAB language, but the idea is:

Code:
{code to create an nxn matix goes here}

for j=1:1000
   {code to invert the nxn matrix goes here}
end

Of course, as n gets larger you would want to reduce the number of times you invert it.
 
  • #6
function time(n)
for i=1:n
N(i)=i;
A=rand(i,i);
tic
for n=1:1000
inv(A);
end
t(i)=toc;
end

plot(N, t) - Gives me some random fluxuation increasing exponentially
plot(log(N), log(t)) - Gives me almost a straight line, how can I improve this?
 
  • #7
Nusc said:
plot(N, t) - Gives me some random fluxuation increasing exponentially
plot(log(N), log(t)) - Gives me almost a straight line, how can I improve this?

Why is this a problem? Perhaps it is supposed to be a straight line.
 
  • #8
But can we improve it?
 
  • #9
Perhaps, but we need more information than "it's not a perfect straight line" in order to make any helpful suggestions.

Can you describe the graph in more detail, or better yet post an image of it?
 
  • #10
Nusc said:
But can we improve it?

Perhaps. As a pedagogical question, what would happen if you were to take the inverse of a random matrix every time? HINT: are all matrices of equal size equally easy to row-reduce? Now, how much does it cost you to generate that random matrix? Actually, I guess it's just as easy to determine that number.
 
  • #11
how to do convolution in matlab?
X(z)=1+(z)^-1
Y(z)=2+z^-2
 

FAQ: Exploring Inverting an nxn Matrix with MATLAB

What is an nxn Matrix?

An nxn matrix is a mathematical representation of a collection of numbers arranged in a rectangular grid. The size of the matrix is determined by the number of rows (n) and columns (n) it contains. It is commonly used in various fields of science and engineering to represent data and solve equations.

What does it mean to invert a matrix?

Inverting a matrix is the process of finding another matrix, called the inverse, that when multiplied by the original matrix, results in the identity matrix (a square matrix with ones on the main diagonal and zeros elsewhere). This is similar to finding the reciprocal of a number in basic arithmetic.

Why is it important to explore inverting a matrix?

Inverting a matrix can be useful in solving systems of linear equations, calculating determinants, and finding solutions to various mathematical problems. It is also a fundamental concept in linear algebra and has many applications in fields such as physics, engineering, and computer science.

How can MATLAB be used to invert an nxn matrix?

MATLAB is a software program that allows for efficient and accurate computation of mathematical operations, including inverting matrices. It has built-in functions such as "inv" and "pinv" that can be used to calculate the inverse of a matrix. Alternatively, the backslash operator (\) can be used to solve systems of linear equations, which involves inverting a matrix.

What are some challenges that may arise when exploring inverting an nxn matrix with MATLAB?

Some challenges that may arise when using MATLAB to invert an nxn matrix include dealing with large or complex matrices, handling numerical errors, and understanding the underlying mathematical principles. It is important to have a good understanding of linear algebra and proper programming techniques to ensure accurate and efficient results.

Similar threads

Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
10
Views
2K
Replies
10
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
3
Views
1K
Back
Top