Message Boards Message Boards

Problem with if statement inside Manipulate for CDF development

Posted 8 years ago

Hello, I am a new Mathematica user and my goal is to be able to create a CDF for scientific calculation. I am currently teaching myself and CDF development through a toy problem. Following is the code to take a 1D numerical integral.

Manipulate[
  DynamicModule[{f = Sin[x], xR = 1, xL = 0, 
    Res = 0.4596976941318603}, Column[
    {Style["1D Definite Integral Calculator", "Function"] ,
     Row[{"Lower Limit ", InputField[Dynamic[xL], Number]}, Spacer[5]],
     Row[{"Upper Limit ", InputField[Dynamic[xR], Number]}, Spacer[5]],
     Row[{"Function  ", InputField[Dynamic[f]]}, Spacer[20]], 
     Button[Style["Calculate", Green, Bold], 
      Dynamic[Res = DGL[f, xR, xL, np, nw];]], 
     Row[{"Result", InputField[Dynamic[Res], Enabled -> False]}, 
      Spacer[51]],

     xLcheck = NumberQ[xL], xRcheck = NumberQ[xR], 
     If[fp == True && xLcheck == True && xRcheck == True, 
      InputField[
       Dynamic[Plot[f, {x, xL, xR}, Dynamic[PlotLabel -> f], 
         ImageSize -> Full]], FieldSize -> {30, 15}]],

      Button[
      Mouseover[Style["http://www.wolfram.com", "Hyperlink"], 
       Style["http://www.wolfram.com", "HyperlinkActive"]], 
      NotebookLocate[{URL["http://www.wolfram.com"], None}], 
      Appearance -> None]
     }]],
  {{fp, True, "Plot the function"}, {False, True}, Checkbox, 
   ControlPlacement -> Bottom}, {{np, 16, "NumberFormat"}, 
   ControlPlacement -> Bottom},
  {{nw, 16, "WorkingPrecision"}, ControlPlacement -> Bottom},
     Initialization :> (
    DGL[f_, xR_, xL_, np_, nw_] := Module[{xLcheck, xRcheck},
      xLcheck = NumberQ[xL];
      xRcheck = NumberQ[xR];
      If[xLcheck == True && xRcheck == True ,
       ResultTF = 
        SetPrecision[
         NIntegrate[f, {x, xL, xR}, 
          WorkingPrecision -> Re[IntegerPart[nw]]], 
         Re[IntegerPart[np]]], 
       "Enter numeric inputs for Lower and Upper Limits"]]
    )
  ]

I am having a several problems and I also think some of the things I am doing can be coded more elegantly.

  1. Even though for the inputfield xR and xL I specified "Number" for the input when I entered a non-numeric input the code returns an error. However according to help document in that case the input field simply should not updated. To overcome that issue I created two variables xLcheck and xRcheck inside the DGL function and when these variables are TRUE calculation is done. I don't know why the "number" specification does not work but my approached solved the problem for the integration. I tried to the same for the plotting but this time it did not work. As you can see from the code right before the plotting I defined xLcheck and xRcheck. (I don't know how to get those values out from my function DGL. Actually I don't know how to get multiple variables out of the function). xLcheck and xRcheck did not work for the plot. If you run the code the values are True for both of them.

These are the two main problems that I am facing other than creating a package for DGL function and using the needs command for it. Thank you for the support.

Erdem

POSTED BY: Erdem Uguz
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