Question

Engineering Computations and Modelling > Week 6 Homework > Backward Substitution 2 solutions submitted (max: Unlimited) | Vie

and returns as output: • X, an n x 1 solution vector x Do not use backslash operator or other MATLAB solver functions to compHERE IS THE CODE I FIXED BUT STILL DOESN'T WORK

NOTE: THE VARIABLE x = zeros(size(b)) CAN'T BE CHANGED CAUSE HAS BEEN SET BY ASSESSOR

Function C Reset LOMATLAB Documentation 1 function x = backsub(u,b) 2 3 % Initialize the output vector. 4 n = length(b); 5 x

HI EXPERTS I NEED HELP TO SOLVE THIS HOMEWORK PROBLEM FOR MATLAB CODE

A COUPLE OF TIMES I TRIED LAST TIME TO ASK BUT ALL OF THE ANSWERS WERE WRONG

PLEASE KINDLY HELP ME FIND THE RIGHT SOLUTION, ANY HELP WILL BE VERY APPRECIATE

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ok, your algorithm is a bit off the mark. You were thinking along the right paths, however. For these kinds of problems, the best way to learn is to do the steps by hand and then thinking about how you will implement them via code.

I have provided both the text and the screenshot for your better understanding. The screenshot also has some comments for your help. Please note that this is a script file which is to be saved as backsub.m in your MATLAB root directory.
The script backsub.m gives the value of the variable matrix as output. The input must be a square upper triangular non-singular matrix with none of the diagonal elements as 0.

backsub.m

+ backsub.m function x = backsub (U,b) n = length(b); Scalculating size of solution vector x = ones (size (b)); $initializing

function x = backsub(U,b)

n = length(b); %calculating size of solution vector
x = ones(size(b)); %initializing solution vector

for i=n:-1:1
x(i) = b(i)/U(i,i); %solving the last row of the matrix
b(1:i-1) = b(1:i-1) - U(1:i-1,i)*x(i); %updating the constant values so that the previous row can now be treated as the last row of the matrix
end
fprintf("result of backward substitution\n");
disp(x);
end

OUTPUT

I have attached screenshots for 3 random test cases, one of them in the question and the other 2 from random sources.

Example matrix in question

>> A = [1,2;0,2] 2 1 2 0 >> B = [2;1] B = 1 >> x = backsub (A,B); result of backward substitution 1.0000 0.5000 fx >> |

3x3 matrix>> A = [1,-2,1;0,1,6;0,0,1] 1 6 -2 1 0 >> b = [ 4;-1;2] b = 4 2 >> x = backsub (A,b) result of backward substitution -24 -13

4x4 matrix>> A = [ 1,2,1,-1 ; 0,-4,1,7;0,0,-2,1;0,0,0,-1] 1 1 -1 7 1 1 2 0 -4 0 -2 0 0 0 >> B = [5;1;1;3] B = 5 -1 1 1 3 >> x = backsub


*A humble request* - If you have any doubt, please use the comment section to communicate. This will clarify your doubt, and also help me to get better at answering your next questions. At the same time, If my answer helped you, please consider leaving an upvote. I hope you understand my viewpoint. Thank you :)

