kill(all)$

program : least square method ,version 1.0
peter.vlasschaert@gmail.com,31/05/2019

load :packages.  (we load other packages later , "see  below")
==============

load(lrats)$

some data points : to find least square fitting.
--------------------------------------------------------------------

x,y : data points
==============

x:[0.7,1.1,1.5,2.1,2.3];
y:[0.8,1.0,1.4,1.6,1.7];

find length of list , x=y
===================

np:length(x);

sum((a*x[i]+b-y[i])^2,i,1,np)
=======================

p1:sum((a*x[i]+b-y[i])^2,i,1,np);

optimization : through differentation with respect to 'a,b'
===============================================

p2a:diff(p1,a) = 0,expand;
p2b:diff(p1,b) = 0,expand;

we need to find : a,b
=================

ratprint:false;

reason : extra info blocked;

try it  : put ratprint : true.

p3:solve([p2a,p2b],[a,b]);

p3a:flatten(p3);

equation :Y= a*X+b , fitting the data.
==============================
use : a,b

p4:Y=a*X+b;

p4a:subst(p3a,p4);

p4b:float(p4a);

p4c:rhs(p4b);

makelist from data : use by wxplot2d
===============================

legend : discrete2 on the graph
------------------------------------------------

xylist:makelist([x[i],y[i]],i,1,np);

plot data and function on the same graph
===================================

wxplot2d([p4c,['discrete,xylist]],[X,0.4,3],
    [style,[lines,2,4],[points,3,1,2]])$
    

forget(a,b);

a;
b;

load packages for problem : check with the above calculation.
====================================================

load(lsquares)$

data format : matrix with 2 columns and np rows.
---------------------------------------------------------------------------

p5:transpose(matrix(x,y));

function :lsquares_estimates.
---------------------------------------------

p6:lsquares_estimates(p5,[x1,y1],y1=a*x1+b,[a,b]);

same : 'p3' = 'p6'

forget(a,b,c);

a;
b;
c;

info: quadratic , 'fitting'  : only show how to use function.
--------------------------------------------------------------------------------------

p6a:lsquares_estimates(p5,[x2,y2],y2=a*x2^2+b*x2+c,[a,b,c]);

p6b:float(p6a);