octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #58558] Jacobi method .m file that runs well i


From: Baracu T.
Subject: [Octave-bug-tracker] [bug #58558] Jacobi method .m file that runs well in Matlab but not in Octave
Date: Sun, 14 Jun 2020 18:54:46 -0400 (EDT)
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0

Follow-up Comment #2, bug #58558 (project octave):

OK, i made some adaption to the problem, I updated the code in order to run
directly without interogation. The code from below runs without problem in
Matlab. When I run in Octave, there appear two problems:
1) does not process the instruction "disp" from inside the code in the
following format (instead, in Matlab it works):

disp("Diag. elem."+"  "+"Line sum"+"  "+"DiffDiag")

2) does not process the instruction "table". It is not implemented? Im
surprised.

Due to those 2 problems, the code does not run successfully in Octave.

The updated code:

% Jacobi method (Met. Jacobi) in Matlab
clc; close all; clear all

%n=input ('Enter no. of eqs. n=');
%Aug=input('Enter elem. of the sys. row-wise (Ex. [1.1  3; 1 0;…;...]:');
%x=input('Enter init. values of the variables (default [0; 0; 0; 0] ):');
%errmax=input('Enter max. err. (default 0.001 ):');
%itmax=input('Enter max no. of it. (default 100 ):');

n=4;
Aug=[6 2 1 2 5   ;  2 5 -1 1 -16.5  ;   1 -1 5 -1 23.5  ;   2 1 -1 7 -1.5];
x=[0; 0; 0; 0];
errmax=0.001; itmax=100

fprintf('\n------The augmented matrix is:-------')
Aug        %print the new pivotized matrix

sum=zeros(n,1);  err=1;  it = 0;  % initialize a set of variables

for i=1:n    % ------Pivotisation to make eqs. diag. dominant---
     for k=i+1:n
          if abs(Aug(i,i))<abs(Aug(k,i))
            for j=1:n+1
                temp=Aug(i,j);
                Aug(i,j)=Aug(k,j);
                Aug(k,j)=temp;
           end
         end
     end
end

fprintf('\n----The aug. matrix after Pivotisation is:--')
Aug        %print the new pivotized matrix
disp('Comparis. diag. elem. vs line sum')
disp("Diag. elem."+"  "+"Line sum"+"  "+"DiffDiag")
disp('---------------------------------------------------------')   

for i=1:n
        s=0;
    for j=1:n
        if j~=i
        s=s+abs(Aug(i,j));
         end
        sum(i)=s;
        DiffDiag(i)= abs(Aug(i,i))-sum(i);
        AbsDiag(i)=abs(Aug(i,i));
    end
end
table(AbsDiag.',sum,DiffDiag.','VariableNames',{
'AbsDiag','Linesum','DiffDiag'})
            if(DiffDiag>0)
                 disp('Matrix diagonally dominant. Sys. Converges')
            else
                 disp('Matrix not diag. domin. Sys. may not converge.')
           end
fprintf('\n-----Perform loop iterations (new vs. old values)-----\n')
fprintf('\n-------Results of  Jacobi method------\n\n')

while err>errmax & it<=itmax
     it=it+1;
     xold=x;
       for i=1:n
            rhs=Aug(i,n+1)/Aug(i,i);  % rhs is the right hand side member
            for j=1:n
                if j~=i
                  rhs = rhs-1/Aug(i,i)*Aug(i,j)*xold(j);
                end
            end
            x(i)= rhs;
            err=abs(x(i)- xold(i));
            tx(it,i)=x(i);  % alternatively [it x(1)  x(2) x(3) x(4)]
            ti(it)=it;
            te(it,i)=err;
       end
end
if err<=errmax & it<=itmax
  disp("Sys. converges after "+it+" itrs.")
  else 
       disp("Sys. not converges after "+it+" it.")
       disp("Partial values from allowed max. no. of it:");
end

