Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
What does this syntax mean

Subject: What does this syntax mean

From: Ender

Date: 15 Jan, 2010 21:15:22

Message: 1 of 10

I am trying to use MATLAB to do LU factorization. I originally tried to use the MATLAB function to do the factorization, but the answers that MATLAB gave me utilizing the [L,U] = lu(A) command, but MATLAB does not give me the correct answer. I am not sure why.

So I am currently trying decipher a code that I found on the internet. The internet code gives the correct answer, however I cannot understand some of the syntax. Could someone tell me what "m~=n " means. The code before this line reads:

A=input('Enter the square matrix to be factorized ');
[m,n]=size(A);

--Ender--

Subject: What does this syntax mean

From: Oleg Komarov

Date: 15 Jan, 2010 21:29:05

Message: 2 of 10

"Ender "
> I am trying to use MATLAB to do LU factorization. I originally tried to use the
> MATLAB function to do the factorization, but the answers that MATLAB gave me
> utilizing the [L,U] = lu(A) command, but MATLAB does not give me the correct
> answer. I am not sure why.

What is the expected answer and which was matlab's answer?

>
> So I am currently trying decipher a code that I found on the internet. The internet code gives the correct answer, however I cannot understand some of the syntax. Could someone tell me what "m~=n " means. The code before this line reads:
>
> A=input('Enter the square matrix to be factorized ');
> [m,n]=size(A);
>
> --Ender--

help logical operator

Oleg

Subject: What does this syntax mean

From: us

Date: 15 Jan, 2010 21:36:19

Message: 3 of 10

"Ender " <jr147@msstate.edu> wrote in message <hiqlta$25v$1@fred.mathworks.com>...
> I am trying to use MATLAB to do LU factorization. I originally tried to use the MATLAB function to do the factorization, but the answers that MATLAB gave me utilizing the [L,U] = lu(A) command, but MATLAB does not give me the correct answer. I am not sure why.
>
> So I am currently trying decipher a code that I found on the internet. The internet code gives the correct answer, however I cannot understand some of the syntax. Could someone tell me what "m~=n " means. The code before this line reads:
>
> A=input('Enter the square matrix to be factorized ');
> [m,n]=size(A);
>
> --Ender--

well,...
- the syntax
     m~=n
   returns LOGICAL TRUE if M is NOT EQUAL N
   returns LOGICAL FALSE if M is EQUAL N

     [m,n]=size(magic(3));
     tf=m~=n
% tf = 0 % <- false
     [m,n]=size(rand(3,4));
     tf=m~=n
% tf = 1 % <- true

note: TF typically is used in conditional expressions, eg,
     IF/ELSEIF/ELSE
     WHILE
     SWITCH/CASE

us

Subject: What does this syntax mean

From: Bobby Cheng

Date: 15 Jan, 2010 21:43:45

Message: 4 of 10

Can you send the matrix here? Not sure what you mean by "MATLAB does not
give me the correct answer".

---Bob.

"us " <us@neurol.unizh.ch> wrote in message
news:hiqn4j$joj$1@fred.mathworks.com...
> "Ender " <jr147@msstate.edu> wrote in message
> <hiqlta$25v$1@fred.mathworks.com>...
>> I am trying to use MATLAB to do LU factorization. I originally tried to
>> use the MATLAB function to do the factorization, but the answers that
>> MATLAB gave me utilizing the [L,U] = lu(A) command, but MATLAB does not
>> give me the correct answer. I am not sure why.
>>
>> So I am currently trying decipher a code that I found on the internet.
>> The internet code gives the correct answer, however I cannot understand
>> some of the syntax. Could someone tell me what "m~=n " means. The code
>> before this line reads:
>>
>> A=input('Enter the square matrix to be factorized ');
>> [m,n]=size(A);
>>
>> --Ender--
>
> well,...
> - the syntax
> m~=n
> returns LOGICAL TRUE if M is NOT EQUAL N
> returns LOGICAL FALSE if M is EQUAL N
>
> [m,n]=size(magic(3));
> tf=m~=n
> % tf = 0 % <- false
> [m,n]=size(rand(3,4));
> tf=m~=n
> % tf = 1 % <- true
>
> note: TF typically is used in conditional expressions, eg,
> IF/ELSEIF/ELSE
> WHILE
> SWITCH/CASE
>
> us
>