Add a comment
Know the answer?
Add Answer to:
HERE IS THE CODE I FIXED BUT STILL DOESN'T WORK NOTE: THE VARIABLE x = zeros(size(b))...
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
  • Complete the definition of the backsub function below by implementing the backward substitution algorithm. This function...

    Complete the definition of the backsub function below by implementing the backward substitution algorithm. This function takes as input: U, an n x n upper triangular matrix b, an n x 1 vector for the right-hand-side of the matrix equation and returns as output: x, an n x 1 solution vector x function x = backsub(U,b) % Initialize the output vector. This will speed up computation by pre-allocating the memory. n = length(b); x = zeros(size(b)); % Do the backward...

  • Function LUfac_solver.m is provided here: function [x] = LUfac_solver(LU,b,piv) % % function [x] = LUfac_solver(lu,b) %...

    Function LUfac_solver.m is provided here: function [x] = LUfac_solver(LU,b,piv) % % function [x] = LUfac_solver(lu,b) % % This program employs the LU factorization to solve the linear system Ax=b.   % % Input % LU: lu matrix from GEpivot_new function % b: right side column vector (ordered corresponding to original vector % sent to GEpivot_new) % piv: vector indicating the pivoting (row interchanges that took place % during GE % % Output % x: solution vector % % Written by Steve...

  • (a) Suppose we want to solve the linear vector-matrix equation Ax b for the vector x. Show that the Gauss elimination algorithm may be written bAbm,B where m 1, This process produces a matrix equa...

    (a) Suppose we want to solve the linear vector-matrix equation Ax b for the vector x. Show that the Gauss elimination algorithm may be written bAbm,B where m 1, This process produces a matrix equation of the form Ux = g , in which matrix U is an upper-triangular matrix. Show that the solution vector x may be obtained by a back-substitution algorithm, in the form Jekel (b) Iterative methods for solving Ax-b work by splitting matrix A into two...

  • In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be ...

    In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be reduced to an echelon form by using only row replacement and row interchanging operations. Row interchanging is almost always necessary for a computer realization because it reduces the round off errors in calculations - this strategy in computer calculation is called partial pivoting, which refers to selecting for a pivot the largest by absolute value entry in a column. The MATLAB...

  • Implement this pseudo code with Java. n is the size of the matrix for an n...

    Implement this pseudo code with Java. n is the size of the matrix for an n x n matrix. k is the pivot. i is the equation being eliminated. j is the term you're working with. Gaussian Elimination with Scaled Partial Pivoting // Forward Elimination function SPPFwdElimination(coeff : array(n,n), const : vector(n), ind : vector(n))   scaling := new vector(n) // vector of scaling factors // Initialize index and scaling vectors   for i = 1 to n   smax := 0   for...

  • I need to create a MATLAB function, bvp_solve.m, to approximate the solution y(x). The function takes...

    I need to create a MATLAB function, bvp_solve.m, to approximate the solution y(x). The function takes the number of grid points n as an input. The outputs are grid vector x and the solution vector y %% This is the function i have so far: function [xi, yi] = bvp_solve(n) % BVP_SOLVE computes the solution y(x) of a two-point boundary value problem % using finite difference method (FDM). % The governing equation is % y''' = -y + (x -...

  • I already solved part A and I just need help with part B 1. Matrix Multiplication...

    I already solved part A and I just need help with part B 1. Matrix Multiplication The product of two n xn matrices X and Y is a third n x n matrix 2 = XY, with entries 2 - 21; = xixYk x k=1 There are n’ entries to compute, each one at a cost of O(n). The formula implies an algorithm with O(nº) running time. For a long time this was widely believed to be the best running...

  • In this exercise, you will work with a QR factorization of an mxn matrix. We will proceed in the ...

    In this exercise, you will work with a QR factorization of an mxn matrix. We will proceed in the way that is chosen by MATLAB, which is different from the textbook presentation. An mxn matrix A can be presented as a product of a unitary (or orthogonal) mxm matrix Q and an upper-triangular m × n matrix R, that is, A = Q * R . Theory: a square mxm matrix Q is called unitary (or orthogona) if -,or equivalently,...

  • I want to change this C code so that instead of prompting the user to enter...

    I want to change this C code so that instead of prompting the user to enter the argument value, or requiring the argument be entered after by pressing the “enter” key, instead I want the code changed so that the program opens a file, (which include the input values) read in the elements and pass in the input file as a command line argument. (the input file includes the size of the matrix and the matrix values) Here's an example...

  • 1. (All students!) For matrices with special properties, it is possible to create special version...

    1. (All students!) For matrices with special properties, it is possible to create special versions of Gauss elimination. Suppose matrix A (nxn) is symmetric (which means that A-A). Suppose also that A is positive definite; this means that the scalar = xTAx is always 20 for every vector x , and J-0 only if x = 0 In this case it can be shown that the usual Gauss elimination process, which effectively creates the factorization A LU, can be simplified...

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