3-D Least Squares Fit
by Yufang Hao
Purpose: Given data in x, y, z, find and plot the best linear least-squares fit to it.
> restart; with(stats[fit]);
> x_data := [12,13,16,15,11]:
> y_data := [5,3,4,2,6]:
> z_data := [7,9,6,8,6]:
The best linear least-squares fit to the given data can be easily found by the leastsquare() command. For more information, please refer to the help page ?stats[fit] command.
> leastsquare[[x,y,z]]([x_data,y_data,z_data]);
> f := unapply(rhs(%),x,y);
The following manipulations make sure that the plot contains every data point.
> x_max := max(seq(x_data[i],i=1..nops(x_data))): x_min := min(seq(x_data[i],i=1..nops(x_data))):
> y_max := max(seq(y_data[i],i=1..nops(y_data))): y_min := min(seq(y_data[i],i=1..nops(y_data))):
> with(plots):
Warning, the name changecoords has been redefined
> data_points := pointplot3d( {seq([x_data[i],y_data[i],z_data[i]], i=1..nops(z_data)) }, symbol=circle, color=blue):
> best_curve := plot3d(f(x,y), x=x_min..x_max, y=y_min..y_max, style=wireframe):
> display3d([best_curve, data_points], labels=['x','y','z'], axes=normal, orientation=[145,45]);