Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 04-10-2009, 08:54 AM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Newton-Raphson Method (Finding Square Roots)

Thanks to Tig for giving me access to the Testbed Server so that I could code this in GScript2.

I limited the allowed input to a maximum of 4 numbers because unlike Java's JOptionPane, Graal's GUIs do not automatically adjust in size with regards to length of text, so I tested the alignment out and everything aligned perfectly up to a max of 4 numbers.

Also, I attached a screenshot at the bottom. It's a bit blurry, but good enough to see how the program works. This is more of an educational thing, really, since you can easily find square roots through the square root function, only now you can see how square roots are derived.

PHP Code:
/*
 *Newton-Raphson Method
 *@author Gambet
*/

//#CLIENTSIDE
function onWeaponFired()
{
 
this.active = !this.active;
 
this.tab "     ";
 
 if (
this.active == trueInitialize();
  else 
selectionClose();
}

function 
Initialize()
{
 new 
GuiWindowCtrl("Selection_Window"
  {
   
profile GuiBlueWindowProfile;
   
= (screenwidth/2);
   
= (screenheight/2-100);
   
width 340;
   
height 90;
   
canclose canmaximize canminimize canresize "false";
   
text "Newton-Raphson Method -By: Gambet-";
   new 
GuiTextCtrl("Selection_Text"
    {
     
profile GuiBlueTextProfile;
     
10;
     
23;
     
height 50;
     
text "What number would you like to find the square root of?";
    }
   new 
GuiMLTextEditCtrl("Selection_TextBox"
    {
      
profile GuiBlueTextEditProfile;
      
280;
      
25;
      
width 50;
      
height 15;
      
maxchars 5;
    }
   new 
GuiButtonCtrl("Selection_SubmitButton"
    {
     
profile GuiBlueButtonProfile;
     
55;
     
67;
     
width 50;
     
height 15;
     
text "Submit";
    }
   new 
GuiButtonCtrl("Selection_CancelButton"
    {
     
profile GuiBlueButtonProfile;
     
225;
     
67;
     
width 50;
     
height 15;
     
text "Cancel";
    }
  }
}

function 
Selection_SubmitButton.onAction()
{
 
temp.value abs(Selection_TextBox.text);
 
 
NewtonRaphson(value);
 
selectionClose();
}

function 
Selection_CancelButton.onAction()
{
 
selectionClose();
}

function 
selectionClose()
{
 
Selection_Window.destroy();
 
this.active false;
}

function 
NewtonRaphson(value)
{
 
temp.x0 2//initial guess
 
temp.finished false;
 
temp.iteration temp.x1 NULL;
 
temp.output "";
 
 while(
finished == false && value 0)
  {
   
iteration++;     
   
x1 = (x0 - (x0^value) / (x0)); //derivation of Newton-Raphson formula
   
output output iteration this.tab this.tab this.tab this.tab this.tab 
            
this.tab this.tab this.tab x1 "\n";

   if (
x1 x0 && (x1 x0 0.000000005)) finished true;
   else if (
x0 x1 && (x0 x1 0.000000005)) finished true;
   else if (
x1 == x0finished true;
     
   
x0 x1;
  }
  
 if (
finished == truedisplayResults(outputvaluex1);
}

function 
displayResults(resultsnumanswer)
{
 new 
GuiWindowCtrl("Results_Window"
  {
   
profile GuiBlueWindowProfile;
   
= (screenwidth/2);
   
= (screenheight/2-100);
   
width 340;
   
height 190;
   
canclose canmaximize canminimize canresize "false";
   
text "Newton-Raphson Method Results";
   new 
GuiScrollCtrl("Results_Scroll"
    {
      
profile GuiBlueScrollProfile;
      
5;
      
22;
      
width 328;
      
height 150;
      
hScrollBar "alwaysOff";
      
vScrollBar "dynamic";
     new 
GuiMLTextCtrl("Results_ScrollText"
      {
        
useownprofile true;
        
profile "GuiBlueTextProfile";
        
8;
        
5;
        
height 40;
        
width 210;
      }
    }
   new 
GuiButtonCtrl("Results_CloseButton"
    {
     
profile GuiBlueButtonProfile;
     
5;
     
170;
     
width 328;
     
height 15;
     
text "Close";
    }
  }
 
Results_ScrollText.text "Iteration(s)" this.tab this.tab this.tab this.tab this.tab 
                           
"Approximation(s)\n" results 
                           
"\nThe square root of " num " is " answer;
}

function 
Results_CloseButton.onAction()
{
 
Results_Window.destroy();
 
this.active false;


Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 07:39 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.