MATLAB question: Please Modify the error of the code , to let the output like the sample: error line(x_old = x_new = zer
Posted: Fri May 20, 2022 10:04 am
MATLAB question:
Please Modify the error of
the code , to let the output like the sample: error line(x_old =
x_new = zero (m, 1); x_old = x_new = zero (n, 1); )
% Using the complete matrix form A
erase everything
close everything
clc
format
A = [1. .2 0 0; .3 .41 .2 0; 0 .3 .51 .2; 0 0 .3 .41];
b = [1 2 3 4] ';
fprintf ('\ nThe real solution is:')
x = A \ b
[m, n] = size (A);
x_old = x_new = zero (m, 1);
total = 1E-8; % Tolerance
error = inf;
iter = 1;
plural_iter = 1000; % Maximum number of repetitions to be
performed
while (error> tol && iter <= max_iter)
for = 1: m
x_new (i) = x_old (i) - A (i,:) * x_old + b (i);
the end
error = standard (x_old-x_new);
x_old = x_new;
the end
x1 = x_new;
fprintf ('\ nThe solution that uses the Richardsons method with the
full matrix is \ n')
x1
% Using full matrix form A = 3 * n
erase everything
close everything
A = [1 .41 .51 .41; .2 .2 .2 NaN; .3 .3 NaN];
b = [1 2 3 4] ';
[m, n] = size (A);
x_old = x_new = zero (n, 1);
total = 1E-8;
error = inf;
iter = 1;
max_iter = 1000;
while (error> tol && iter <= max_iter)
x_okusha (1) = x_old (1) - (A (1,1) * x_old (1) + A (2,1) * x_old
(2)) + b (1); % Row 1
because = 2: n-1
x_old (i) = x_old (i) - (A (3, 1) * x_old (1) + A (1, i) * x_old
(i) + A (2, i) * x_old (i + 1) )) + b (i);
the end
x_new (n) = x_old (n) - (A (3, n-1) * x_old (n-1) + A (1, n) *
x_old (n)) + b (n);% Last line
error = standard (x_old-x_new);
x_old = x_new;
the end
x2 = x_new;
fprintf ('\ nThe solution with A = 3 * n says \ n')
x2
Please Modify the error of
the code , to let the output like the sample: error line(x_old =
x_new = zero (m, 1); x_old = x_new = zero (n, 1); )
% Using the complete matrix form A
erase everything
close everything
clc
format
A = [1. .2 0 0; .3 .41 .2 0; 0 .3 .51 .2; 0 0 .3 .41];
b = [1 2 3 4] ';
fprintf ('\ nThe real solution is:')
x = A \ b
[m, n] = size (A);
x_old = x_new = zero (m, 1);
total = 1E-8; % Tolerance
error = inf;
iter = 1;
plural_iter = 1000; % Maximum number of repetitions to be
performed
while (error> tol && iter <= max_iter)
for = 1: m
x_new (i) = x_old (i) - A (i,:) * x_old + b (i);
the end
error = standard (x_old-x_new);
x_old = x_new;
the end
x1 = x_new;
fprintf ('\ nThe solution that uses the Richardsons method with the
full matrix is \ n')
x1
% Using full matrix form A = 3 * n
erase everything
close everything
A = [1 .41 .51 .41; .2 .2 .2 NaN; .3 .3 NaN];
b = [1 2 3 4] ';
[m, n] = size (A);
x_old = x_new = zero (n, 1);
total = 1E-8;
error = inf;
iter = 1;
max_iter = 1000;
while (error> tol && iter <= max_iter)
x_okusha (1) = x_old (1) - (A (1,1) * x_old (1) + A (2,1) * x_old
(2)) + b (1); % Row 1
because = 2: n-1
x_old (i) = x_old (i) - (A (3, 1) * x_old (1) + A (1, i) * x_old
(i) + A (2, i) * x_old (i + 1) )) + b (i);
the end
x_new (n) = x_old (n) - (A (3, n-1) * x_old (n-1) + A (1, n) *
x_old (n)) + b (n);% Last line
error = standard (x_old-x_new);
x_old = x_new;
the end
x2 = x_new;
fprintf ('\ nThe solution with A = 3 * n says \ n')
x2