![]() |
![]() |
#1 (permalink) |
Banned
Location: back to my old location
|
[perl] Solving an algebraic equation
Hi, im a complete no0b at Perl, but I attempted to write a program that can solve the y-y=m(x-x) equation (forgot what its called).
Here it is: #!/usr/bin/perl use warnings; use strict; print "You solve the y-y=m(x-x) equation with this program. \n"; print "Enter y variable. \n"; my $cot = [STDIN]; print "Enter slope. \n"; my $hot = [STDIN]; print "Enter x variable. \n"; my $pot = [STDIN]; print "y-$cot=$hot(x-$pot) \n"; So, when I execute it, it works, no errors, I enter the slope and stuff, but it doesnt solve the equation. It's like: y-4 =2.5 (x-1 ) What could be the problem? Edit: After the variables there should be the STDIN tag thing. Last edited by VF19; 01-24-2004 at 01:53 PM.. |
![]() |
![]() |
#2 (permalink) |
Banned
Location: 'bout 2 feet from my iMac
|
you can use tags [code] and [/ code] to encase your code so we can see it all... please...
by the way, what is is SUPPOSED to print out? it's doing EXACTLY what you're asking it to do, printing out the values you put in. Last edited by cheerios; 01-24-2004 at 01:38 PM.. |
![]() |
![]() |
#3 (permalink) |
Banned
Location: back to my old location
|
Like I said I'm a no0b I have no clue what im doing.
I want it to solve the y-y=m(x-x) equation.(i think its called writing the equation of the line?) The point was (1,4)(x and y) and the slope was 2.5 (m) In this case, it was supposed to put out y=2.5x+1.5 |
![]() |
![]() |
#4 (permalink) |
Banned
Location: 'bout 2 feet from my iMac
|
ok, well you need to tell it to do that. your eqyation is y - y1 = m * (x - x1) you need to show the program HOW to print the equation you want.
Code:
print "y-$cot=$hot(x-$pot) \n"; what YOU want to do is break this up into steps. you always want to print "y =" right? and that will always be followed by your m value, followed by "x + " and then that last value. so you have every bit of information you need, except that last value. so, solve that generically, $b = something then change your print statement to be something like this: Code:
print "y = $m x + $b \n" |
![]() |
![]() |
#5 (permalink) |
Banned
Location: back to my old location
|
Ok.. I got you there. I should have known I had to do the calculating and whatnot ..
Still doesn't work, but a hell of a lot closer than where I was. Here it is: #!/usr/bin/perl use warnings; use strict; print "You can solve the y-y=m(x-x) equation with this program. \n"; print "Enter y variable. \n"; my $yval = [STDIN]; print "Enter slope. \n"; my $slpe = [STDIN]; print "Enter x variable. \n"; my $xval = [STDIN]; $xvxsl= "$xval * $slpe"; $yvpxxs= "$yval + $xvxsl"; print "Step one: y-$yval=$slpe x- $xvxsl \n"; print "Step two: y-$yval+$yval=$slpv x - $yvpxxs \n"; print "Answer: y= $slpe x - $yvpxxs \n"; Got a bunch of errors, says that I need an explicit package name ![]() |
![]() |
![]() |
#6 (permalink) |
Banned
Location: 'bout 2 feet from my iMac
|
here's one issue:
Code:
$xvxsl= "$xval * $slpe"; $yvpxxs= "$yval + $xvxsl"; |
![]() |
![]() |
#7 (permalink) |
Banned
Location: back to my old location
|
Hooray! It works!
Well sort of. The answer it shows for the (1,4) m=2.5 equation is y=2.5x-6.5, which is wrong. It's y=2.5x-1.5. Any idea on how I can change that? Like change the $yvpxxs to a negative number? Ok, so I deleted to "" signs, didnt work, but I put 'my' in front of the variable things and it worked.. Here's the working script: #!/usr/bin/perl use warnings; use strict; print "You can solve the y-y=m(x-x) equation with this program. \n"; print "Enter y variable. \n"; my $yval = [STDIN]; print "Enter slope. \n"; my $slpe = [STDIN]; print "Enter x variable. \n"; my $xval = [STDIN]; my $xvxsl= $xval * $slpe ; my $yvpxxs= $yval + $xvxsl ; print "Step one: y-$yval=$slpe x-$xvxsl \n"; print "Step two: y-$yval+$yval=$slpe x-$yvpxxs \n"; print "Answer: y= $slpe x-$yvpxxs \n"; |
![]() |
![]() |
#9 (permalink) |
Upright
Location: BC, Canada
|
chomp chomp chomp
as a general rule of thumb you should always chomp everything you get from STDIN. heres an example that you can put into your code.
Code:
my $xval = <STDIN>; chomp ($yval, $slpe, $xval);
__________________
- 42 - |
![]() |
![]() |
#11 (permalink) |
Upright
Location: BC, Canada
|
Chomp removes newlines not spaces.
Code:
my $val = "hhh \n"; print "$val";print "h\n"; chomp ($val); print "$val";print "h"; Code:
hhh h hhh h reference: http://www.perldoc.com/perl5.8.0/pod/func/chomp.html
__________________
- 42 - |
![]() |
![]() |
#12 (permalink) |
Banned
Location: back to my old location
|
Oh ok.. Thanks for that chomp function, really makes it easier to read the output.
Yeah, I just multiplied $yvpxxs by -1. Now it's: #!/usr/bin/perl use warnings; use strict; print "You can solve the y-y=m(x-x) equation with this program. \n"; print "Enter y variable. \n"; my $yval = [STDIN]; print "Enter slope. \n"; my $slpe = [STDIN]; print "Enter x variable. \n"; my $xval = [STDIN]; chomp ($yval, $slpe, $xval); my $xvxsl= $xval * $slpe ; my $yvpxxs= $yval + $xvxsl *-1 ; print "Step one: y-$yval=$slpe x-$xvxsl \n"; print "Step two: y-$yval+$yval=$slpe x-$yvpxxs \n"; print "Answer: y= $slpe x-$yvpxxs \n"; |
![]() |
Tags |
algebraic, equation, perl, solving |
|
|