How can I create a for loop inside another for loop for a geometric series?
    5 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Amy
 el 28 de Feb. de 2014
  
    
    
    
    
    Comentada: Amy
 el 10 de Mzo. de 2014
            I have the following code so far:
    %alphaSum.m
    %
    %
    %Amy Wallace, 19/02/2014
    %Variable dictionary
    %N Number of terms to sum
    %alpha Sum of geometric series
    %x vector of constants
    %n Loop counter
    N = input('Enter the number of terms to sum: ');
    alpha = 0;
    x = [0.9 0.99 0.999 0.9999 0.99999 0.999999 0.9999999];
    for n = 0:N-1
        alpha = alpha + (x.^(n));
    end
    format long
    alpha
This allows the user when the script is ran in the command window to enter one term of N to sum. I was wondering, if by using nested looping I could possibly use multiple N terms to sum at the one time? I have already tried calling N as a string with no luck. Thanks
0 comentarios
Respuesta aceptada
  Mischa Kim
    
      
 el 28 de Feb. de 2014
        
      Editada: Mischa Kim
    
      
 el 28 de Feb. de 2014
  
      Amy, check out
Nin = inputdlg('Enter space-separated numbers:', 'N vector', [1 50]);
N   = str2num(Nin{:});
x   = [0.9 0.99 0.999 0.9999 0.99999 0.999999 0.9999999];
alpha = zeros(length(N), length(x));
for ii = 1:length(N)
    for n = 0:N(ii)-1
        alpha(ii,:) = alpha(ii,:) + (x.^(n));
    end
end
Más respuestas (0)
Ver también
Categorías
				Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

