9 Replies Latest reply on Sep 11, 2014 6:15 AM by kglad

    Math.log() to draw graph question

    hirstwood Level 1

      Hi

       

      I'm trying to generate a graph to represent exponential functions y =loga x which uses the basic function y =log10 x. I have successfully generated y = ax but even using this as an inverse isn't working.

      It's old code that I'm editing and most of the maths is done in class files.

       

      But basically to draw the curve and the points I'm using:

      for (i=0; i<=(xMax-xMin)/dx; i++)

        {

        myX=xMin+i*dx;

        if (type==1)

        {

        myY=Math.pow(a*myX+b,2)+c;

        }

        if (type==2)

        {

        //myY=a*Math.pow(myX,2)+b*myX+c;

       

        myY = (myX-b)*(myX-c);

       

        }

        if (type==3)

        {

        //myY=a*(Math.pow(myX+b,2))+c;

        myY=a*(Math.pow(myX-b,2))+c;

        }

       

        //********************** SH 09SEPT2014

        if(type == 4)

        {

        myY = a * Math.exp(myX);

        }

        if(type == 5)

        {

        myY = myX*Math.log(a);

        }

        //***********************

       

        if ( (myY>=yMin) and (myY<=yMax) )

        {

        Data[j]=new Array;

        Data[j]=[myX,myY];

        j=j+1;

        } else if (j>0) {

        if (Data[j-1][0]<>undefined)

        {

        Data[j]=new Array;

        Data[j]=[undefined,undefined];

        j=j+1;

        }

        }

        • 1. Re: Math.log() to draw graph question
          kglad Adobe Community Professional & MVP

          myY = myX*Math.log(a);

           

          looks wrong.  if that's supposed to be y =loga x, it should be


          myY=Math.log(myX)/Math.log(a);

          • 2. Re: Math.log() to draw graph question
            hirstwood Level 1

            Thanks for the help! I'm still getting the same issue   which we think is to do with the asymptote (as with the the trig graph tan). I'm going through all the class files but can't see anything that handles it...... back to the drawing board......

            logaxGraphForum.jpg

            • 3. Re: Math.log() to draw graph question
              kglad Adobe Community Professional & MVP

              what issue?

               

              and there's no way your equation and mine are the same.

              • 4. Re: Math.log() to draw graph question
                hirstwood Level 1

                issue = the curve drawn flips off the graph (blue line). Which is exactly what happened before using your equation. I've no reason to lie about this - I came here in good faith for help, and I received your help for which I am grateful and was in no way being ungrateful.

                 

                I've copied the entire class below - the issue will be in the drawFunction

                 

                class Quadratic extends MovieClip {

                  public var className:String;

                  public var type:Number;

                  // type=1 (ax+b)^2+c

                  // type=2 ax^2+bx+c

                  // type=3 a(x+b)^2+c

                  public var a:Number;

                  public var b:Number;

                  public var c:Number;

                 

                  private var gridWidth:Number;

                  private var gridHeight:Number;

                  private var xMin:Number;

                  private var xMax:Number;

                  private var yMin:Number;

                  private var yMax:Number;

                  private var leftSpace:Number;

                  private var topSpace:Number;

                  private var xUnit:Number;

                  private var yUnit:Number;

                  private var numTypes:Number;

                 

                  public var Data:Array;

                 

                  function Quadratic()

                  {

                  // constructor

                  }

                 

                  function init(myParams:Array, myType:Number, myDims:Array, myStartAt:Array, XDims:Array, YDims:Array)

                  {

                  trace("Quadratic init");

                  className="Quadratic";

                  a=1;

                  b=0;

                  c=0;

                  if (myParams.length>0) { a=myParams[0]; }

                  if (myParams.length>1) { b=myParams[1]; }

                  if (myParams.length>2) { c=myParams[2]; }

                 

                  numTypes=5;//************************* 3

                  type=2;

                  if ( (myType>0) and (myType<=numTypes)) { type=myType; }

                 

                  gridWidth=100;

                  gridHeight=100;

                  if (myDims.length>0) { gridWidth=myDims[0]; }

                  if (myDims.length>1) { gridHeight=myDims[1]; }

                 

                  leftSpace=0;

                  topSpace=0;

                  if (myStartAt.length>0) { leftSpace=myStartAt[0]; }

                  if (myStartAt.length>1) { topSpace=myStartAt[1]; }

                 

                 

                  xMin=0;

                  xMax=10;

                  if (XDims.length>0) { xMin=XDims[0]; }

                  if (XDims.length>1) { xMax=XDims[1]; }

                  yMin=0;

                  yMax=10;

                  if (YDims.length>0) { yMin=YDims[0]; }

                  if (YDims.length>1) { yMax=YDims[1]; }

                 

                  getDataPoints();

                  drawFunction();

                 

                  }

                 

                 

                  function setParams(myParams:Array)

                  {

                  if (myParams.length>0) { a=myParams[0]; }

                  if (myParams.length>1) { b=myParams[1]; }

                  if (myParams.length>2) { c=myParams[2]; }

                  }

                 

                  function reDraw()

                  {

                  clear();

                  getDataPoints();

                  drawFunction();

                  }

                 

                  public function getDataPoints() {

                // trace("getDataPoints");

                  var dx:Number;

                  var myX:Number;

                  var myY:Number;

                  var i:Number;

                  var j:Number;

                  var endX1:Number;

                  var endX2:Number;

                  var endY:Number;

                  var term:Number;

                  var xMaxY:Number;

                 

                  xUnit=gridWidth/(xMax-xMin);

                  yUnit=gridHeight/(yMax-yMin);

                 

                 

                  dx=0.5/xUnit;

                // trace("dx="+dx);

                  Data = new Array;

                  j=0;

                 

                  for (i=0; i<=(xMax-xMin)/dx; i++)

                  {

                  myX=xMin+i*dx;

                  if (type==1)

                  {

                  myY=Math.pow(a*myX+b,2)+c;

                  }

                  if (type==2)

                  {

                  //myY=a*Math.pow(myX,2)+b*myX+c;

                 

                  myY = (myX-b)*(myX-c);

                 

                  }

                  if (type==3)

                  {

                  //myY=a*(Math.pow(myX+b,2))+c;

                  myY=a*(Math.pow(myX-b,2))+c;

                  }

                 

                  //********************** SH 09SEPT2014

                  if(type == 4)

                  {

                  myY = a * Math.exp(myX);

                  }

                  if(type == 5)

                  {

                  //myY = a*Math.log(myX);

                  myY=Math.log(myX)/Math.log(a);

                  }

                  //***********************

                 

                  if ( (myY>=yMin) and (myY<=yMax) )

                  {

                  Data[j]=new Array;

                  Data[j]=[myX,myY];

                  j=j+1;

                  } else if (j>0) {

                  if (Data[j-1][0]<>undefined)

                  {

                  Data[j]=new Array;

                  Data[j]=[undefined,undefined];

                  j=j+1;

                  }

                  }

                  }

                 

                  if (Data[j][0]==undefined)

                  {

                  j=j-1;

                  }

                 

                 

                 

                  }

                 

                  // returns a 2D array of (x,y) given the XValues sent in

                  public function getYValues(XValues:Array): Array {

                  trace("getYValues  XValues="+XValues);

                  var dx:Number;

                  var myX:Number;

                  var myY:Number;

                  var i:Number;

                  var j:Number;

                  var endX1:Number;

                  var endX2:Number;

                  var endY:Number;

                  var term:Number;

                  var xMaxY:Number;

                 

                  xUnit=gridWidth/(xMax-xMin);

                  yUnit=gridHeight/(yMax-yMin);

                 

                 

                  Data = new Array;

                 

                  for (i=0; i<XValues.length; i++)

                  {

                  myX=XValues[i];

                  if (type==1)

                  {

                  myY=Math.pow(a*myX+b,2)+c;

                  }

                  if (type==2)

                  {

                  //myY=a*Math.pow(myX,2)+b*myX+c;

                  myY = (myX-b)*(myX-c);

                  }

                  if (type==3)

                  {

                  //myY=a*(Math.pow(myX+b,2))+c;

                  myY=a*(Math.pow(myX-b,2))+c;

                  }

                 

                  //********************** SH 09SEPT2014

                  if(type == 4)

                  {

                  myY = a * Math.exp(myX);

                  }

                  if(type == 5)

                  {

                  //myY = a*Math.log(myX);

                  myY=Math.log(myX)/Math.log(a);

                  }

                  //***********************

                 

                  // add data regardless of whether on graph

                  Data[i]=new Array;

                  Data[i]=[myX,myY];

                  }

                 

                 

                  return Data;

                 

                  }

                 

                 

                  public function drawFunction()

                  {

                  trace("drawFunction");

                  var i:Number;

                 

                  xUnit=gridWidth/(xMax-xMin);

                  yUnit=gridHeight/(yMax-yMin);

                 

                 

                  lineStyle(2,0x0000FF,100);

                  i=0;

                  trace("1) i="+i+" "+Data[i][0]+","+Data[i][1]);

                  moveTo(leftSpace+(Data[0][0]-xMin)*xUnit, gridHeight+topSpace-(Data[0][1]-yMin)*yUnit);

                  for (i=1; i<Data.length; i++)

                  {

                  trace("2) i="+i+" "+Data[i][0]+","+Data[i][1]);

                  if ( (Data[i][0]==undefined) || (Data[i-1][0]==undefined) )

                  {

                  moveTo(leftSpace+(Data[i][0]-xMin)*xUnit, gridHeight+topSpace-(Data[i][1]-yMin)*yUnit);

                  } else {

                  lineTo(leftSpace+(Data[i][0]-xMin)*xUnit, gridHeight+topSpace-(Data[i][1]-yMin)*yUnit);

                  }

                  }

                 

                  }

                 

                }

                • 5. Re: Math.log() to draw graph question
                  kglad Adobe Community Professional & MVP

                  the first problem i see is the domain of the log function is incorrectly defined.  ie, myX=0 is allowed.

                  • 6. Re: Math.log() to draw graph question
                    hirstwood Level 1

                    OK, sorry my maths is obviously letting me down here, how would I go about doing that?

                    • 7. Re: Math.log() to draw graph question
                      kglad Adobe Community Professional & MVP

                      if(myX!=0){

                        myY=Math.log(myX)/Math.log(a);

                      }

                       

                      p.s when using the adobe forums, please mark helpful/correct responses, if there are any.

                      • 8. Re: Math.log() to draw graph question
                        hirstwood Level 1

                        Hi

                        Sorry - I had meant to mark the post already.

                         

                        And... LOL oh the shame I thought there was something far more maths that I was missing! (I had popped something similar in before seeing your reply).

                         

                        There are still 0's sneaking through, I have tried to eliminate any being passed to the class file. Thank you for all your help but I'm not sure I can spend much more time on this unfortunately.

                        • 9. Re: Math.log() to draw graph question
                          kglad Adobe Community Professional & MVP

                          you're welcome.