Question

I need the Matlab code and the Solution to the problem.

I need the Matlab code and the Solution to the problem.
media%2F6ad%2F6add8602-62d9-4a11-8dcf-01
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: I have modified the GEpivot.m and saved it in the file GaussWithOutPivot.m

Answer: Code %File Name: GEpivot %Define function function [x, lu,piv] - GEpivot (A, b) %Find value [m, nsize (A) %Check cond

%End end %Create variable piv- (1:n); %Loop for k- 1:n-1 %Find value [colmax index] max (abs (A (k: n, k) ) ) ; = - %Increas

%swap b %Copy value tempbb (k) %Copy value b (k) = b (index); &Copy value b (index) = tempb; %swap pivot %Copy value temp piv

for i k+1 : n = %Find value %End end %Find value b(k+1 : n) b(kt1:n) -A(k+1:n, k) *b ( k ) ; = %End end &Create x = zeros (n,

end %set value lu = A; %End end %File Name: GausswithoutPivot.n %Define function function x = GausswithOutpivot (A, b) %Find

%Find value %Find value %End end %Find value tx (n, :) %Loop for i = n-1:-1:1 %Find value %End end set value %End

%File Name: MysampleRun.n %Sample Run nAry[2 5 10 20 : SPrint &Print message %Print stars fprintf(* %Loop for kk= 1: length (

%Loop for j = 1:n %Find value A(i,j) max (i,j); %End end %End end %Find value xDinv (A) *bi %Call function (xP, lu, piv] = GE

Print value fprintf(ti\t%f\t%f\n, n, err!, err2); %End end Print Sample Output XD- xP 0.000000 0.000000 0.000000 0.000000 0

Copyable code:

%File Name: GEpivot.m

%Define function

function [x,lu,piv] = GEpivot(A,b)

     %Find value

     [m,n] = size(A);

     %Check condition

     if m ~= n

     %Print

     error('The matrix is not square.')

     %End

     end

     %Find value

     m = length(b);

     %Check condition

     if m ~= n

     %Print

     error('The matrix and the vector do not match in size.')

     %End

     end

     %Create variable

     piv = (1:n)';

     %Loop

     for k = 1:n-1

     %Find value  

     [col_max index] = max(abs(A(k:n,k)));

     %Increase value

     index = index + k-1;

     %Check condition

     if index ~= k

     %Swap rows k and index

     tempA = A(k,k:n);

     %Copy value

     A(k,k:n) = A(index,k:n);

     %Copy value

     A(index,k:n) = tempA;

     %Swap b

     %Copy value

     tempb = b(k);

     %Copy value

     b(k) = b(index);

     %Copy value

     b(index) = tempb;

     %Swap pivot

     %Copy value

     temp = piv(k);

     %Copy value

     piv(k) = piv(index);

     %Copy value

     piv(index) = temp;

     %End

     end

     %Find value

     A(k+1:n,k) = A(k+1:n,k)/A(k,k);

     %Loop

     for i = k+1:n

     %Find value

     A(i,k+1:n) = A(i,k+1:n) - A(i,k)*A(k,k+1:n);

     %End

     end

     %Find value

     b(k+1:n) = b(k+1:n) - A(k+1:n,k)*b(k);

     %End

     end

     %Create

     x = zeros(n,1);

     %Set value

     x(n) = b(n)/A(n,n);

     %Loop

     for i = n-1:-1:1

     %Find value

     x(i)=(b(i)-A(i,i+1:n)*x(i+1:n))/A(i,i);

     %End

     end

     %Set value

     lu = A;

%End

end

%File Name: GaussWithOutPivot.m

%Define function

function x = GaussWithOutPivot(A, b)

     %Find size

     [n, n] = size(A);

     %Find size   

     [n, k] = size(b);

     %Create variable   

     tx = zeros(n,k);

     %Loop   

     for i = 1:n-1

     %Find value

     tp = -A(i+1:n,i)/A(i,i);

     %Find value

     A(i+1:n,:) = A(i+1:n,:) + tp*A(i,:);

     %Find value

     b(i+1:n,:) = b(i+1:n,:) + tp*b(i,:);

     %End

     end

     %Find value

     tx(n,:) = b(n,:)/A(n,n);

     %Loop

     for i = n-1:-1:1

     %Find value

     tx(i,:) = (b(i,:) - A(i,i+1:n)*tx(i+1:n,:))/A(i,i);

     %End

     end

     %Set value

     x = tx;

%End

end

%File Name: MySampleRun.m

%Sample Run

nAry = [2 5 10 20 ];

%Print

fprintf("\n************************************\n");

%Print message

fprintf("n\t||xD - xP||\t||xD - xNP||\n");

%Print stars

fprintf("************************************\n");

%Loop

for kk= 1: length(nAry)

     %Get current n value

     n = nAry (kk);

     %Define A

     A = zeros(n,n);

     %Define b

     b = ones(n, 1);

     %Loop

     for i=1:n

          %Loop

          for j = 1:n

          %Find value

          A(i,j) = max(i,j);

          %End

          end

     %End

     end

     %Find value

     xD = inv(A)*b;

     %Call function

     [xP, lu, piv] = GEpivot(A, b);

     %Call function

     [xNP] = GaussWithOutPivot(A,b);

     %Find norm between xD and xP    

     err1 = norm(xD - xP, inf);

     %Find norm between xD and xNP

     err2 = norm(xD - xNP, inf);

     %Print value

     fprintf("%i\t%f\t%f\n", n, err1, err2);

%End

end

%Print

fprintf("************************************\n");

Add a comment
Know the answer?
Add Answer to:
I need the Matlab code and the Solution to the problem.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT