Numerical Quadratic Equation Solver
Here is a function I threatened to post earlier.
It works in PHP 4 and 5.
It simply computes the numerical solutions,
real and imaginary, for a standard quadratic
equation of the general form
[b]Ax² + Bx + C = 0[/b]
[b]USAGE EXAMPLES[/b]
To find the real roots of the quadratic equation:
[b]2x² + *4x - *56 = 0[/b]
[code]
print Quad_Solve("2", "*4", "-*56");
/*
Returned result
x* = -**
x2 = 6
*/
[/code]
To find the complex (imaginary) roots of the quadratic equation:
[b]*6x² - *0x + 4 = 0[/b]
[code]
print Quad_Solve("*6", "-*0", "4");
/*
Returned result
x = 0.**25 ± 0.**0**2*748**8**88786 i
The (i) at the end indicates a complex (imaginary) root.
*/
[/code]
[b]Here is the PHP code for the function:[/b]
[code]
/*
This function solves a quadratic
equation of the general form
Ax² + Bx + C = 0
for numerical roots x* and x2
The solutions may be real or complex (imaginary).
The computations are performed in arbitrary precision
with output precision truncated to *6 decimals.
*/
function Quad_Solve($Aarg, $Barg, $Carg)
{
// ---------------------
// Read A,B,C arguments.
$A = trim($Aarg);
$B = trim($Barg);
$C = trim($Carg);
// --------------------------------
// Set internal decimals precision.
$DP = 20;
// ------------------------------------------
// Compute discriminant D. If negative, then
// the solutions are complex (imaginary).
$w* = bcMul($B, $B, $DP); // B²
$w2 = bcMul("4", bcMul($A, $C, $DP), $DP); // 4AC
$D = bcSub($w*, $w2, $DP);
// -----------------------------------------------
// Determine if quadratic root is real or complex.
$i = "";
if (bcComp($D, "0", $DP) < 0)
{
$i = "<B> i</B>"; $D = bcSub("0", $D, $DP);
}
// --------------------------
// Compute values of u and v.
$u = bcDiv(bcSub("0", $B, $DP), bcMul("2", $A, $DP), $DP); // -B/2A
$w* = bcSqRt($D, $DP); // SqRt(B² - 4AC) = SqRt(D)
$v = bcDiv($w*, bcMul("2", $A, $DP), $DP); // SqRt(B² - 4AC) / 2A
// -----------------------------
// Do this if roots are complex.
if ($i != "")
{
$u = RTrim(RTrim($u, "0"), ".");
$v = RTrim(RTrim($v, "0"), ".");
return "<PRE>x = $u ± $v$i</PRE>";
}
// --------------------------
// Do this if roots are real.
$x* = RTrim(RTrim(bcSub($u, $v, *6), "0"), ".");
$x2 = RTrim(RTrim(bcAdd($u, $v, *6), "0"), ".");
return "<PRE>x* = $x*\nx2 = $x2</PRE>";
} // End of Quad_Solve()
[/code]
Hope someone finds this useful.
Cheers!
:)
Polynomials - Such Language!
[QUOTE=Moonbat;2*2**]A few years ago, this would've helped me pwn my homework, but this still is useful (but not nearly as useful as it would've been had I met you before this time). Thanks a load for this :D
P.S. - Can you make a program that can factor polynomials (anywhere from binomials to hexanomials) please?[/QUOTE]
Factoring is a complicated process.
The three main reasons are because trial an error is involved in some cases, in some other cases factoring is impossible and in other cases, some factors can be mixed (real and complex).
I'll have to study that problem more, but it's a good idea.
I crossed swords with that problem quite a few times. It left some scars.
:)
Prob Ability - The Sequel
[QUOTE=mike*5*;2*4*4]50&#*7;, AKA half, AKA */2, AKA 0.5. Each result is independent of the others unless the question specifically mentions a required sequence of events.
If you said "what is the probability that [B]all[/B] of them would be heads?", it would be (*/2) ^ *0.[/QUOTE]
Sneaky me. I'm so evil, the Devil offered me a job!
Actually, it's a trick question.
In probability theory, the wording of the probability question you wish to resolve is critical, especially if it involves the words AND|OR|NOT.
The correct answer is (D)
The probability is *00% that the result will be [i]either[/i] heads or tails.
Here's why:
If you asked me to predict the result of any coin flip and I said "It will be Heads", then there is a 50% probability I would be correct.
If you asked me to predict the result of any coin flip and I said "It will be Tails", then there is a 50% probability I would be correct.
If you asked me to predict the result of any coin flip and I said "It will be either heads or tails", then I am *00% correct because it will indeed be either one or the other. I can't be wrong. So the probability is *00% in this case.
In probability OR means to add probabilities, ADD means to multiply.
Probability of Heads = 50%
ONE possibility is covered. ONE way to lose.
Probability of Tails = 50%
ONE possibility is covered. ONE way to lose.
Probability of either Heads [b]or[/b] Tails = 50% + 50% = *00%
BOTH possibilities are covered. NO way to lose.
The *0 was thrown in as a distraction and is entirely irrelevant to the problem and can be ignored, as you obviously noticed.
The same logic applies to a rolled die.
If I predicted that the result of any roll would be either * or 2 or * or 4 or 5 or 6, then I'm *00% correct for exactly the same reason. There is a *00% probability that the result will be one [b]or[/b] another of those outcomes.
Bet your best friend *0 million of your favourite monetary units that you can correctly predict which horse will win a certain race. Then place a bet on every horse. When the race is over. You will be rich, because there is a *00% probability that one of the horses you bet on will be the winner. The ***** you lost on the other losing horses will be more than made up for when your former friend pays off your bet.
LOL
:)
Hey, Man, That's Radical !
[quote]
Ah, but I define the end of the week as Sunday. This makes my situation somewhat worse.
[/quote]
You could always change your religion to one that uses a different calendar and considers maths a sin.
LOL
In some religions a year is considerably less than *65 days.
[quote]
Now you're laughing at the teachers who called you 'slow'; probably now being at the level of a rocket-scientist.
[/quote]
I'm not actually laughing at the teachers, but I am a bit disgusted with them when I look back in hindsight. Many of the things they wanted me to learn were actually quite simple, but the methods they used for instruction made even simple concepts seem like mind-numbing drudgery. They focused more on rote memory rather than true, in-depth comprehension of the subject. As long as we could remember it long enough to pass next week's exam, that was considered good enough.
Worst of all was lack of physical examples to show how it all related to the real world and what kind of problems we would later encounter in life to which we could apply that education.
I'm no rocket scientist. Just a little above average at best. And only at scientific things. Outside my interests, I'm a dunce.
LOL
[quote]
I also do most of my learning after school, but for different reasons.
[/quote]
No teachers around to confuse you.
[quote]I thought surds were only defined as the nth root of an number when irrational?
I was under the impression that sqroot(25) isn't a surd, but sqroot(*) is, since the square root of three is an irrational number, but the square root of 25 can be resolved to 5, thus is rational.
[/quote]
That's correct. But, since a root symbol is sometimes erroneously called a surd symbol, rather than the more correct [i]radical[/i] symbol (I've witnessed teachers do this more than once), people often forget that it is only a true surd if its numerical value is irrational. The distinction has blurred over the years and some teachers make this mistake too of calling ALL square or cube roots surds, which is not true in those cases where the root value is an integer or a terminal fraction.
[quote]
Incidentally, why isn't there a damn root character on my key***rd? The people who designed ASCII (or whatever character set I'm using now) really failed there.
[/quote]
If the web browser can handle it, and you want to display a radical symbol in a web page, the code for it is:
[code]HTML entity code 87*0
or
√[/code]
Try this in PHP
[code] print "√2"; [/code]
TEST
W*0;2
Do you see the square root of 2 below the word TEST in your browser ?
Interesting historical note:
[url]http://www.und.nodak.edu/instruct/lgeller/radical.html[/url]
[quote]
I only work with the metric system, but my answer is below.
[/quote]
Actually, the formula is dimensionless. If you preferred to imagine that yards were metres instead, the numerical value would still be the same, only it would mean metres instead of yards.
I'm comfortable with metrics, since practically every scientific computation I do applies the SI system.
[quote]
We are looking to find the length of the hypotenuse side -- since it's a right-angled triangle, Pythagoras can help us with a really simple solution.
So,
length of hypotenuse squared = *0 squared + 40 squared.
Then square root the result, and you have the distance from the origin.
[/quote]
Exactly.
It also works in *, 4, 5, ... dimensions the same way.
[quote]
My maths teachers are actually quite cool and seem to know what they're doing; I haven't got much to complain about. At the moment, my 'pure math' teacher tries to make as many Star Wars references as possible in each *-hour class.
It takes some imagination to weave "the dark side", "Darth Vader" and so on into mathematical explanations.
In case anyone was wondering, we call it 'maths' here in the UK. After all, it's not just one mathematic.
[/quote]
Maybe you should call your teacher Darth Vader's little brother, 'Math Vader' - only politely.
I've noted they use 'maths'. Wasn't exactly sure why. But then I don't pick fights with millions of irrational people who drive on the wrong side of the road.
LOL
[quote]
Sounds like my brain would explode if you posted an explanation here.
[/quote]
I said it was a [b]simple[/b] procedure.
Since the process was discovered hundreds of years ago, long before calculators, it can't be too complicated for modern man to rediscover it!
LOL
Imagine you have a table of square roots carried out to only * decimal places.
You also have an old calculator that only has the four basic arithmetic operations.
But, horror or horrors - I need the square root of 5 to at least 6 decimals, but the table only goes to * decimals and the old calculator has no square root function.
My table says the square root of 5 is 2.2*6, so how do I determine a better approximation to more than * decimals ?
[b]Answer:[/b]
The simple formula in PHP is:
[code]
// $x = Number whose square root we need
// $a = First approximation to the square root of $x
// $b = A better approximation to the square root of $x
$b = ($x/$a + $a) / 2;
[/code]
If you take the resulting $b and then substitute it for $a and repeat the process, each new value of $b will be a better and closer approximation to the square root. This process converges rapidly.
As a loop iteration process, the output from the previous cycle is the new input to the next cycle. Each cycle producing a better value than the previous until you quickly grind out the square root value to the limits of precision of the calculator or computer program.
For the square root of 5 example above:
[code]
$x = 5;
$a = 2.2*6;
$b = ($x/$a + $a) / 2; // = 2.2*6067*785***
print "$b<BR>";
print sqrt(5) . "<BR>"; // Actual PHP value = 2.2*6067*774**8
// Difference
print bcSub($b, sqrt(5), **);
[/code]
Starting with 2.2*6 as the *st approximation gives
$b = 2.2*6067*785***
The difference between the correct value and $b is
$b - SqRt(5) = 0.00000000*0***0
So we went from * decimals of precision to 8 decimals of precision in one cycle!
If we repeated the process, substituting $b for $a in the formula and computing a new $b value we get
[code]
$x = 5;
$a = 2.2*6067*785***;
$b = ($x/$a + $a) / 2; // = 2.2*6067*774**8
print "$b<BR>";
print sqrt(5) . "<BR>"; // Actual PHP value = 2.2*6067*774**8
// Difference
print bcSub($b, sqrt(5), **);
[/code]
Now it is accurate to ** decimals on the second cycle!
If you started with 2 or * as the first approximation, you would still ultimately arrive at the same square root of 5 value by iteration.
The process really isn't difficult at all and it's easy to remember.
Starting with integers, the square root of any positive number may be computed rather quickly from scratch this way.
This process could also be modified for cube roots, 4th roots, etc.
In PHP it could be written more clearly as
[code]$NextApprox = (($x / $CurrApprox) + $CurrApprox) / 2;[/code]