Solving a system of 4 linear equations with Gauss Method

1 view (last 30 days)
I have written a basic code to help solve this problem, but Matlab is giving me an error.
%Gauss Method
clear;clc;format('long','g')
i=1;
x(i)=1;
y(i)=1;
z(i)=1;
t(i)=1;
errorx(i)=9999;
errory(i)=9999;
errorz(i)=9999;
errort(i)=9999;
while errorx(i)>=10^-5 || errory(i)>=10^-5 || errorz(i)>=10^-5 ||errort(i)>=10^-5
x(i+1) = (2 - y(i) + z(i) - 2*t(i))/5;
y(i+1) = (1 + z(i) + x(i) - t(i))/4;
z(i+1) = (3 - x(i) - y(i) - t(i))/5;
t(i+1) = (4 +2*x(i) - y(i) +2*z(i))/8;
errorx(i) = abs((x(i+1)-x(i))/x(i));
errory(i) = abs((y(i+1)-y(i))/y(i));
errorz(i) = abs((z(i+1)-z(i))/z(i));
errort(i) = abs((t(i+1)-t(i))/t(i));
i=i+1;
end
disp([x',y',z',t'])
and the error is
??? Attempted to access errorx(2); index out of bounds because numel(errorx)=1.
Error in ==> gaussmethodas3 at 12 while errorx(i)>=10^-5 errory(i)>=10^-5 errorz(i)>=10^-5 ||errort(i)>=10^-5
  1 Comment
Konstantinos Sofos
Konstantinos Sofos on 21 Mar 2015
Edited: Konstantinos Sofos on 21 Mar 2015
use
errorx(i+1) = abs((x(i+1)-x(i))/x(i));
errory(i+1) = abs((y(i+1)-y(i))/y(i));
errorz(i+1) = abs((z(i+1)-z(i))/z(i));
errort(i+1) = abs((t(i+1)-t(i))/t(i));
debugging can make you to understand your code. see MATLAB Debugging Tutorial

Sign in to comment.

Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!