Lost Password?


  #1 (permalink)  
Old 10-11-2008, 08:33 AM
Brandon W's Avatar   
Brandon W Brandon W is offline
Guru
 
Join Date: Sep 2008
Location: Australia
Age: 15
Posts: 1,588
Rep Power: 15
Brandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really nice
Send a message via MSN to Brandon W
Default Part I: Starting Trigonometry

G'day everyone. This is my latest tutorial and will be CRUCIAL for you to understand it for when I do part II. All this does is return results for an angle supplied using the formulas sin, cos and tan.

This will eventually turn into a complete trigonometry tutorial. I am at school now so it might be a few weeks away.

Starting out with Trigonometry in PHP

Aim: To get a user to enter a value and a script will determine the cosine, sine and tangent.
Hypothesis: A user will enter a value between 1-90 and it will be converted to radians so it can be calculated correctly. The script will then output an answer to the chosen radio button, cosine, sine and tangent. In other words save them from using a calculator or an image like;
[img] http://upload.wikimedia.org/wikipedi..._functions.svg [/img]


Procedure:
Let’s get started by creating the interface the user will be using. I won’t be explaining this since you should have a knowledge of HTML.
Here is the code for it:
PHP Code:
<?php
function formInput() {
?>

    <center>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <p> 
                Please input the number you wish to work with. Make sure it is in degrees! 
            </p>
            <p> 
                <input type="text" name="val" /> 
            </p>
            <p>
                <input type="radio" name="opt" value="sin" /> Sine (sin) <br />
                <input type="radio" name="opt" value="cos" /> Cosine (cos) <br />
                <input type="radio" name="opt" value="tan" /> Tangent (tan)
            </p>
            <p>
                <input type="submit" name="submit" value="Submit" />
            </p>
    </center>
<?php 
}
?>
Save that as form.php. Make sure it is spelt exactly the same.
Just a brief description on the PHP section of this. The first part we are creating a function called formInput, something you might find quite unusual is that the opening tag is at the top of the file in a separate set of PHP tags to the closing tag. The reason for this is to make sure PHP outputs the HTML correctly you have to use;
PHP Code:
Echo &#8220;HTML HERE”; 
This can be really annoying because of all the speech marks in the HTML, we could easily replace them with ‘ or even use the escape “\” but that means more programming. Here it is done quick and simple.

Now the second part is with the action of the form. In a quick summary all it is doing is telling that the action of the form is to use itself. So instead of using another file it will use its own.

Next is the hardest part, yet really easy, of the script. The functions which will do the magic for us all, remember these functions have no use till they are used. This will be shown in the section of this tutorial. Here is the script;
PHP Code:
<?php
function sinAns($val) {
    if (
$val 360 || $val <= 0) {
        echo 
"Allowed number are only between 1 and 360.";
    } else {
        
$ans sin(deg2rad($val));
        echo 
$ans;
    }
}

function 
cosAns($val) {
    if (
$val 360 || $val <= 0) {
        echo 
"Allowed number are only between 1 and 360.";
    } else {
        
$ans cos(deg2rad($val));
        echo 
$ans;
    }
}

