How to solve Lc=y without backslash operator?

3 views (last 30 days)
Hi! My assignment is to solve y in this L*c=y operation where L is a undertriangular matrice and c is a vector with values. My equation system is: L11y1 = c1
L21y1 + L22y2 = c2
L31y1 + L32y2 + L33y3 = c3
This leads to: y1 = c1/L11 y2= (c2-L21*y1)/L22 y3= (c3-L32y1 - L31y1)/L33
And i'm not allowed to use backslash operator to solve for y and save the results in a vector. I have to write a code that no matter what size n x n undertriangular matrice i use, the code will solve for y and save the results in a vector. I tried making a for loop but i'm stuck.
for k=1:3
a=(c(k)/L(k,1))
b=(c(k+1)-L(k+1,k))/L(k+1,k+1)
end
I know the code isnt working because the last iteration exceeds dimensions of the L matrice, but i dont know what to do. I can code this for one specific matrice, i'm just not sure how to code it so it'll work for any n x n size matrice.

Answers (1)

James Tursa
James Tursa on 15 Jan 2022
Edited: James Tursa on 15 Jan 2022
First, I am assuming there is a typo and the system you are solving is Ly = c, not Lc=y.
Second, you made a good start by writing down the first few solutions as follows (I have rearranged things slightly and added one more line):
y1 = c1/L11
y2= (c2 - L21*y1)/L22
y3= (c3 - L31*y1 - L32*y2)/L33
y4= (c4 - L41*y1 - L42*y2 - L43*y3)/L44
:
Next thing for you to do is look for the patterns in the above solution and recognize that you will need two nested loops. The outer loop will loop over y1, y2, y3, etc. The inner loop will loop over the Lij * yj terms to calculate that sum. It will look something like this:
n = size(L,1);
for i=1:n
% some code here to initialize the sum you are about to calculate in the next loop
for j=_____ % you will need to pick appropriate limits for j (based on value of i)
_____; % some code here to sum up the appropriate Lij * yj terms
end
y(i) = _____; % a formula involving the appropriate c and L terms and the sum you just calculated
end
Alternatively, you could replace that inner loop sum with a dot product of two vectors formed from appropriate portions of L and y. Not sure what is allowed in your assignment.
Give this a shot and come back with any questions you have.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!