Octave #1

Hey I was just home early from work today and decided I would put up a port of a numerical analysis project I completed a while back. On Rhomberg Integration is a numerical way of doing integration not as accurate as you would by hand, but you cant tell a computer to get a pen and paper out can you…. wait you probably can… but the fact remains I don’t know how. Anyways I done this the first time around in Matlab but i dont use it any-more due to the fact it costs thousands of pounds to get and therefore i used Octave instead and its very nice i have to say it feels alot like matlab and its proper Free software which is a bonus. Anyways here is the code:

  1. #Rhomberg.m
  2. #Rhomberg Integration in Ocatave :)
  3. #Not so nicely coded but hey it seems to work and integrate it right
  4. #Try Rhomberg(0,pi,8)
  5.  
  6. function r = Rhomberg(a,b,n)
  7.   disp("Rhomberg Integration");
  8.  
  9.   h=b-a;
  10.   r(1,1)= h/2*(f(a)+f(b));
  11.   disp(r);
  12.  
  13.   for i=2:n
  14.     tmp=0;
  15.     for k=1:2^(i-2)
  16.       tmp += f(a +(k-0.5)*h);
  17.     endfor
  18.     r(2,1)=0.5*(r(1,1)+h*tmp);
  19.  
  20.     for j=2:i
  21.       ext = ((r(2,j-1)-r(1,j-1))/(4^(j-1)-1));
  22.       r(2,j) = r(2,j-1) + ext;
  23.     endfor
  24.     for j=1:i
  25.       disp(r(2,j));
  26.     endfor
  27.     h=h/2;
  28.     for j=1:i
  29.       r(1,j)=r(2,j);
  30.     endfor
  31.   endfor
  32.   format long;
  33.   #printf("long",r);
  34. endfunction
  35.  
  36. function s = f(x)
  37.   s = sin(x);
  38. endfunction