Solved

# Matlab loop help

Posted on 2008-10-22
Medium Priority
1,330 Views
Hey

i dont really understand whats happening in this example code i have. i dont understand matlabs implicit algebra. If you look at the attatched code can you please tell me how to write the bottom part of it with just "for loops" instead of the built in matlab algebra so i can better understand whats happening.

how do i write the:

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

line of the loop:

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

as a series of nested loops. when i it like this i think i'll be able to understand whats happening more easily and then hopefully i'll be able to play with the function!

thanks
``````function x = UpperTriangularSolve(A,b,n)

% function x = UpperTriangularSolve(A,b,n)
%
% Solve the system  A x = b  where A is assumed to be upper triangular,
% i.e. A(i,j) = 0 for j < i, and the diagonal is assumed to be nonzero,
% i.e. A(i,i) \= 0.
%
% ARGUMENTS:  A   upper triangular n x n matrix
%             b   right hand side column n-vector
%             n   integer matrix dimension
%
% RETURNS:    x   column n-vector solution

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

for i = n-1:-1:1
x(i) = ( b(i) - A(i,i+1:n)*x(i+1:n) ) / A(i,i);
end
``````
0
Question by:Ashwee
1 Comment

Accepted Solution

Ashwee earned 0 total points
ID: 22776012
erm i kind of worked it out :P

``````for i = n-1:-1:1
f = 0;
x(i) = b(i);

for m = i+1:n
m;
f = f + A(i,m)*x(m);
end

x(i) = x(i) - f;
x(i) = x(i) / A(i,i);
end
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month7 days, 23 hours left to enroll