bugGNU Octave - Bugs: bug #58558, Jacobi method .m file that runs...

 
 

bug #58558: Jacobi method .m file that runs well in Matlab but not in Octave

Submitter:  None
Submitted:  Sat 13 Jun 2020 05:30:00 PM UTC
   
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Duplicate Assigned to:  None
Originator Name:  Tudor B. Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * 5.2.0
Operating System:  * Microsoft Windows Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Mon 15 Jun 2020 05:01:38 AM UTC, comment #3: 

Thanks for clearing up the two specific issues you found. Fortunately, these are both known issues and have already been reported, so no more information needed here. I'm closing this bug report as a duplicate.

For the 'table' class, see bug #44571. Or try using the 'tablicious' package which implements a lot of 'table' functionality already, see https://apjanke.github.io/octave-tablicious/.

For the 'string' class, see bug #50855. Having the 'string' object type will allow the '+' operator to concatenate strings together like you expect.

Mike Miller <mtmiller>
Group Member
Sun 14 Jun 2020 10:54:46 PM UTC, comment #2: 

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

Baracu T. <tubar>
Sun 14 Jun 2020 10:01:53 PM UTC, comment #1: 

Thank you for the report, but please provide more information if you are trying to help Octave development. What is the specific error or difference that you see when you run this script?

I have tried running it a few times by guessing what the inputs should look like, and I just end up with what look like simple programming errors on empty arrays or mismatched array sizes.

Mike Miller <mtmiller>
Group Member
Sat 13 Jun 2020 05:30:00 PM UTC, original submission:  

Jacobi method .m file that runs well in Matlab but not in Octave

The following program runs OK in MAtlab and gives results, instead does not work in OCTAVE.

Example: Solve the system A*x=b:
A=[6 2 1 2  ;   2 5 -1 1  ;   1 -1 5 -1  ;   2 1 -1 7]
b=[5 ; -16.5 ; 23.5 ; -1.5]
x0= [0;0;0;0]

Aug(A,b) considers A and b connected connected.


% 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 ):');
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
 
disp(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,:))

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by tubar (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2020-06-15 mtmiller StatusNeed Info Duplicate
        Open/ClosedOpen Closed
    2020-06-14 mtmiller StatusNone Need Info

    Back to the top

    Powered by Savane 3.13-caa5.
    Corresponding source code