Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Newton-Raphson Method (Finding Square Roots) (https://forums.graalonline.com/forums/showthread.php?t=85080)

Gambet 04-10-2009 08:54 AM

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;




Schetti 04-10-2009 09:01 AM

Can I use that in my next maths lesson?

PS: Nice

devilsknite1 04-10-2009 07:11 PM

Nice. Wouldn't it be a bit easier to just use TAB rather than this.tab? :o

napo_p2p 04-10-2009 07:23 PM

Quote:

Originally Posted by devilsknite1 (Post 1482774)
Nice. Wouldn't it be a bit easier to just use TAB rather than this.tab? :o

Yes, but the main focus here is on the method. Although it's a good thing to point out that TAB is out there.

I guess it's also a good time to issue a disclaimer of sorts. The N-R method is for educational purposes only. Practically, use:
PHP Code:

value 0.5 


Gambet 04-10-2009 07:24 PM

Quote:

Originally Posted by devilsknite1 (Post 1482774)
Nice. Wouldn't it be a bit easier to just use TAB rather than this.tab? :o


Yeah, if it actually worked with a MLTextCtrl. I'm not sure if you can add columns to MLTextCtrl's, so TAB wouldn't work (which means '\t' wouldn't work either). At least, it didn't work for me when I tried it.


Quote:

Originally Posted by napo_p2p (Post 1482778)
I guess it's also a good time to issue a disclaimer of sorts. The N-R method is for educational purposes only. Practically, use:
PHP Code:

value 0.5 



I mentioned something like that in the original post. :) Personally, I just find it pretty neat.

napo_p2p 04-10-2009 07:31 PM

Quote:

Originally Posted by Gambet (Post 1482779)
I mentioned something like that in the original post. :) Personally, I just find it pretty neat.

Certainly, you did. I just wanted to make that that when poor little Billy is browsing the scripting examples looking for a way to do square roots, he finds it. I'm always looking out for poor little Billy.

WhiteDragon 04-12-2009 05:33 AM

Nice, more random **** that most people don't understand and is highly under-appreciated.

Ah, woe, I have become a cynic.

Gambet 04-12-2009 06:33 AM

Quote:

Originally Posted by WhiteDragon (Post 1483161)
Nice, more random **** that most people don't understand and is highly under-appreciated.

Ah, woe, I have become a cynic.


Not sure what you mean by this but the N-R method is pretty important when it comes to finding roots of non-linear equations. As mentioned before, despite its implications, I just find it pretty neat to understand how to find roots on your own.

devilsknite1 04-13-2009 02:09 AM

Ah, TAB doesn't work with that? Weird; I'll mess with it a bit.

Either way, again, nice script. Very useful :)


All times are GMT +2. The time now is 06:22 AM.

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