|
From: Robert D. <rob...@gm...> - 2015-01-02 02:52:17
|
On 2015-01-01, Dr. M Kanagasabapathy <rr...@gm...> wrote:
> load (lsquares) $
> p:matrix([x, y set Data 1], [x, y set Data 2], [x, y set Data 3]...)
> lsquares_estimates ( p, [x,y], a*x^(n-1)+b*x^(n-2) ..., [a, b,c,...]);
>
> But it throws syntax error. Kindly give the exact code for the polynomial
> fit for numeric data.
I find the following works OK. If it doesn't work for you, can you copy
and paste the exact syntax you tried and the error message?
(%i1) display2d : false $
(%i2) p : apply (matrix, makelist ([random (100), random (100)], 10));
(%o2) matrix([12,2],[34,85],[4,91],[29,85],[98,3],[35,65],[40,26],[39,20],
[19,4],[97,6])
(%i3) e : y = apply ("+", makelist (a[i]*x^(10 - i), i, 10));
(%o3) y = a[1]*x^9+a[2]*x^8+a[3]*x^7+a[4]*x^6+a[5]*x^5+a[6]*x^4+a[7]*x^3
+a[8]*x^2+a[9]*x+a[10]
(%i4) load (lsquares) $
(%i5) foo : lsquares_estimates (p, [x, y], e, makelist (a[i], i, 10));
(%o5) [[a[1] = -1155755190270798587/981785532627902201799600000,
a[2] = 646809466727286179951/1456843048415596815573600000,
a[3] = -173006106410953036242361/2509007472271305626821200000,
a[4] = 1010049894065024960029219/175047032949160857685200000,
a[5] = -102625604549102766333482597/358429638895900803831600000,
a[6] = 970282049812993188830584957/111511443212058027858720000,
a[7] = -3624471009173316623409508138583/22581067250441750641390800000,
a[8] = 9623721002446879874478041103593/5645266812610437660347700000,
a[9] = -12328448873345781003075430937/1344111145859628014368500,
a[10] = 2341593309905654785954/131405040321550875]]
Note that p is just random numbers, and I constructed the equation e
instead of typing it out.
Since finding the coefficients requires only solving a linear system
of equations, Maxima finds an exact solution.
The usual disclaimer about fitting high order polynomials applies
here ...
> *Also please help me to get the code to load a set of numeric data from an
> Excel file into Maxima.*
Maxima can read comma-separated values (via read_list, read_matrix,
etc). So you can export the data from Excel as .csv and then read it
into Maxima. E.g. mydata : read_matrix("myfile.csv", 'comma);
best,
Robert Dodier
|