Question about error: Subscript indices must either be real positive integers or logicals.
    2 views (last 30 days)
  
       Show older comments
    
Hello all,
I am new to matlab and am currently writing code to solve a system of nonlinear functions using the Newton Raphson method. In my code I am dealing with creating a jacobian and solving using LU factorization.
I am putting this command into the command window which calls for functions from file 'test':
NR_PartA( 'test', [ 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0], 0.00001, 0.0001)
However, I am getting this error:
__Subscript indices must either be real positive integers or logicals.
Error in NR_PartA (line 13) yguess = myfunc(guess); %Initial values for y matrix in forward substitution__
I was wondering if anyone could help me out in solving this problem?
Here is my code:
%Define a function that will read in:
%an m file containing n number of nonlinear functions
%an initial guess for the functions
%A value that will determine if convergence to a solution has occured
%(tolerance)
%The value for iteration for each guess
function K = NR_PartA(myfunc, guess , epsilon, step) 
%maxiteration = 100;   %define the maximum number of iterations for the function
n = length(guess); %define the number of functions based on the test file input
deltay = zeros(1,n);  % delta F for initial Newton Raphson guess
yguess = myfunc(guess); %Initial values for y matrix in forward substitution
initialguess = zeros(1,n);
Jacob = zeros(n,n); %Set up the initial Jacobian Matrix to a matrix of zeros
func = 1; %set func to 1 so that while loop has a valid argument
while func
%Find new value of delta y
for i = 1:1:n
    %for number of equations in myfun, set delta y
    deltay(i) = initialguess(i) - yguess(i);
end
%Exit loop if delta y has converged to an acceptable tolerance (epsilon)
if abs(deltay) < epsilon
func = 0;
break;
end
%Develop the Jacobian matrix
for i=1:1:n
    %Fill in the empty matrix by moving column by column, line by line
    for j =1: 1:n
        x1 = guess(j);
        %set arguments for jacobian elements
        xplus = x1 + step;
        xminus = x1 - step;
          deltax1 = guess;
          deltax1(j) = xplus;
          deltax2 = guess;
          deltax2(j) = xminus;
    fguess1 = myfunc(deltax1);
    fguess2 = myfunc(deltax2);
    guess1 = fguess1(i); 
    guess2 = fguess2(i);
  %Find each element of the jacobian  
    J = (guess1 - guess2)/ (2*step);
    Jacob(i,j) = J; 
      end
  end
  %Sparse Jacobian to make function faster
    JJ = sparse(Jacob);
    [L,U,P] = lu(JJ); %define lower and upper matrixes for LU factorization
    b = deltay;
    c = zeros(n,1);
    b = deltay*P;
   %forward substitution 
    for i=1:n
        c(i)= (b(i)-L(i, :)*c)/L(i,i); 
    end
    %set a matrix of zeros for new guess
    newguess = zeros(n,1);
    %backward substitution
    for i=n:-1:1
        newguess(i) = (c(i)-U(i,:)*newguess)/U(i,i);
        guess(i) = guess(i) + newguess(i);
    end
    yguess = myfunc(guess);
  end
  %Output the value of the variables after convergence has occured
  K = guess;
  end
Sorry if I am missing information for you guys. Let me know if you need anything else.
Thank you for your time.
0 Comments
Accepted Answer
  Guillaume
      
      
 on 25 Feb 2015
        double [0 1 0 1 0 ...] is not the same as logical [0 1 0 1 0 ....], assuming you were intending to pass logical to your function, you'll have to declare your array as such:
NR_PartA( 'test', logical([1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0]), 0.00001, 0.0001)
Or use true and false instead of 0, 1 in your array.
0 Comments
More Answers (2)
  Image Analyst
      
      
 on 25 Feb 2015
        I didn't delve into the code but I'm 100% certain it will be explained well by the FAQ http://matlab.wikia.com/wiki/FAQ#How_do_I_fix_the_error_.22Subscript_indices_must_either_be_real_positive_integers_or_logicals..22.3F
0 Comments
See Also
Categories
				Find more on Systems of Nonlinear Equations in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
