How can I efficiently invert an nxn matrix in MATLAB and visualize the results?

  • Thread starter Thread starter Nusc
  • Start date Start date
  • Tags Tags
    Matlab Matrix
Click For Summary

Discussion Overview

The discussion revolves around efficiently inverting an nxn matrix in MATLAB and visualizing the timing results through various plots. Participants explore coding techniques, performance issues, and the implications of their results.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant shares an initial attempt to measure the time taken to invert a matrix and plot the results, but encounters issues with the plot not displaying.
  • Another participant suggests that a semicolon after the plot command might prevent the display and emphasizes the correct usage of plot parameters.
  • A participant proposes a function to measure inversion time but questions the results, noting that they expect a straight line in the log-log plot.
  • Concerns are raised about the accuracy of timing due to the speed of execution, with a suggestion to repeat the inversion multiple times to obtain a more reliable measurement.
  • Further code adjustments are discussed, including changing the matrix size dynamically within a loop to ensure proper timing measurements.
  • Participants express confusion over the resulting plots, with one noting random fluctuations in the results and another questioning the nature of the expected output.
  • There is a suggestion to consider the variability in the difficulty of inverting different random matrices and the cost of generating them as factors affecting the results.

Areas of Agreement / Disagreement

Participants generally agree on the need for accurate timing methods and the importance of proper coding practices, but there is no consensus on the best approach to achieve a straight line in the log-log plot or the interpretation of the results.

Contextual Notes

Some participants note limitations in their methodologies, such as the execution speed affecting timing accuracy and the need for more detailed descriptions of the resulting graphs to facilitate further discussion.

Who May Find This Useful

Individuals interested in MATLAB programming, particularly in the context of matrix operations, performance analysis, and data visualization techniques.

Nusc
Messages
752
Reaction score
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
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)
 
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?
 
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
 
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.
 
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?
 
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.
 
But can we improve it?
 
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
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K