1
$\begingroup$

My function "DefineRss[]" takes in three arguments and outputs a plane value.

It sits in the first argument of the function plot3D and that in turn sits in the manipulate function

The function also uses the arguments with an algorithm to sum up the distance between each plot point on a 3d table and the newly created plane (3d table data set up at the begining of the program and split into three rows(zxy)) .

the variable rss is supposed to be initialized each time a slider value is changed.

The effect I am trying to get the program to show is to have the rss change before your eyes as the user slides sliders up and down tilting the plane.

The code appears to work but the variable rss remains the same, the output is always the sum of the values and does not change as sliders are changed at all, infact it jsut outputs the sum.

    (*Extract table from directory and initialise t1 appropriatley*)
t = Import[
   "c:\\Users\\Sony \
Viao\\OneDrive\\Documents\\SumOfSquares\\RegressionExample.xls", 
   "xls"];
tp = t[[1]];
t1 = Table[tp[[i]], {i, 2, Length[tp]}];

(* xcol, ycol and zcol are the three tables belonging to t1*)
xcol = Table[t1[[i]][[1]], {i, 1, Length[t1]}];
ycol = Table[t1[[i]][[2]], {i, 1, Length[t1]}];
zcol = Table[t1[[i]][[3]], {i, 1, Length[t1]}];

(* rss is initialised to none for now, it should be updated \
dynamically in the manipulate function*)
rss = none;

(* This function returns the equation for a plane and sums up the \
residuals between the plot points and the plane and initialises the \
variable rss*)
DefineRSS[a_, b_, c_] := Block[{output}, output = ((a*x) + (b*y) + c);

  rss = ((Sum[
       zcol[i] - (a*xcol[i] - b*ycol[i] - c), {i, 1, Length[t1]}])^2);
  output]


(*Manipulate allows the function Definerss to both take arguments for \
a plane and sum up the rss variable*)
Manipulate[
 Plot3D[DefineRSS[a, b, c], {x, -10, 10}, {y, -10, 10}, 
  AxesLabel -> {X, Y, Z}], {a, -5, 5}, {b, -5, 5}, {c, -5, 5}]

(*outputs rss*)
rss
$\endgroup$
1
  • 1
    $\begingroup$ To have something update, it has to really appear in the manipulate expression, or be explicitly tracked. your rss is neither of these. It is outside of the Manipulate scope. If you had a minimal example with the import, one can better show you how, but I can't run your code. $\endgroup$
    – Nasser
    Commented Jan 31, 2015 at 18:33

1 Answer 1

2
$\begingroup$

The OP's basic approach seems to work for me, although the code for rss doesn't work because zcol etc. are arrays, not functions. Assuming you want the sum of squares of the residuals, here is an example fix:

{xcol, ycol, zcol} = RandomReal[{-10, 10}, {3, 20}];

Clear[DefineRSS];
DefineRSS[a_?NumericQ, b_?NumericQ, c_?NumericQ] := (
  rss = Total[(zcol - (a*xcol + b*ycol + c))^2];
  (a*x) + (b*y) + c)

Manipulate[
 Plot3D[DefineRSS[a, b, c], {x, -10, 10}, {y, -10, 10}, 
  AxesLabel -> {X, Y, Z}],
 {a, -5, 5}, {b, -5, 5}, {c, -5, 5}]
Dynamic[rss]

Update: If I understood the comment right, here is how to show the random points and residuals. Adjust the PlotRange to suit.

Manipulate[
 With[{plane = DefineRSS[a, b, c]},
  Show[
   Plot3D[plane, {x, -10, 10}, {y, -10, 10}, AxesLabel -> {X, Y, Z}, 
    PlotLabel -> rss],
   Graphics3D[{
     Red, PointSize[Medium], Point[Transpose[{xcol, ycol, zcol}]],
     Line[
      Transpose[
       {{xcol, ycol, zcol}, {xcol, ycol, plane /. Thread[{x, y} -> {xcol, ycol}]}},
       {2, 3, 1}]]
     }],
   PlotRange -> {{-11, 11}, {-11, 11}, {-50, 50}}
   ]
  ],
 {a, -5, 5}, {b, -5, 5}, {c, -5, 5}]

Note: Originally I mentioned that TrackedSymbols :> Manipulate seemed to be needed. Today it does not. I must have had some lurking definition interfering in the dynamic updating tracking before.

$\endgroup$
3
  • $\begingroup$ This works perfectly, just the rss is so stupidly huge it cant be right. $\endgroup$ Commented Feb 2, 2015 at 21:15
  • $\begingroup$ Thank you very much for your help, do you know how I can get the plot points and residuals between them and the new plane? how would they slot in the manipulate function?? $\endgroup$ Commented Feb 2, 2015 at 21:15
  • 1
    $\begingroup$ @JethroDevøn See if the update shows what you want. I think the rss can be very big if the points are taken at random between ±10 and rss is the sum of squares of the residuals. Perhaps you meant a different formula for rss? $\endgroup$
    – Michael E2
    Commented Feb 3, 2015 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.