1

I don't understand how to solve the problem in octave. There are a main file and a gauss function that solves the matrix using the Gaussian method. The x results are stored in the "res" array. At the end of the gauss function, I want to display the result on the screen, but I get this error.

main.m

n9=9;
n10=10;
A1 = [61, -82, 35, -48, -40, -43, -50, -1, -92;
      -39, -99, -75, -75, 26, -1, 42, 21, 10;
      85, -88, 76, 48, -8, 90, 94, -86, -24;
      28, 8, -31, -29, 94, 98, -95, 96, 50;
      86, -37, 12, 14, -99, -26, 33, 17, 88;
      24, -30, -51, -57, 14, -53, 58, 21, -92;
      7, -9, -43, 69, -82, 56, -30, 100, -44;
      24, 37, 98, -77, -55, 16, 41, 12, 46;
      81, 79, 8, -70, 65, -76, -39, -82, -54]

A2 = [268, 74, -71, 11, -104, 16, -33, 64, 7, 11;
      74, 375, -118, 230, -51, -212, 180, 204, 163, -35;
      -71, -118, 400, -122, -81, 72, -201, 110, 20, 333;
      11, 230, -122, 239, -47, -40, 189, 188, 57, -69;
      -104, -51, -81, -47, 325, -53, -13, -222, -27, -118;
      16, -212, 72, -40, -53, 335, -64, -60, -110, 73;
      -33, 180, -201, 189, -13, -64, 325, 47, -8, -84;
      64, 204, 110, 188, -222, -60, 47, 446, 123, 123;
      7, 163, 20, 57, -27, -110, -8, 123, 379, 65;
      11, -35, 333, -69, -118, 73, -84, 123, 65, 378]

A3 = [-327, 106, 0, 0, 0, 0, 0, 0, 0, 0;
      -104, -512, 92, 0, 0, 0, 0, 0, 0, 0;
      0, -5, 56, 6, 0, 0, 0, 0, 0, 0;
      0, 0, 42, 501, 26, 0, 0, 0, 0, 0;
      0, 0, 0, 2, 167, -70, 0, 0, 0, 0;
      0, 0, 0, 0, 218, 439, -64, 0, 0, 0;
      0, 0, 0, 0, 0, -145, 943, 365, 0, 0;
      0, 0, 0, 0, 0, 0, -8, -91, -26, 0;
      0, 0, 0, 0, 0, 0, 0, -143, -749, 169;
      0, 0, 0, 0, 0, 0, 0, 0, 219, 566]
b1 = [67, 94, -83, 76, -93, -79, -18, 94, -77]
b2 = [420, 19, 384, 83, 108, 264, 80, 14, -215, 394]
b3 = [-981, 687, 845, 542, -915, -244, 409, 459, -552, -462]

gauss(A1, b1, n9)

gauss.m

function result = gauss (a, b, n)
  res = [9];
  temp = 0;
  for k = 1:n,
    for i = k+1:n,
      for j = k:n,
        if j == k,
          temp = a(i,j) ./ a(k,j);
          a(i,j) = a(i,j) - a(k,j) .* a(i,j) ./ a(k,j);
          b(i) = b(i) - b(k) * temp;
        else
          a(i,j) = a(i,j) - temp .* a(k,j);
        endif;
      end;
    end;
  end;
  for i = n:1,
    for j = n-1:0,
      if a(i, j) != 0,
        if i == j,
           res = b(j) ./ a(i,j);
        else
           b(i) = b(i) - a(i,j) .* res(j);
        endif;
      endif;
    end;
  end;

  for i = 1:9,
    printf("%f", res(i))
  end;

endfunction

I have tried declaring the "res" array in different ways

Mary
  • 205
  • 1
  • 2
  • 6
Michael
  • 11
  • 1

1 Answers1

1

you should always copy/paste the full error message including line numbers, etc., so it tells us exactly what octave is telling you.

my guess: in gauss.m you have a line saying:

b(i) = b(i) - a(i,j) .* res(j);.

You define res as a 1x1 scalar (res = [9]). In that line above, you call for res(j). If res only has 1 element, there will be an error if you ask it for more than 1 element. (there is no res(2), which is what your error message you gave in the title to this question is trying to tell you.) Because this is an incrementing for loop, you pass in n = 9, the j loop goes from j = n-1:0, there will be many possible values of j that could produce errors. Now I see that there are places that res can change. If you expect that to make res larger, before the res(2) call happens, it appears that it did not do so.

Also, without looking too deeply into what the code is doing, your for loop has j going to 0. Octave indexing starts at 1. So any time you call res(0) you're going to get an error. You should ensure that doesn't happen.

Nick J
  • 1,310
  • 12
  • 25