Forum Moderators: coopster

Message Too Old, No Replies

php equation

solving for a variable in the equation

         

daneosporin

5:55 pm on Oct 15, 2005 (gmt 0)

10+ Year Member



I'm trying to create a function that solves for a variable in the equation, but I am not sure how to do this. My actual equation cannot be simplified to solve for the variable, but basically what I haved tried is this:

function example($A,$B)
{
$Q;
$A=$Q-$B;
}

I know this is wrong, but what do I need to change to solve for $Q?

- Danny

madmac

6:32 pm on Oct 15, 2005 (gmt 0)

10+ Year Member



Any equation that you want to solve for a variable, has to able to be simplified enough to be able to solve for that variable, otherwise it is unsolvable for that variable.

This might require numerous steps though.

Think through how you would logically solve it by hand/calculator. Include all the steps. Write this into PHP Code.

Your example is really too simple to be of any help:

A = Q - B; solve for Q
A + B = Q

/* Q = A + B */
function example($A, $B) {
return $A + $B;
}

Perhaps if you gave us something closer to what you're actually trying to do or your actual equation, we might be able to better help.

daneosporin

7:30 pm on Oct 15, 2005 (gmt 0)

10+ Year Member



Hi Madmac,
More specifically I need to make a php script to calculate "Yield to Maturity" of a bond.
The equation is:

$P=$C * (1-1/pow((1+$I),$N))/$I+$F*(1/pow((1+$I),$N))

where:
$P is price
$C is coupon payment
$I is yield to maturity
$N is number of payments
$F is Face Value

The equation cannot be simplified to equal $I because it has $I taken to different powers.

madmac

10:01 pm on Oct 15, 2005 (gmt 0)

10+ Year Member



You can still logically work through the problem as if you were solving it by hand (and not using a financial calculator). Translate that process to code.

Here is a decent page:
[moneychimp.com...]

And here is one that even spells out in plain english steps how to calculate it by hand (which you can turn into code):
[ehow.com...]

daneosporin

7:05 pm on Oct 17, 2005 (gmt 0)

10+ Year Member



Madmac,
Thanks for the assistance. The equation those pages used seemed to be too simple to me. I though for sure they were forgetting to compound the interest. I tried the equation to get YTM and plugged the info into my equation to get current price and it got a price within 1% of the current price I used in the YTM equation. This is close enough for me. Maybe when the page starts getting good traffic I will review it, but no point in working too hard on it right now.

Anyways here is the code I used for anyone who is interested:


<?php
$P = $_GET['P'];
$F = $_GET['F'];
$Coupon = $_GET['Coupon'];
$T = $_GET['T'];

$C = $Coupon/100;
?>

<table><tr><td valign = "top">

<form name="bondyield">
<table>
<tr><td align="right">current price &nbsp </td><td><input type="text" size="10" name="P" value="<?php echo $P?>"></td></tr>
<tr><td align="right">face value &nbsp </td><td><input type="text" size="10" name="F" value="<?php echo $F?>"></td></tr>
<tr><td align="right">coupon rate &nbsp </td><td><input type="text" size="10" name="Coupon" value="<?php echo $Coupon?>"></td></tr>
<tr><td align="right">years to maturity &nbsp </td><td><input type="text" size="10" name="T" value="<?php echo $T?>"></td></tr>
<tr><td></td><td><input type="submit" value="get quote"></td></tr>
</table>
</form name="bondyield">
<p>

<?
if ($P>0 and $F>0 and $T>0 and $C>0) {

$CY = $F*$C/$P;
$YTM = (($C*$F)+($F-$P)/$T)/(($F+$P)/2);

echo "Current Yield: " . $CY . "<br>";
echo "YTM" . $YTM;

} else {
echo "Please enter all four variables";
}

?>