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: anonymous
Subject: [Octave-bug-tracker] [bug #58558] Jacobi method .m file that runs well in Matlab but not in Octave
Date: Sat, 13 Jun 2020 13:30:02 -0400 (EDT)
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0

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

                 Summary: Jacobi method .m file that runs well in Matlab but
not in Octave
                 Project: GNU Octave
            Submitted by: None
            Submitted on: Sat 13 Jun 2020 05:30:00 PM UTC
                Category: Interpreter
                Severity: 3 - Normal
                Priority: 5 - Normal
              Item Group: Matlab Compatibility
                  Status: None
             Assigned to: None
         Originator Name: Tudor B.
        Originator Email: tbaracu@yahoo.co.uk
             Open/Closed: Open
                 Release: 5.2.0
         Discussion Lock: Any
        Operating System: Microsoft Windows

    _______________________________________________________

Details:

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,:))





    _______________________________________________________

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]