0

I am new to Matlab, so there is a lot I do not understand; I have three questions, as followed:

The biggest issue I have revolves around function B= upper_triang(A) where it states that it is 'a function unused'. Reading through the Matlab tooltip, and I am guessing that it has to do with a value that has not been defined? ANd B of course being unset, but I thought I defined B as a function, so why is it unset? I am assuming that B is U, but in the math part that is ahh, so I am not sure what it would be.

To give context to my code I sent the part of the homework code I have a question on.

enter image description here

%%%This is for part a for problem 1.
A=zeros(8,8);%%%For the size column and row size of matrix A.
for j=1:8
for k=1:8
if j==k
A(j,k)=-2;
end
if abs(j-k)==1
A(j,k)=1;
end
end
end
[L,U]=lu(A); %%%Proving L and U. 
L*U; %%%Proving that L*U=A, thus it is correct.

%%%This is for part b for problem 1
function B= upper_triang(A) 
%%%Input A--> Matrix for m*n for the size
%%%Output B--> Is the upper triangular matrix of the same size
[m,n]=size(A);
for k=1:m-1
    for i=k+1:m
        u=A(i,k)/A(k,k);
        
        for j=(k):n
            A(i,j)=A(i,j)-u*A(k,j);
        end
    end
end
B-A; 
end
UF6
  • 19
  • 3
  • Please read [ask]. Each post should be a single question, not three. // When you say "where it states that it is 'a function unused'", what do you mean? Is this an error message you get when you run the code, or is it a warning from the editor? I'm guessing it's the latter, since you don't call that function. – Cris Luengo Oct 19 '20 at 06:16

1 Answers1

0

Function being unused

The problem here is that you've only created a function, but you've never used it. Everything after the function B = upper_triang(A) just tells Matlab what to do in case that function is ever called, but your never do.

I'm guessing that, after your LU decomposition but before the declaration of your function, you should add a line

B = upper_triang(A);

Output unset

There is a second problem which is that, while you declared that B should be the output of your function upper_triang(), it it not "created" anywhere in the function itself. There should be a line somewhere with

B = something;

Now, my linear algebra is quite rusty but I think that what you're trying to do should rather be:

%%%This is for part b for problem 1
function B= upper_triang(A) 
%%%Input A--> Matrix for m*n for the size
%%%Output B--> Is the upper triangular matrix of the same size
[m,n]=size(A);
B = A; % copy the input matrix in B, and work with it from now on
for k=1:m-1
    for i=k+1:m
        u=B(i,k)/B(k,k);
        for j=(k):n
            B(i,j)=B(i,j)-u*B(k,j);
        end
    end
end
B = B - A; % not really sure about this
end
Matteo V
  • 1,163
  • 1
  • 8
  • 17
  • Thank you for your answer, and it has helped me. I have also solved the issues I have been having and a lot of it has to do with running the first script before the second, so I have to many A's instead of just using the solved A. I still have one issue though, I do not understand what this error means and why I keep getting it: "Not enough input arguments. Error in triang (line 5) [m,n]=size(A);" – UF6 Oct 21 '20 at 01:43
  • What I am trying to do for part b is to write a code to perform the Gauss elimination, and transform the matrix into the upper triangular form. In terms, then the det(A) is the same as the diagonal entries are exactly the same. – UF6 Oct 21 '20 at 01:44