Implement a code to solve Ux = b for x

4 vues (au cours des 30 derniers jours)
Emily
Emily le 16 Fév 2014
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

Réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by