next up previous index
Next: Fitting in Mathematica Up: Fitting Previous: A Summary of Fitting

$\chi ^2$ Fitting in Maple

In Maple things initially proceed in a much the same way as they did in Maxima. And so, we have to define arrays xi, yi, and $\sigma _i$ first: 

\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}> x := arr...
...igma := array(1 .. 300, [])>\end{verbatim}\end{tex2html_preform}}
\end{figure}

The syntax of the array command is slightly different from that of Maxima. We will summarise those differences in a table towards the end of this chapter. In short, the command array creates a new array with dimensions given its first argument. The second argument is optional and can be used to initialise the values in an array.

Now we define $\chi ^2$:

\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}> chi_2 :=...
... / 2
----- sigma[i]
i = 1>\end{verbatim}\end{tex2html_preform}}
\end{figure}

The syntax of sum is nearly the same as in Maxima. The only difference is the way that the range of i is specified. The next step is to evaluate $\partial\chi^2/\partial a$ and $\partial\chi^2/\partial b$, equate the partial derivatives to zero and solve the resulting linear equations:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}> da := di...
...
----- \ sigma[i] /
i = 1>\end{verbatim}\end{tex2html_preform}}
\end{figure}

The solve command of Maple is much more powerful than its relative in Maxima, so we don't have to modify equations da = 0 and db = 0 any further. On the other hand, we have to save the result on a list, which we are going to call solutions, because Maple doesn't generate expression labels automatically. Having saved the solution we will then be able to chop it to pieces and make corresponding assignments to a and b.
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}> solution...
... i = 1 / \i = 1 / \i = 1 //>\end{verbatim}\end{tex2html_preform}}
\end{figure}

Observe that at least this time Maxima does a better job of formatting output, although the above looks a lot better when printed on the Maple X11 Worksheet using real Greek letters. The result of calling solve is a list of two equations. solutions[1] returns the first equation, b = ..., and solutions[2] returns the second one, a = .... In order to make the assignments we have to use function rhs, which extracts the right hand side of an equation it is applied to: 
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}> b0 := rh...
... i = 1 / \i = 1 / \i = 1 //>\end{verbatim}\end{tex2html_preform}}
\end{figure}

and
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}> a0 := rh...
... i = 1 / \i = 1 / \i = 1 //>\end{verbatim}\end{tex2html_preform}}
\end{figure}

Maple would make a mess out of the solutions list if you tried,
b := rhs(solutions[1]);
instead of b0 := rhs(solutions[1]), because it would substitute the new value of b into the solutions list at the same time. It is safer to create new variables a0 and b0 while extracting the relevant assignments with rhs.

So far, so good. Now we are going to hit the same bug as we have already found in Maxima. Taking derivatives $\partial a / \partial y_k$ and $\partial b/\partial y_k$ returns zero in both cases!

\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}> diff(a0,...
...alloc=1638100, time=1.74
0>\end{verbatim}\end{tex2html_preform}}
\end{figure}

Unfortunately, Maple in this context is even worse than Maxima. Let us look again at a simpler expression

\begin{displaymath}\sum_{i=1}^N x_i y_i
\end{displaymath}

and let us try a Maple equivalent of Maxima's command
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}ev('diff(s...
... n), y[k]), [n=5, k=3], diff);\end{verbatim}\end{tex2html_preform}}
\end{figure}

that worked so nicely for us in the previous section. Here is what Maple does with this expression:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}> eval(Dif...
...[4] y[4] + x[5] y[5])
dy[k]
>\end{verbatim}\end{tex2html_preform}}
\end{figure}

As you see, Maple steadfastly refuses even to substitute 3 for k, treating the whole expression dy[k] as one unstructured object. We have to request differentiation with respect to y[3] explicitly:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}> eval(Dif...
...)
dy[3]> value(%);
x[3]>\end{verbatim}\end{tex2html_preform}}
\end{figure}

Since we are forced to subject ourselves to such indignities, we can just as well evaluate the sums at the very beginning and differentiate the result explicitly with respect to y3.

And so this is our Maple result for $\partial a / \partial y_k$, for n=5 and k=3:

\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}> da0y := ...
...gma[3] sigma[4] sigma[5] //>\end{verbatim}\end{tex2html_preform}}
\end{figure}

And similarly for $\partial b/\partial y_k$ for n=5 and k=3:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}> db0y := ...
...gma[3] sigma[4] sigma[5] //>\end{verbatim}\end{tex2html_preform}}
\end{figure}

There is no exact equivalent of Maxima's pickapart command in Maple, so we can't simplify expressions for da0y and db0y, easily, in terms of new, dynamically generated variables.  Maple has some low level utilities for splitting expressions apart and some high level ones, of which you have already encountered rhs. In this case you could also try numer and denom, which would extract  numerator and denominator from both da0y and db0y, but while doing so, they would fully expand the resulting expressions too, making them even less readable and less manageable.

In summary, this is where we've got to stop in Maple. The obtained expressions for a, b, $\partial a / \partial y_k$, and $\partial b/\partial y_k$ are correct (cf. Section 2.1.8), and can be easily compared to results returned by Maxima.


next up previous index
Next: Fitting in Mathematica Up: Fitting Previous: A Summary of Fitting
Zdzislaw Meglicki
2001-02-26