1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB Help

  1. Jan 21, 2009 #1
    1. The problem statement, all variables and given/known data

    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)

    2. Relevant equations



    3. 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?
     
  2. jcsd
  3. Jan 22, 2009 #2

    MATLABdude

    User Avatar
    Science Advisor

    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)
     
  4. Jan 22, 2009 #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?
     
  5. Jan 23, 2009 #4

    MATLABdude

    User Avatar
    Science Advisor

    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
     
  6. Jan 23, 2009 #5

    Redbelly98

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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 (Text):


    {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.
     
  7. Jan 23, 2009 #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?
     
  8. Jan 23, 2009 #7

    Redbelly98

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    Why is this a problem? Perhaps it is supposed to be a straight line.
     
  9. Jan 24, 2009 #8
    But can we improve it?
     
  10. Jan 24, 2009 #9

    Redbelly98

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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?
     
  11. Jan 24, 2009 #10

    MATLABdude

    User Avatar
    Science Advisor

    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.
     
  12. Feb 17, 2009 #11
    how to do convolution in matlab?
    X(z)=1+(z)^-1
    Y(z)=2+z^-2
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: MATLAB Help
  1. Matlab HELP ! (Replies: 4)

  2. Matlab help (Replies: 2)

  3. Matlab help (Replies: 2)

  4. Help with matlab (Replies: 7)

  5. MATLAB help (Replies: 2)

Loading...