Problem with multiple for loops
    8 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
for i=1:n
    for j=1:m
        for k=1:m
             if(k~=j)
                  rij(i,k)=x(i,j)-x(i,k);
                  fij(k) = double((48 /((rij(i,k))^13))-(24 /((rij(i,k))^7)));  
                  p=p+1;
               elseif (k==j)
                       fij(k)=0;
                   else
                       disp('Something went wrong')
               end  
          end
          f(j)=sum(fij);
          xddot(i,j)=f(j)/m;
          x(i+1,j)=x(i,j)+ ((xdot(i,j))*delt);
          xdot(i+1,j)= xdot(i,j) + ((xddot(i,j))*delt);
      end
  end
Above shown is my code and the problem is, in the innermost loop the program is not running the if command properly and just runs the elseif command and gets out of the inner for loop and never returns as it should. Anyone with a solution!
0 Commenti
Risposte (2)
  Jose Jeremias Caballero
      
 il 31 Dic 2011
        Observation. 
Since k and j are natural numbers, 
they may be identical or different. 
Now if k is different from j then we do.
rij (i, k) = x (i, j)-x (i, k); 
fij (k) = double ((48 / ((rij (i, k)) ^ 13)) - (24 / ((rij (i, k)) ^ 7))); 
p = p +1; 
contrast model is done 
fij (k) = 0 
So the other option is meaningless.
0 Commenti
  Dr. Seis
      
 il 31 Dic 2011
        So after you run the code above (and assuming you initialized "p = 0;" before the code) you get "p" still equal to 0 after the code has completed?
p should be equal to n*(m^2-m), so the only time p should equal 0 in the code above is if m = 1. Is m > 1?
0 Commenti
Vedere anche
Categorie
				Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!