using sor in matlab without matrix in matlab

To use the Successive Over-Relaxation (SOR) method in MATLAB without using matrices, you can implement a loop-based approach. Here's an example code snippet that demonstrates how to do it:

main.m
% Initialize variables
tolerance = 1e-6;
maxIterations = 100;

% Define the SOR parameters
omega = 1.5; % relaxation factor
n = 10; % number of unknowns
b = zeros(n, 1); % right-hand side vector
x0 = zeros(n, 1); % initial guess
x = x0; % current solution estimate

% SOR loop
for k = 1:maxIterations
    xOld = x; % save the previous solution
    
    % update current solution
    for i = 1:n
        sigma = 0;
        for j = 1:n
            if i ~= j
                sigma = sigma + A(i, j) * x(j);
            end
        end
        x(i) = (1 - omega) * xOld(i) + (omega / A(i, i)) * (b(i) - sigma);
    end
    
    % compute the error
    error = norm(x - xOld);
    
    % check for convergence
    if error < tolerance
        break;
    end
end

% Print the final solution
disp('Final solution:');
disp(x);
819 chars
39 lines

Note that in this code, I've used A to represent the coefficient matrix of the linear system, which you need to define according to your problem.

The algorithm uses a nested loop to iterate over all unknowns and update their values based on the SOR formula. The loop continues until a convergence criterion is met (in this case, the error between the current and previous solutions is below a certain tolerance).

Make sure to modify the code to fit your specific problem and adjust the SOR parameters (omega) and convergence criteria (tolerance, maxIterations) accordingly.

Keep in mind that using matrices can provide a more efficient and concise implementation, but if you specifically need to avoid using matrices, this loop-based approach should work.

related categories

gistlibby LogSnag