<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2008 - INRIA
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 -->
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:lang="en" xml:id="lsq">
    <refnamediv>
        <refname>lsq</refname>
        <refpurpose>linear least square problems.  </refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>X=lsq(A,B [,tol])</synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>A</term>
                <listitem>
                    <para>Real or complex (m x n) matrix</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>B</term>
                <listitem>
                    <para>real or complex (m x p) matrix</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>tol</term>
                <listitem>
                    <para>positive scalar,  used to determine the effective rank of A
                        (defined as the order of the largest leading triangular
                        submatrix R11 in the QR factorization with pivoting of A,
                        whose estimated condition number &lt;= 1/tol. The tol default value is
                        set to <literal>sqrt(%eps)</literal>.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>X</term>
                <listitem>
                    <para>real or complex (n x p) matrix</para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>
            <literal>X=lsq(A,B)</literal> computes the minimum norm least square solution of
            the equation <literal>A*X=B</literal>, while <literal>X=A \ B</literal> compute a least square
            solution with at at most <literal>rank(A)</literal>  nonzero components per column.
        </para>
    </refsection>
    <refsection>
        <title>References</title>
        <para>
            <literal>lsq</literal> function is  based on the LApack functions DGELSY for
            real matrices and ZGELSY for complex matrices.
        </para>
    </refsection>
    <refsection>
        <title>Examples</title>
        <programlisting role="example"><![CDATA[
//Build the data
x=(1:10)';

y1=3*x+4.5+3*rand(x,'normal');
y2=1.8*x+0.5+2*rand(x,'normal');
plot2d(x,[y1,y2],[-2,-3])
//Find the linear regression
A=[x,ones(x)];B=[y1,y2];
X=lsq(A,B);

y1e=X(1,1)*x+X(2,1);
y2e=X(1,2)*x+X(2,2);
plot2d(x,[y1e,y2e],[2,3])

//Difference between lsq(A,b) and A\b
A=rand(4,2)*rand(2,3);//a rank 2 matrix
b=rand(4,1);
X1=lsq(A,b)
X2=A\b
[A*X1-b, A*X2-b] //the residuals are the same
 ]]></programlisting>
    </refsection>
    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="backslash">backslash</link>
            </member>
            <member>
                <link linkend="inv">inv</link>
            </member>
            <member>
                <link linkend="pinv">pinv</link>
            </member>
            <member>
                <link linkend="rank">rank</link>
            </member>
        </simplelist>
    </refsection>
</refentry>