Subject: What does this syntax mean

From: Ender

Date: 17 Jan, 2010 19:21:04

Message: 5 of 10

MATLAB does not give me the same answer that is in the back of the Numerical Analysis book that I am using. The Matrix A is:

A=[0,2,-1;1,-1,2;1,-1,4]

The answer is:

L = [1,0,0;1.5,1,0;1.5,1,1] & U = [2,-1,1;0,4.5,7.5;0,0,-4]

When using the MATLAb LU Factorization code [L,U] = lu(A)
I do not get this answer. I don't know why. However there is a code that I found on the internet:

http://www.mathworks.com/matlabcentral/fileexchange/7779-lu-factorization
-by-doolittles-method

That gives me the answer that I am looking for. I just don't totally understand what the code is doing to get the right answer.


"Bobby Cheng" <bcheng@mathworks.com> wrote in message <hiqnih$h83$1@fred.mathworks.com>...
> Can you send the matrix here? Not sure what you mean by "MATLAB does not
> give me the correct answer".
>
> ---Bob.
>
> "us " <us@neurol.unizh.ch> wrote in message
> news:hiqn4j$joj$1@fred.mathworks.com...
> > "Ender " <jr147@msstate.edu> wrote in message
> > <hiqlta$25v$1@fred.mathworks.com>...
> >> I am trying to use MATLAB to do LU factorization. I originally tried to
> >> use the MATLAB function to do the factorization, but the answers that
> >> MATLAB gave me utilizing the [L,U] = lu(A) command, but MATLAB does not
> >> give me the correct answer. I am not sure why.
> >>
> >> So I am currently trying decipher a code that I found on the internet.
> >> The internet code gives the correct answer, however I cannot understand
> >> some of the syntax. Could someone tell me what "m~=n " means. The code
> >> before this line reads:
> >>
> >> A=input('Enter the square matrix to be factorized ');
> >> [m,n]=size(A);
> >>
> >> --Ender--
> >
> > well,...
> > - the syntax
> > m~=n
> > returns LOGICAL TRUE if M is NOT EQUAL N
> > returns LOGICAL FALSE if M is EQUAL N
> >
> > [m,n]=size(magic(3));
> > tf=m~=n
> > % tf = 0 % <- false
> > [m,n]=size(rand(3,4));
> > tf=m~=n
> > % tf = 1 % <- true
> >
> > note: TF typically is used in conditional expressions, eg,
> > IF/ELSEIF/ELSE
> > WHILE
> > SWITCH/CASE
> >
> > us
> >
>

Subject: What does this syntax mean

From: Phil Goddard

Date: 18 Jan, 2010 00:41:03

Message: 6 of 10

lu factorizations aren't unique so getting a different result doesn't necessarily mean that either implementation is wrong.

But I would also point out that the L and U that you assert are correct do not reproduce your A.

>> L = [1,0,0;1.5,1,0;1.5,1,1], U = [2,-1,1;0,4.5,7.5;0,0,-4], L*U
L =
    1.0000 0 0
    1.5000 1.0000 0
    1.5000 1.0000 1.0000
U =
    2.0000 -1.0000 1.0000
         0 4.5000 7.5000
         0 0 -4.0000
ans =
     2 -1 1
     3 3 9
     3 3 5

Phil.

Subject: What does this syntax mean

From: Steven Lord

Date: 18 Jan, 2010 03:58:13

Message: 7 of 10


"Ender " <jr147@msstate.edu> wrote in message
news:hivnv0$nq5$1@fred.mathworks.com...
> MATLAB does not give me the same answer that is in the back of the
> Numerical Analysis book that I am using. The Matrix A is:
>
> A=[0,2,-1;1,-1,2;1,-1,4]
>
> The answer is:
>
> L = [1,0,0;1.5,1,0;1.5,1,1] & U = [2,-1,1;0,4.5,7.5;0,0,-4]
>
> When using the MATLAb LU Factorization code [L,U] = lu(A)
> I do not get this answer. I don't know why.

When I use LU in MATLAB, I too receive a different answer than the one you
cited above. When I check the output of the LU call in MATLAB (by computing
L*U) I receive your original matrix. When I check the product of the two
matrices you gave above, I do not receive your original matrix.

