%%%%%%%% JACOBI CALCULATOR %%%%%%%%%%%

% Define the matrix A and vector b
A = [2 -1 0; -1 3 -1; 0 -1 2];
b = [1; 8; -5];

% Initial guess
x0 = [0; 0; 0];

% Maximum allowed component-wise error percentage
max_error_percentage = 10;

% Initialize variables
x = x0;
n = length(b);
D = diag(diag(A));
R = A - D;
tolerance = max_error_percentage / 100;

% Display the header for the iteration table
fprintf('Iteration\t x1\t\t x2\t\t x3\t\t Max Error (%%)\n');
fprintf('-----------------------------------------------------------------------\n');

% Iteration counter
k = 1;

while true
    % Calculate the new approximation
    x_new = D \ (b - R * x);
    
    % Calculate the component-wise error
    error = abs((x_new - x) ./ x_new);
    max_error = max(error) * 100;  % Convert to percentage
    
    % Print the current iteration, the values of x, and the max error percentage
    fprintf('%d\t\t %.6f\t %.6f\t %.6f\t %.2f%%\n', k, x_new(1), x_new(2), x_new(3), max_error);
    
    % Check if the maximum component-wise error is below the tolerance
    if max_error < max_error_percentage
        break;
    end
    
    % Update the current approximation
    x = x_new;
    k = k + 1;
end

% Summary table
fprintf('\nSummary Table:\n');
fprintf('Iteration\t x1\t\t x2\t\t x3\t\t Max Error (%%)\n');
fprintf('-----------------------------------------------------------------------\n');
fprintf('%d\t\t %.6f\t %.6f\t %.6f\t %.2f%%\n', k, x_new(1), x_new(2), x_new(3), max_error);
 

Octave online compiler

Write, Run & Share Octave code online using OneCompiler’s Octave online compiler for free. It’s a simple and powerful platform to practice numerical computations and matrix operations using GNU Octave right from your browser.

About Octave

GNU Octave is an open-source high-level programming language primarily intended for numerical computations. It is mostly compatible with MATLAB, and it's commonly used for linear algebra, numerical analysis, signal processing, and other scientific computing tasks.

Sample Code

The following is a simple Octave program that prints a greeting:

printf("Hello, OneCompiler!\n");

Taking inputs (stdin)

OneCompiler’s Octave editor supports stdin through the I/O tab. Here's an example of reading input from the user:

name = input("Enter your name: ", "s");
printf("Hello, %s!\n", name);

Syntax Basics

Variables

a = 10;
b = 3.14;
name = "Octave";

Vectors and Matrices

v = [1, 2, 3];
M = [1, 2; 3, 4];

Arithmetic

OperationSyntax
Add+
Subtract-
Multiply*
Divide/
Element-wise.*, ./

Conditionals

x = 10;
if x > 5
    disp("x is greater than 5");
else
    disp("x is 5 or less");
end

Loops

For loop

for i = 1:5
    disp(i);
end

While loop

i = 1;
while i <= 5
    disp(i);
    i = i + 1;
end

Functions

function y = square(x)
    y = x ^ 2;
end

result = square(4);
printf("Square: %d\n", result);

This guide provides a quick reference to Octave programming syntax and features. Start writing Octave code using OneCompiler’s Octave online compiler today!