function 
tanAns($val) {
    if (
$val 90 || $val <= 0) {
        echo 
"Allowed number are only between 1 and 90.";
    } else {
        
$ans tan(deg2rad($val));
        echo 
$ans;
    }
}
?>
The three functions here do the calculations for you, at the start of the last three functions it checks to make sure the value entered is under 90 and over than 1 it shows an error message. If it passes the error check it will do the calculations.
Now let’s get on with one of the functions that do the calculations.
PHP Code:
function sinAns($val) {
    if (
$val 90 || $val >= 0) {
        echo 
"$opt can only be used with a number between 1 and 90.";
    } else {
        echo 
sin(deg2rad($val));
    }

As above, the first line declares the function and opens the function code. The last line also closes off the functions code. The second line is yet another if statement. But this time it does something different. Since the $val has passed as the user has entered something, this time it will check if it between the correct numbers, 1 and 90. If it doesn’t it will show an error message. Next is the actual calculations, as long as the $val passes between the numbers 1 and 90, it will calculate. Sin, cos and tan are built in functions into PHP. The next part of the function then turns the value $val from degrees to radians so it can be calculated properly. It then echo’s the answer.

Now save this file as functions.php. Next is the file which processes all the functions and values.


Create a new PHP file and save it as index.php. This is the main part of the code. Here it is;
PHP Code:
<?php
require "functions.php";
require 
"form.php";

if(!isset(
$_POST['submit'])) {
    
formInput();
} else {
    switch (
$opt) {
        case 
"sin":
            
sinAns($val);
            break;
            
        case 
"cos":
            
cosAns($val);
            break;
            
        case 
"tan":
            
tanAns($val);
            break;
    }
    echo 
"<br /><a href='index.php'>Another calculation</a>";
}
?>
OK let’s start with explaining the code. The first few lines require the two files that we have typed earlier so that the functions can be called in this file.
The rest of the code is an if statement. If the user has not clicked on the submit button, it will do the function “formInput()”. As you can see we typed this in “form.php” a while ago which is the interface for the user. If they have clicked on it, it will do a switch statement. The switch statement is based on the value of $opt. Each case is a possibility of what it can be. So let’s say that the user chose “sin” it will do the function sinAns using $val as the value to be used. The break statement then closes that case.
The last echo statement gives the user to do another calculation.

Conclusion: Now you have completely working sin, cos and tan calculator

+rep if you found this helpful.

Last edited by Brandon W; 10-11-2008 at 06:09 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 10-11-2008, 09:01 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,271
Last Blog:
wxWidgets is NOT code ...
Rep Power: 36
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default Re: Part I: Starting Trigonometry

Some comments/questions:

It's important for people to be aware that EVERY programming language uses radian based trig functions (because of calculus and its relation to the sciences). As a result, to accommodate degrees, it is necessary to use a conversion like the one Brandon used.

That said, why did you limit the angle to 1-90? Sin and Cos are defined for all angles, and it is easy to test for the values where Tan is undefined.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-11-2008, 09:03 AM
Brandon W's Avatar   
Brandon W Brandon W is offline
Guru
 
Join Date: Sep 2008
Location: Australia
Age: 15
Posts: 1,588
Rep Power: 15
Brandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really nice
Send a message via MSN to Brandon W
Default Re: Part I: Starting Trigonometry

So Sin and Cos can be from 0 - 360 degrees?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-11-2008, 11:31 AM
chili5's Avatar   
chili5 chili5 is online now
Code Warrior
 
Join Date: Mar 2008
Age: 15
Posts: 3,591
Rep Power: 31
chili5 is a name known to allchili5 is a name known to allchili5 is a name known to allchili5 is a name known to allchili5 is a name known to allchili5 is a name known to all
Default Re: Part I: Starting Trigonometry

Wow, that's really well written.

Yeah sin, cos can be used with any angle.
__________________
Emo Philips - "My computer beat me at checkers, but I sure beat it at kickboxing."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-11-2008, 11:51 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,271
Last Blog:
wxWidgets is NOT code ...
Rep Power: 36
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default Re: Part I: Starting Trigonometry

sin and cos are defined on ALL real numbers.
tan is defined for all x where x is not in the form (2n+1)pi/2, n an integer (pi/2, 3pi/2, 5pi/2, etc)
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 10-11-2008, 11:54 AM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,203
Last Blog:
Ext JS or Ext GWT
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: Part I: Starting Trigonometry

Nice work Brandon W! +rep (if you want contest points please PM me).
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 10-11-2008, 06:07 PM
Brandon W's Avatar   
Brandon W Brandon W is offline
Guru
 
Join Date: Sep 2008
Location: Australia
Age: 15
Posts: 1,588
Rep Power: 15
Brandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really nice
Send a message via MSN to Brandon W
Default Re: Part I: Starting Trigonometry

No thanks +rep will do. I want to be Guru

Thanks for that Winged, I will change the code now. I was only working with Right-Angled Triangles.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 10-12-2008, 11:58 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,271
Last Blog:
wxWidgets is NOT code ...
Rep Power: 36
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default Re: Part I: Starting Trigonometry

You'll get the circle-based definition of the trig functions soon
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 10-12-2008, 12:03 PM
chili5's Avatar   
chili5 chili5 is online now
Code Warrior
 
Join Date: Mar 2008
Age: 15
Posts: 3,591
Rep Power: 31
chili5 is a name known to allchili5 is a name known to allchili5 is a name known to allchili5 is a name known to allchili5 is a name known to allchili5 is a name known to all
Default Re: Part I: Starting Trigonometry

Trig with Circles/Waves

I would +rep you for this, but I can't atm. I did for something else, and have to wait.
__________________
Emo Philips - "My computer beat me at checkers, but I sure beat it at kickboxing."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 10-12-2008, 05:28 PM
Brandon W's Avatar   
Brandon W Brandon W is offline
Guru
 
Join Date: Sep 2008
Location: Australia
Age: 15
Posts: 1,588
Rep Power: 15
Brandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really nice
Send a message via MSN to Brandon W
Default Re: Part I: Starting Trigonometry

Doesn't bother me mate it's all good

Hmmm, that should be this term of Maths. We are doing Algebra 3 and Deductive Geometry Sounds interesting, I can't wait! We have started Algebra 3 just not the Geometry section.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump