Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #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
  #2  
Old 04-10-2009, 09:01 AM
Schetti Schetti is offline
SC2 Player
Schetti's Avatar
Join Date: Nov 2008
Location: Austria
Posts: 269
Schetti is on a distinguished road
Can I use that in my next maths lesson?

PS: Nice
__________________
“Peace cannot be kept by force. It can only be achieved by understanding.” – Albert Einstein
Reply With Quote
  #3  
Old 04-10-2009, 07:11 PM
devilsknite1 devilsknite1 is offline
C:
devilsknite1's Avatar
Join Date: Jul 2006
Location: Florida, USA
Posts: 269
devilsknite1 has a spectacular aura about
Send a message via AIM to devilsknite1 Send a message via MSN to devilsknite1 Send a message via Yahoo to devilsknite1
Nice. Wouldn't it be a bit easier to just use TAB rather than this.tab? :o
Reply With Quote
  #4  
Old 04-10-2009, 07:23 PM
napo_p2p napo_p2p is offline
oh snaps
napo_p2p's Avatar
Join Date: Sep 2003
Location: Pismo Beach, California
Posts: 2,118
napo_p2p has a spectacular aura aboutnapo_p2p has a spectacular aura about
Send a message via AIM to napo_p2p Send a message via MSN to napo_p2p
Quote:
Originally Posted by devilsknite1 View Post
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 
__________________
Scito hoc super omnia.
Haec vita est tua una sola.
Dum vita superest, utere maxime quoque puncto, momento, et hora quae habes.
Tempus neminem non manet.
Noli manere tempus.
Carpe Diem

Seize the Day.
Reply With Quote
  #5  
Old 04-10-2009, 07:24 PM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Quote:
Originally Posted by devilsknite1 View Post
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 View Post
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.
Reply With Quote
  #6  
Old 04-10-2009, 07:31 PM
napo_p2p napo_p2p is offline
oh snaps
napo_p2p's Avatar
Join Date: Sep 2003
Location: Pismo Beach, California
Posts: 2,118
napo_p2p has a spectacular aura aboutnapo_p2p has a spectacular aura about
Send a message via AIM to napo_p2p Send a message via MSN to napo_p2p
Quote:
Originally Posted by Gambet View Post
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.
__________________
Scito hoc super omnia.
Haec vita est tua una sola.
Dum vita superest, utere maxime quoque puncto, momento, et hora quae habes.
Tempus neminem non manet.
Noli manere tempus.
Carpe Diem

Seize the Day.
Reply With Quote
  #7  
Old 04-12-2009, 05:33 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Nice, more random **** that most people don't understand and is highly under-appreciated.

Ah, woe, I have become a cynic.
Reply With Quote
  #8  
Old 04-12-2009, 06:33 AM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Quote:
Originally Posted by WhiteDragon View Post
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.
Reply With Quote
  #9  
Old 04-13-2009, 02:09 AM
devilsknite1 devilsknite1 is offline
C:
devilsknite1's Avatar
Join Date: Jul 2006
Location: Florida, USA
Posts: 269
devilsknite1 has a spectacular aura about
Send a message via AIM to devilsknite1 Send a message via MSN to devilsknite1 Send a message via Yahoo to devilsknite1
Ah, TAB doesn't work with that? Weird; I'll mess with it a bit.

Either way, again, nice script. Very useful
Reply With Quote
Reply


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 09:42 PM.


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