Implement a code to solve Ux = b for x

조회 수: 4 (최근 30일)
Emily
Emily 2014년 2월 16일
I am supposed to write a code that will solve Ux = b where U,x and b are matrices. Using the built in MATLAB commands for this (i.e. x = U\b) is not acceptable. This is the code I have that I wrote from a general outline from my TA:
function [ x ] = utrisolve( A,b )
%UNTITLED6 Summary of this function goes here
% Detailed explanation goes here
A;
b;
[n1,n2] = size(A);
if n1 ~= n2
print('matrix must be square');
return;
end
AA = A;
bb = b;
while n2 ~= 0
x(n2) = bb(n2)\AA(n1,n2);
v = AA(1:n1-1,n2);
AA = AA(1:n1-1,1:n2-1);
bb = bb(1:n2-1)-v.*x(n2);
n2 = n2-1;
n1 = n1-1;
end
end
and when I enter the following into the command window I get the following error and do not know how to fix it:
>> A = [-1 1 2 0; 0 2 -1 2; 0 0 -3 -1; 0 0 0 4];
b = [1; 1; 1; 1];
[x] = utrisolve(A,b)
Error using -
Matrix dimensions must agree.
Error in utrisolve (line 17)
bb = bb(1:n2-1)-v.*x(n2);
Help Please!
if true
% code
end

답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by