Implement a code to solve Ux = b for x
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!