0

I have written the following piece of code:

    M = [3 0 0; 0 2 0; 0 0 0.5]    % mass matrix

    i_vals = 1:1000:60e06;    % values of k_12 from 1 to 600 million in steps of 1000
    modes = zeros(3, length(i_vals));

    for n=1:length(i_vals)
        i = i_vals(n)    % i is the value of k_12
        K = [i+8e06 -i -2e06; -i i+2e06 -1e06; -2e06 -1e06 5e06];    % stiffness matrix

        [V,L]=eig(K,M);         
        V(:,I)=V;               
        A = V(:, [1])
        transpose(A)
        modes(:, n) = A

    end
    loglog(i_vals, modes')

But the loop seems to go forever and I do now know what is wrong with it. The idea was to get the first column from matrix V, and see what happens to the 3 elements in this column when value of k_12 is changed.

  • You do realize that you have to go around the loop 60,000 times? What is `V(:, I) = V;` supposed to do? Won't that cause `V` to grow on every iteration? All that copying to bigger and bigger arrays can really slow things down. Does this work when you make i_vals much smaller? (say stop at 6001, so just 6 loops)? – Floris Dec 12 '13 at 19:34
  • (And strictly speaking, `60e6` is _60_ million, not 600) =) – Stewie Griffin Dec 12 '13 at 20:04
  • @user2579288, please consider accepting one of answers below. As it stands, this question appears to be unanswered. – Stewie Griffin Aug 30 '15 at 15:35

2 Answers2

4

I don't know how you make this run forever. To me it looks as if it won't run at all. This won't answer your question, but will hopefully help you on the way =)

  1. What do you want to do with this line? V(:,I)=V; What is I? Was it supposed to be i? Btw, using i and j as variables in MATLAB is not recommended (however, if you don't use complex numbers in your field, you shouldn't care too much).
  2. You have a loop that goes 60,000 times, with calculations of eigenvalues etc. That is bound to take time (although not forever, as you state it does). You should get the answer eventually (if only the rest of the code worked). The resolution of your plot would be more than accurate enough with 10,000 or even 100,000 steps at a time.

This part:

   A = V(:, [1])
   transpose(A)
   modes(:, n) = A

could simply be written as:

modes(:,n) = V(:,1)';

assuming you want the transposed of A. transpose(A) does nothing in this context actually. You would have to do A = transpose(A) (or rather A = A') for it to work.

Community
  • 1
  • 1
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • 1
    Well - we were writing very similar thoughts at the same time... I'm going to have to upvote you for that. – Floris Dec 12 '13 at 19:53
4

There are all kinds of problems with your code - some of which may contribute to your issue.

  • You are computing values of i on a linear scale, but ultimately will be plotting on a log scale. You are doing a huge amount of work towards the end, when there is nothing visible in the graph for your effort. Much better to use a log scale for i_vals:

i_vals = logspace(0, 7.778, 200); % to get 200 log spaced values from 
                                  % 1 to approx 60E6`
  • You are using a variable I that has not been defined (in the code snippet you provide). Depending on its size, you may find that V is growing...
  • You are using a variable name i - while that is legal, it overwrites a built in (sqrt(-1)) which I personally find troublesome.
  • Your transpose(A); line doesn't do anything (You would have to do A = transpose(A);).
  • You don't have ; after several lines - this is going to make Matlab want to print to the console. This will take a huge amount of resource. Suppress the output with ; after every statement.

EDIT the following program runs quickly:

M = [3  0  0.0; 
     0  2  0.0; 
     0  0  0.5];   % mass matrix

i_vals = logspace(0, 7.78, 200);    % values of k_12 from 1 to 600 million in steps of 1000
modes = zeros(3, length(i_vals));

for n=1:length(i_vals)
    i = i_vals(n);    % i is the value of k_12
    K = [i+8e06 -i -2e06; -i i+2e06 -1e06; -2e06 -1e06 5e06];    % stiffness matrix

    [V,L]=eig(K,M);         

    modes(:, n) = V(:,1);

end

loglog(i_vals, modes')

Resulting graph: enter image description here

If I didn't break anything (hard to know what you were doing with I), maybe this can be helpful.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • 1
    [Flashback...](http://stackoverflow.com/questions/17134389/explanation-of-the-matlab-coding/17134786#17134786) =P – Stewie Griffin Dec 12 '13 at 20:06
  • @RobertP. - where did you dig that one up... Are you saying there's something about the way I write answers? :-) – Floris Dec 12 '13 at 20:09
  • Nope... Me answering by pointing out problems with code, you following up a few minutes later with "best practice"... =) Nice answer btw! – Stewie Griffin Dec 12 '13 at 20:10
  • That was _only_ meant as a compliment! As in, I post an answer, you post a better, more comprehensive one. I see that it might not look that way from the way I wrote it... – Stewie Griffin Dec 12 '13 at 20:23