Solver Completing the Square to Get a Quadratic into Vertex Form
Algebra
->
Algebra
->
Polynomials-and-rational-expressions
-> Solver Completing the Square to Get a Quadratic into Vertex Form
Log On
Ad:
Algebra Solved!™
: algebra software solves algebra homework problems with step-by-step help!
Ad:
Algebrator™
solves your algebra problems and provides step-by-step explanations!
Algebra: Polynomials, rational expressions and equations
Solvers
Lessons
Problems, free tutors
Quiz
In Depth
Source code of 'Completing the Square to Get a Quadratic into Vertex Form'
This Solver (Completing the Square to Get a Quadratic into Vertex Form)
was created by by
jim_thompson5910(13794)
:
View Source
,
Show
,
Put on YOUR site
About jim_thompson5910
:
==section input Enter the quadratic in the form of {{{ax^2+bx+c}}} a=*[input a=1] b=*[input b=5] c=*[input c=6]. If you have any coefficients in decimal form, convert them into fractions. For example if you have the equation {{{y=.2x^2+.5x+.1}}} convert it into {{{y=(1/5)x^2+(1/2)x+1/10}}} ==section solution perl sub replace { my $exp=$_[0]; $exp=~s/\//z/g; $exp=~s/\W+//g; $exp=~s/z/\//g; return $exp; } sub gcf { my ($x, $y) = @_; ($x, $y) = ($y, $x % $y) while $y; return $x; } sub lcm { return($_[0] * $_[1] / gcf($_[0], $_[1])); } sub multigcf { my $x = shift; $x = gcf($x, shift) while @_; return $x; } sub multilcm { my $x = shift; $x = lcm($x, shift) while @_; return $x; } sub mult{ my (@arr1,@arr2); @arr1=($_[0],$_[1]); @arr2=($_[2],$_[3]); if(@_==2) {@arr1=split(/\//,$_[0]); push(@arr1,1) if @arr1==1; @arr2=split(/\//,$_[1]); push(@arr2,1) if @arr2==1; } return "".($arr1[0]*$arr2[0])."/".($arr1[1]*$arr2[1]).""; } sub add{ my @arr1=($_[0],$_[1]); my @arr2=($_[2],$_[3]); if(@_==2) {@arr1=split(/\//,$_[0]); push(@arr1,1) if @arr1==1; @arr2=split(/\//,$_[1]); push(@arr2,1) if @arr2==1; } if(($arr1[1]!=$arr2[1])&&(($arr1[1]!=0)&&($arr2[1]!=0))) { my $gcf=gcf($arr1[0],$arr1[1]); my $lcm=($arr1[1]*$arr2[1])/$gcf; $exported_lcm=$lcm; $exported_gcf=$gcf; $arr1[0]=($lcm/$arr1[1])*$arr1[0]; $arr2[0]=($lcm/$arr2[1])*$arr2[0]; $arr1[1]=($lcm/$arr1[1])*$arr1[1]; $arr2[1]=($lcm/$arr2[1])*$arr2[1]; } $arr1[0]+=$arr2[0]; return "$arr1[0]/$arr1[1]"; } sub subtract{ my @arr1=($_[0],$_[1]); my @arr2=($_[2],$_[3]); if(@_==2) {@arr1=split(/\//,$_[0]); push(@arr1,1) if @arr1==1; @arr2=split(/\//,$_[1]); push(@arr2,1) if @arr2==1; } my $return=add($arr1[0],$arr1[1],-$arr2[0],$arr2[1]); return $return; } sub reduce{ my @arr1=@_; my @original_arr1=@arr1; if(@_==1) {@arr1=split(/\//,$_[0]); push(@arr1,1) if @arr1==1; } if($arr1[0]==0) {return $arr1[0];} if($arr1[1]!=0) { my $gcf=gcf($arr1[0],$arr1[1]); $arr1[0]/=$gcf; $arr1[1]/=$gcf; } return "$arr1[0]/$arr1[1]"; } sub invert{ my @arr1=@_; if(@_==1) {@arr1=split(/\//,$_[0]); push(@arr1,1) if @arr1==1; } return "$arr1[1]/$arr1[0]"; } sub divide{ my @arr1=($_[0],$_[1]); my @arr2=($_[2],$_[3]); if(@_==2) {@arr1=split(/\//,$_[0]); push(@arr1,1) if @arr1==1; @arr2=split(/\//,$_[1]); push(@arr2,1) if @arr2==1; } my $return=mult($arr1[0],$arr1[1],$arr2[1],$arr2[0]); return $return; } my $graph_dimensions="500,500,-10,10,-10,10"; if($a==0) {print " <font size=\"4\" color=\"red\"><b>Error:</b></font> the leading coefficient \"a\" cannot be equal to zero. "; return; } $a=~s/\)|\(//g; $b=~s/\)|\(//g; $c=~s/\)|\(//g; my (@a,@b,@c); @a=split(/\//,$a); @b=split(/\//,$b); @c=split(/\//,$c); $a[1]=1 if @a==1; $b[1]=1 if @b==1; $c[1]=1 if @c==1; #my ($displayed_a,$displayed_b,$displayed_c)=($a,$b,$c); #$displayed_a="($displayed_a)" if $displayed_a=~m/\//; #$displayed_b="($displayed_b)" if $displayed_b=~m/\//; $a="($a)" if $a=~m/\//; $b="($b)" if $b=~m/\//; my ($inner_expression,$factored_b,$factored_c,$display); #$original_equation="$a x^2+$b x+$c"; $display="y=$a x^2+$b x+$c"; $display=~s/\+\-/\-/g; my $original_equation=$display; $original_equation=~s/y=//; print " {{{$display}}} Start with the given equation "; my (@factored_b,@factored_c,$abs); $factored_b=divide(@b,@a); @factored_b=split(/\//,$factored_b); $factored_b=reduce(@factored_b); @factored_b=split(/\//,$factored_b); $factored_b=~s/\/1$//; #my @factored_c=divide(@c,@a); #$factored_c=reduce(@factored_c); #@factored_c=split(/\//,$factored_c); $inner_expression.="x^2+"; $inner_expression.="(".join("/",@factored_b).")x+" if $factored_b[1]!=1; $inner_expression.="$factored_b[0]x+" if $factored_b[1]==1; #$inner_expression.="".join("/",@factored_c)."+" if $factored_c[1]!=1; #$inner_expression.="$factored_c[0]+" if $factored_c[1]==1; $inner_expression=~s/\+$//; my $negative_c="-$c"; $negative_c=~s/\-\-/\+/g; if(length($c)!=0) {$abs=$c; $abs=~s/^\-//; $display="y$negative_c=$a x^2+$b x"; $display=~s/\+\-/\-/g; print " {{{$display}}} ", ($c=~m/\-/) ? "Add {{{$abs}}} to" : " Subtract {{{$abs}}} from"; print " both sides ";} #if($a!=1) #{ $display="y$negative_c=$a($inner_expression)"; $display=~s/\+\-/\-/g; print " {{{$display}}} Factor out the leading coefficient {{{$a}}} "; #} my $half_b=divide(@factored_b,2,1); my @half_b=split(/\//,$half_b); $half_b=reduce(@half_b); @half_b=reduce($half_b); $half_b=~s/\/1$//; my $square_b=mult(@half_b,@half_b); my @square_b=split(/\//,$square_b); $square_b=reduce(@square_b); @square_b=reduce($square_b); $square_b=~s/\/1$//; my $before_factor=$inner_expression; $before_factor.="+$square_b"; $inner_expression.="+$square_b-$square_b"; $before_factor=~s/\+\-/\-/g; my $after_factor="x+$half_b"; $after_factor=~s/\+\-/\-/g; print " Take half of the x coefficient {{{$factored_b}}} to get {{{$half_b}}} (ie {{{(1/2)($factored_b)=$half_b}}}). Now square {{{$half_b}}} to get {{{$square_b}}} (ie {{{($half_b)^2=($half_b)($half_b)=$square_b}}}) "; $display="y$negative_c=$a($inner_expression)"; $display=~s/\+\-/\-/g; print " {{{$display}}} Now add and subtract this value inside the parenthesis. Doing both the addition and subtraction of {{{$square_b}}} does not change the equation "; $display="y$negative_c=$a(($after_factor)^2-$square_b)"; $display=~s/\+\-/\-/g; print " {{{$display}}} Now factor {{{$before_factor}}} to get {{{($after_factor)^2}}} "; $display="y$negative_c=$a($after_factor)^2-$a($square_b)"; $display=~s/\+\-/\-/g; $display=~s/\-\-/\+/g; @square_b=split(/\//,$square_b); $square_b[1]=1 if @square_b==1; my $new_c=mult(@square_b,@a); my @new_c=split(/\//,$new_c); $new_c=reduce(@new_c); $new_c=~s/\/1$//; print " {{{$display}}} Distribute "; $display="y$negative_c=$a($after_factor)^2-$new_c"; $display=~s/\+\-/\-/g; $display=~s/\-\-/\+/g; print " {{{$display}}} Multiply "; ### Optional $abs=$negative_c; $abs=~s/\-//; $display="y=$a($after_factor)^2-$new_c+$c"; $display=~s/\+\-/\-/g; $display=~s/\-\-/\+/g; my $final_c=subtract(@c,@new_c); my @final_c=split(/\//,$final_c); $final_c=reduce(@final_c); $final_c=~s/\/1$//; print " {{{$display}}} Now add {{{$abs}}} to both sides to isolate y "; $display="y=$a($after_factor)^2+$final_c"; $display=~s/\+\-/\-/g; $display=~s/\-\-/\+/g; print " {{{$display}}} Combine like terms "; my $final_equation=$display; $final_equation=~s/y=//; $a=~s/\)|\(//g; my $h="-$half_b"; $h=~s/\-\-//; my $k=$final_c; print " Now the quadratic is in vertex form {{{y=a(x-h)^2+k}}} where {{{a=$a}}}, {{{h=$h}}}, and {{{k=$k}}}. Remember (h,k) is the vertex and \"a\" is the stretch/compression factor. "; $original_equation=~s/\s//g; print " Check: Notice if we graph the original equation {{{y=$original_equation}}} we get: {{{graph($graph_dimensions,$original_equation)}}} Graph of {{{y=$original_equation}}}. Notice how the vertex is ({{{$h}}},{{{$k}}}). Notice if we graph the final equation {{{y=$final_equation}}} we get: {{{graph($graph_dimensions,$final_equation)}}} Graph of {{{y=$final_equation}}}. Notice how the vertex is also ({{{$h}}},{{{$k}}}). So if these two equations were graphed on the same coordinate plane, one would overlap another perfectly. So this visually verifies our answer. "; ==section output ans ==section check a b c h k