Solver Solve the System of Equations by Graphing
Algebra
->
Algebra
->
Linear-equations
-> Solver Solve the System of Equations by Graphing
Log On
Ad:
Algebrator™
solves your algebra problems and provides step-by-step explanations!
Ad:
Algebra Solved!™
: algebra software solves algebra homework problems with step-by-step help!
Algebra: Linear Equations, Graphs, Slope
Solvers
Lessons
Problems, free tutors
Quiz
In Depth
Source code of 'Solve the System of Equations by Graphing'
This Solver (Solve the System of Equations by Graphing)
was created by by
jim_thompson5910(13794)
:
View Source
,
Show
,
Put on YOUR site
About jim_thompson5910
:
==section input Enter the two equations in standard form {{{Ax+By=C}}} where A, B, and C are whole numbers *[input a=1]x+*[input b=2]y=*[input c=3] *[input d=4]x+*[input e=5]y=*[input f=6] ==section solution perl sub replace { my $exp=$_[0]; $exp=~s/\//z/g; $exp=~s/\-/w/g; $exp=~s/\W+//g; $exp=~s/z/\//g; $exp=~s/w/\-/g; return $exp; } $a=replace($a); $b=replace($b); $c=replace($c); $d=replace($d); $e=replace($e); $f=replace($f); 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; } sub clean_up { my $exp=$_[0]; $exp=~s/\+\-/\-/g; $exp=~s/\-\-/\+/g; $exp=~s/^\+//; $exp=~s/\+$//; $exp=~s/\s//g; $exp=~s/(\D)1([a-z])/$1$2/g; return $exp; } sub generate_fraction { my $exp=$_[0]; my @array=split(/\//,$exp); $array[1]=1 if @array==1; return @array; } sub enclose { my $exp=$_[0]; $exp="($exp)"; 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; } my @a=generate_fraction($a); my @b=generate_fraction($b); my @c=generate_fraction($c); my @d=generate_fraction($d); my @e=generate_fraction($e); my @f=generate_fraction($f); $a=enclose($a) if $a=~m/\//; $b=enclose($b) if $b=~m/\//; #$c=enclose($c) if $c=~m/\//; $d=enclose($d) if $d=~m/\//; $e=enclose($e) if $e=~m/\//; #$f=enclose($f) if $f=~m/\//; my $display1=clean_up("$a x+$b y=$c"); my $display2=clean_up("$d x+$e y=$f"); my $equation1=clean_up($display1); my $equation2=clean_up($display2); #$equation1=clean_up($equation1) my @sys1_denom=$display1=~m/\/(\d+)/g; my @sys2_denom=$display2=~m/\/(\d+)/g; my $sys1_lcm=multilcm(@sys1_denom); my @sys1_lcm=generate_fraction($sys1_lcm); my $sys2_lcm=multilcm(@sys2_denom); my @sys2_lcm=generate_fraction($sys2_lcm); #print " # #Start with the given system # #{{{$display1}}} # #{{{$display2}}} # # #"; my ($flag1,$flag2,$inv_b,$inv_e); if((($a=~m/\//)||($b=~m/\//))||($c=~m/\//)) {$display1=clean_up("$sys1_lcm($a x+$b y)=$sys1_lcm($c)"); print " Let's look at the first equation {{{$equation1}}} {{{$display1}}} Multiply both sides of the first equation by the LCD $sys1_lcm "; $a=mult(@sys1_lcm,@a); @a=generate_fraction($a); $a=reduce(@a); $a=~s/\/1$//; $b=mult(@sys1_lcm,@b); @b=generate_fraction($b); $b=reduce(@b); $b=~s/\/1$//; $c=mult(@sys1_lcm,@c); @c=generate_fraction($c); $c=reduce(@c); $c=~s/\/1$//; $display1=clean_up("$a x+$b y=$c"); print " {{{$display1}}} Distribute "; $equation1=$display1; print " --------- "; $flag1=1; } if((($d=~m/\//)||($e=~m/\//))||($f=~m/\//)) {$display2=clean_up("$sys2_lcm($d x+$e y)=$sys2_lcm($f)"); print " Let's look at the second equation {{{$equation2}}} {{{$display2}}} Multiply both sides of the second equation by the LCD $sys2_lcm "; $d=mult(@sys2_lcm,@d); @d=generate_fraction($d); $d=reduce(@d); $d=~s/\/1$//; $e=mult(@sys2_lcm,@e); @e=generate_fraction($e); $e=reduce(@e); $e=~s/\/1$//; $f=mult(@sys1_lcm,@f); @f=generate_fraction($f); $f=reduce(@f); $f=~s/\/1$//; $display2=clean_up("$d x+$e y=$f"); print " {{{$display2}}} Distribute "; $equation2=$display2; print " --------- "; $flag2=1; } if(($flag1==1)||($flag2==1)) { print " So our new system of equations is: {{{$equation1}}} {{{$equation2}}} "; } else { print " Start with the given system of equations: {{{$equation1}}} {{{$equation2}}} ";} print " In order to graph these equations, we need to solve for y for each equation. "; #---------------------------# print " So let's solve for y on the first equation {{{$equation1}}} Start with the given equation "; $equation1="$b y=$c-$a x"; $equation1=clean_up($equation1); my $abs=$a; $abs=~s/\-//; $abs="" if $abs==1; print " {{{$equation1}}} ", ($a=~m/\-/) ? "Add {{{$abs x}}} to" : " Subtract {{{$abs x}}} from"; print " both sides "; $equation1="$b y=-$a x+$c"; $equation1=clean_up($equation1); print " {{{$equation1}}} Rearrange the equation "; $equation1="y=(-$a x+$c)/($b)"; $equation1=clean_up($equation1); print " {{{$equation1}}} Divide both sides by {{{$b}}} "; $equation1="y=(-$a/$b) x+($c)/($b)"; $equation1=clean_up($equation1); print " {{{$equation1}}} Break up the fraction "; my $eq1_reduced_m=reduce(-$a,$b); my $eq1_reduced_b=reduce($c,$b); $eq1_reduced_m=~s/\/1$//; $eq1_reduced_b=~s/\/1$//; $equation1="y=$eq1_reduced_m x+$eq1_reduced_b" if $eq1_reduced_m!~m/\//; $equation1="y=($eq1_reduced_m) x+$eq1_reduced_b" if $eq1_reduced_m=~m/\//; $equation1=clean_up($equation1); my $equa1_graph=$equation1; $equa1_graph=~s/y=//g; print " {{{$equation1}}} Reduce Now lets graph {{{$equation1}}} (note: if you need help with graphing, check out this <a href=http://www.algebra.com/algebra/homework/Linear-equations/graphing-linear-equations.solver>solver</a>) {{{ graph( 600, 600, -10, 10, -10, 10, $equa1_graph) }}} Graph of {{{$equation1}}} "; ### -------------------------------------------------------------------------- ### print " So let's solve for y on the second equation {{{$equation2}}} Start with the given equation "; $equation2="$e y=$f-$d x"; $equation2=clean_up($equation2); $abs=$d; $abs=~s/\-//; $abs="" if $abs==1; print " {{{$equation2}}} ", ($d=~m/\-/) ? "Add {{{$abs x}}} to" : " Subtract {{{$abs x}}} from"; print " both sides "; $equation2="$e y=-$d x+$f"; $equation2=clean_up($equation2); print " {{{$equation2}}} Rearrange the equation "; $equation2="y=(-$d x+$f)/($e)"; $equation2=clean_up($equation2); print " {{{$equation2}}} Divide both sides by {{{$e}}} "; $equation2="y=(-$d/$e) x+($f)/($e)"; $equation2=clean_up($equation2); print " {{{$equation2}}} Break up the fraction "; my $eq2_reduced_m=reduce(-$d,$e); my $eq2_reduced_b=reduce($f,$e); $eq2_reduced_m=~s/\/1$//; $eq2_reduced_b=~s/\/1$//; $equation2="y=$eq2_reduced_m x+$eq2_reduced_b" if $eq2_reduced_m!~m/\//; $equation2="y=($eq2_reduced_m) x+$eq2_reduced_b" if $eq2_reduced_m=~m/\//; $equation2=clean_up($equation2); my $equa2_graph=$equation2; $equa2_graph=~s/y=//g; print " {{{$equation2}}} Reduce "; print " Now lets add the graph of {{{$equation2}}} to our first plot to get: {{{ graph( 600, 600, -10, 10, -10, 10, $equa1_graph,$equa2_graph) }}} Graph of {{{$equation1}}}(red) and {{{$equation2}}}(green) "; my $xans_num=$c*$e-$f*$b; my $xans_denom=$a*$e-$d*$b; my $yans_num=$a*$f-$d*$c; my $yans_denom=$a*$e-$d*$b; my $xans=reduce($xans_num,$xans_denom) if $xans_denom!=0; my $yans=reduce($yans_num,$yans_denom) if $yans_denom!=0; $xans=~s/\/1$//; $yans=~s/\/1$//; if(($eq1_reduced_m eq $eq2_reduced_m)&&($eq1_reduced_b ne $eq2_reduced_b)) {print "From the graph, we can see that the two lines are parallel and will never intersect. So there are no solutions and the system is inconsistent."; return;} elsif(($eq1_reduced_m eq $eq2_reduced_m)&&($eq1_reduced_b eq $eq2_reduced_b)) {print "From the graph, we can see that the two lines are identical (one lies perfectly on top of the other) and intersect at all points of both lines. So there are an infinite number of solutions and the system is dependent."; return;} else { print "From the graph, we can see that the two lines intersect at the point ({{{$xans}}},{{{$yans}}}) (note: you might have to adjust the window to see the intersection)"; } ==section output ==section check angle=40 angle1=50