table(ti(:), tx(:,1),tx(:,2),tx(:,3),tx(:,4),te(:,1),te(:,2),te(:,3),te(:,4),
'VariableNames', {'itr','x1','x2','x3', 'x4','err1','err2','err3', 'err4'})

fprintf('\nSys solution:\n %f\n %f\n %f\n %f\n\n after %d itrs.\n', x, it);
fprintf('\nSol.:\n x1=%9.5f\n x2=%9.5f\n x3=%9.5f\n x4=%9.5f\n',
x(1,:),x(2,:),x(3,:),x(4,:))




############################################
############################################
############################################
THE RESULT THAT SHOUD APPEAR:



itmax =

   100


------The augmented matrix is:-------
Aug =

    6.0000    2.0000    1.0000    2.0000    5.0000
    2.0000    5.0000   -1.0000    1.0000  -16.5000
    1.0000   -1.0000    5.0000   -1.0000   23.5000
    2.0000    1.0000   -1.0000    7.0000   -1.5000


----The aug. matrix after Pivotisation is:--
Aug =

    6.0000    2.0000    1.0000    2.0000    5.0000
    2.0000    5.0000   -1.0000    1.0000  -16.5000
    1.0000   -1.0000    5.0000   -1.0000   23.5000
    2.0000    1.0000   -1.0000    7.0000   -1.5000

Comparis. diag. elem. vs line sum
Diag. elem.  Line sum  DiffDiag
---------------------------------------------------------

ans =

  4×3 table

    AbsDiag    Linesum    DiffDiag
    _______    _______    ________

    6          5          1       
    5          4          1       
    5          3          2       
    7          4          3       

Matrix diagonally dominant. Sys. Converges

-----Perform loop iterations (new vs. old values)-----

-------Results of  Jacobi method------

Sys. converges after 14 itrs.

ans =

  14×9 table

    itr      x1         x2         x3         x4         err1         err2    
     err3          err4   
    ___    _______    _______    ______    ________    _________    _________ 
  __________    __________

     1     0.83333       -3.3       4.7    -0.21429      0.83333          3.3 
         4.7       0.21429
     2      1.2214    -2.6505    3.8305     0.69048       0.3881      0.64952 
     0.86952       0.90476
     3     0.84825    -3.1606    4.0637     0.36259      0.37317       0.5101 
     0.23324       0.32789
     4      1.0887    -2.8991    3.9708      0.5754      0.24046       0.2615 
    0.092962       0.21281
     5      0.9461    -3.0564    4.0175     0.45606      0.14261      0.15734 
     0.04677       0.11934
     6      1.0305    -2.9661    3.9907     0.52596      0.08443     0.090265 
    0.026813      0.069904
     7     0.98161    -3.0193    4.0059     0.48511     0.048921     0.053115 
    0.015148      0.040848
     8      1.0104    -2.9885    3.9968     0.50884     0.028797     0.030768 
   0.0090086      0.023729
     9     0.99374    -3.0066     4.002     0.49493     0.016664     0.018066 
     0.00514       0.01391
    10      1.0035    -2.9961    3.9989     0.50301    0.0098021     0.010476 
   0.0030624     0.0080764
    11     0.99787    -3.0022    4.0007     0.49827    0.0056736    0.0061486 
     0.00175     0.0047346
    12      1.0012    -2.9987    3.9996     0.50102    0.0033361    0.0035664 
   0.0010419     0.0027494
    13     0.99928    -3.0008    4.0002     0.49941    0.0019316    0.0020927 
  0.00059594     0.0016115
    14      1.0004    -2.9995    3.9999     0.50035    0.0011354    0.0012141 
  0.00035451    0.00093597


Sys solution:
 1.000411
 -2.999547
 3.999875
 0.500348

 after 14 itrs.

Sol.:
 x1=  1.00041
 x2= -2.99955
 x3=  3.99988
 x4=  0.50035


    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?58558>

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]