Message Boards Message Boards

Im having trouble making a few lines of code work within manipulate

Posted 10 years ago

I hope this code gives a rough Idea of what I am trying to do, can someone tell me how I've gone about using manipulate in the wrong way and how I might look to restructure this existing code?

What I require it to do is allow me to manipulate the plain with the sliders and display the change graphicaly, I then want the values to directly effect what the sum of the residuals would be if they were to extend down to the manipulated plane, I also want to display that RSS and have it change as the sliders move.

I also cant find a bug that is not allowing the code to find the file regression.xls

The manipulate header

manipulate [

Body of the code

model = z /. Solve[a x + b y + c z + d == 0, z][[1]];
 (-d - a x - b y)/c
   (*get data*)
   data = Import["RegressionExample.xls"][[1]] // Rest;
 (*and header*)
 header = Import["RegressionExample.xls"][[1]] // First
     {"Adverts", "Airplay", "Sales"}
     (*fit the data to the model*)
     fit = NonlinearModelFit[data, model, {a, b, c, d}, {x, y}]
     NonlinearModelFit::fitd : 
       "First argument \!\(data\) in NonlinearModelFit is not a list \
or a rectangular array. \!\(\*ButtonBox[\">>\", \
ButtonStyle->\"Link\", ButtonFrame->None, \
ButtonData:>\"paclet:ref/NonlinearModelFit\", ButtonNote -> \
\"NonlinearModelFit::fitd\"]\)"
        FittedModel[
         0.000927139168578378` (44355.63156634302` + 
            93.71519296640882` x + 3870.819045915761` y)]
        FittedModel[
         0.000927139168578378` (44355.63156634302` + 
            93.71519296640882` x + 3870.819045915761` y)]
        (*sum of squared residuals*)
        sumOfSquaredResiduals = fit["FitResiduals"] // Norm
       693.1292063621133`
       (*range for plotting*)
       {advertsMin, advertsMax} = {Min[#], Max[#]} &@data[[All, 1]]
       {9.1`, 2271.86`}
       {airplayMin, airplayMax} = {Min[#], Max[#]} &@data[[All, 2]]
       {0.`, 63.`}
       (*make the plots separately then combine*)
       (*best fit plane*)
       bestFitPlanePlot = 
        Plot3D[fit[x, y], {x, advertsMin, advertsMax}, {y, airplayMin,
           airplayMax}, PlotStyle -> {Blue, Opacity[.2]}];
 (*original data*)
 dataPlot = 
  ListPointPlot3D[data, PlotStyle -> Directive[Red, PointSize[.015]]];
 (*make the drop lines*)
 lines = Line[{#, {#[[1]], #[[2]], fit[#[[1]], #[[2]]]}}] & /@ data;
 (*plot them*)
 linePlot = Graphics3D[lines];

The Manipulate footer

, {a, -5, 5}, {b, -5, 5}, {c, -5, 5}, {d, -5, 5}]
POSTED BY: jethro holcroft
2 Replies

In addition to Craig's excellent suggestions, Manipulate starts with a capital M.

See http://reference.wolfram.com/language/tutorial/GettingUsedToTheWolframLanguage.html and links therein.

POSTED BY: Bruce Miller

It is difficult to help without code to cut and paste into your responders' own notebook. It is advisable to use the code block mark up in the top left icon in this editor.

Among other things, it appears that your import is picking up strings and FindFit is objecting to fitting a model. Check the dimensions and FullForm of data.

Advice. Get commands working in small compact examples. Integrate those parts into a complete working example. Generalize with Manipulate as one of the last steps.

POSTED BY: W. Craig Carter
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract