could i get help with this question?

1 次查看(过去 30 天)
can someone help me with this?
  4 个评论
poppy
poppy 2022-11-25
编辑:Walter Roberson 2022-11-26
FIRST Function for Gauss elimination:
function [x, A, b] = GE(A, b)
%-------------------------------------------------------------------------%
%GaussE: Function which performs Gauss Elimination, pivoting if required.
%INPUTS
% A = Input A matrix
% b = Input b vector
%OUTPUTS
% A = The forward eliminated coefficient matrix
% b = The forward eliminated b vector
% x = The final solution vector x
%-------------------------------------------------------------------------%
n = length(A);
%-------------------------------------------------------------------------%
% FORWARD ELIMINATION START:
%-------------------------------------------------------------------------%
for i = 1:(n-1) % Pivot row index (stop at 2nd last row)
%-------------------------------------------------------------------------%
% < INSERT PARTIAL PIVOTING CODE HERE >
%-------------------------------------------------------------------------%
for k = (i+1):n % Skips the pivot element and indexes what is BELOW it in the SAME COLUMN as the pivot element.
factor = ( A(k,i) / A(i,i) ); % Calculates the row factor for current row
for j = 1:n % Column index
A(k,j) = A(k,j) - factor*A(i,j); % Row subtraction, element-by-element
end
b(k) = b(k) - factor*b(i); % Updates b
end
end
%-------------------------------------------------------------------------%
% FORWARD ELIMINATION END
%-------------------------------------------------------------------------%
%-------------------------------------------------------------------------%
% BACKWARDS SUBSTITUTION START:
%-------------------------------------------------------------------------%
x = zeros(n,1);
x(n) = b(n) / A(n,n); % Compute x(n) immediately
for i = (n-1):(-1):(1) % Row index, starting at the 2nd last row and working backwards
summ = b(i);
for j = (i+1):n % Before solving for x(i), need to subtract all terms to the RIGHT of the current pivot element (if any).
summ = summ - A(i,j)*x(j);
end
x(i) = summ/A(i,i); % Divide final 'sum' by the pivot element
x
end
%-------------------------------------------------------------------------%
% BACKWARDS SUBSTITUTION END
%-------------------------------------------------------------------------%
end
Second function:
function [x]=BlockDiagonalSolver(A,b)
n=length(A);
d=sqrt(length(A));
x=zeros(n,1);
for i= 1:d
start_row=((i-1)*d+1);
end_row=(i*d);
h=start_row:end_row;
A_solve=A(h,h);
b_solve=b(h);
[A_1,b_1,x(h)]= GaussE(A_solve,b_solve);
end
QUESTION: How would I go about generating a d block diagonal system for d = [ 5,10,15....50] and solve each system by integrating the two functions above.
Walter Roberson
Walter Roberson 2022-11-26
编辑:Walter Roberson 2022-11-26
See diag and blkdiag
Nothing in the assignment calls for integration.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by