It's entirely possible that your numerical analysis textbook may have the
wrong answer, or it could be that you read the answer to the wrong problem.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: What does this syntax mean

From: Ender

Date: 19 Jan, 2010 16:35:20

Message: 8 of 10

It is entirely possible that I mixed up the problems. I will check again than get back to you.

--Ender--


"Steven Lord" <slord@mathworks.com> wrote in message <hj0m8i$f9q$1@fred.mathworks.com>...
>
> "Ender " <jr147@msstate.edu> wrote in message
> news:hivnv0$nq5$1@fred.mathworks.com...
> > MATLAB does not give me the same answer that is in the back of the
> > Numerical Analysis book that I am using. The Matrix A is:
> >
> > A=[0,2,-1;1,-1,2;1,-1,4]
> >
> > The answer is:
> >
> > L = [1,0,0;1.5,1,0;1.5,1,1] & U = [2,-1,1;0,4.5,7.5;0,0,-4]
> >
> > When using the MATLAb LU Factorization code [L,U] = lu(A)
> > I do not get this answer. I don't know why.
>
> When I use LU in MATLAB, I too receive a different answer than the one you
> cited above. When I check the output of the LU call in MATLAB (by computing
> L*U) I receive your original matrix. When I check the product of the two
> matrices you gave above, I do not receive your original matrix.
>
> It's entirely possible that your numerical analysis textbook may have the
> wrong answer, or it could be that you read the answer to the wrong problem.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Subject: What does this syntax mean

From: Ender

Date: 20 Jan, 2010 17:43:04

Message: 9 of 10

I did make a mistake. I put the wrong value of A

A is:
A=[2 ,-1 ,1; 3 ,3 ,9; 3 ,3 ,5]

The answer should still be:
L = [1,0,0;1.5,1,0;1.5,1,1] U = [2,-1,1;0,4.5,7.5;0,0,-4]

If you multiply L*U you will get the same answer for A.

However, when I use [L,U]=lu(A)

I do not get the same result for L & U.

The link for this program "Doolittle.m" does give the correct answer with my original value of A. The link is:
http://www.mathworks.com/matlabcentral/fileexchange/7779-lu-factorization-by-doolittles-method

I do not know why the MATLAB code does not produce the same result. How can I make the MATLAB code output the answer that I am looking for.

--Ender--

"Ender " <jr147@msstate.edu> wrote in message <hj4n08$kme$1@fred.mathworks.com>...
> It is entirely possible that I mixed up the problems. I will check again than get back to you.
>
> --Ender--
>
>
> "Steven Lord" <slord@mathworks.com> wrote in message <hj0m8i$f9q$1@fred.mathworks.com>...
> >
> > "Ender " <jr147@msstate.edu> wrote in message
> > news:hivnv0$nq5$1@fred.mathworks.com...
> > > MATLAB does not give me the same answer that is in the back of the
> > > Numerical Analysis book that I am using. The Matrix A is:
> > >
> > > A=[0,2,-1;1,-1,2;1,-1,4]
> > >
> > > The answer is:
> > >
> > > L = [1,0,0;1.5,1,0;1.5,1,1] & U = [2,-1,1;0,4.5,7.5;0,0,-4]
> > >
> > > When using the MATLAb LU Factorization code [L,U] = lu(A)
> > > I do not get this answer. I don't know why.
> >
> > When I use LU in MATLAB, I too receive a different answer than the one you
> > cited above. When I check the output of the LU call in MATLAB (by computing
> > L*U) I receive your original matrix. When I check the product of the two
> > matrices you gave above, I do not receive your original matrix.
> >
> > It's entirely possible that your numerical analysis textbook may have the
> > wrong answer, or it could be that you read the answer to the wrong problem.
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> >

Subject: What does this syntax mean

From: Phil Goddard

Date: 21 Jan, 2010 03:41:08

Message: 10 of 10


The lu algorithm used by MATLAB for a full matrix uses partial pivoting, which the Doolittle algorithm does not, hence the difference in results.
(You'd need to look at the references near the end of the help for the lu function to see the specifics of the pivoting.)

It seems that you can only turn pivoting off when doing an lu on a sparse matrix, so you could (rather inefficiently) do the following:

>> [l,u] = lu(sparse(A),0); %the 0 specifies not to pivot
>> L = full(l)
>> U = full(u)

to get the U and L that you seek.

Phil.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us