Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Graphing program trouble, need help!

Status
Not open for further replies.

actibus

Programmer
Jul 26, 2001
1
US
Hi, I made this graphing program the other day, and it works ok, except that it won't work right if i type in negative exponents! It began to graph half a hyperbola once, but I'm not sure what happened afterwards! Anyway, here is the code, and tell me if you can see what's wrong with it:

SCREEN 12
CLS
INPUT "What is the coefficient of x"; num1
INPUT "What is the power of x"; num2
INPUT "What number is added to the equation"; num3
PRINT
PRINT "Inputting a high value below will increase speed, at the cost of"
PRINT "quality. A lower value will be more detailed but slower. A recommended"
PRINT "value is anything between 1 and 10."
INPUT "What intervals are to be set for each value of x"; value
INPUT "And finally, what color (1-14) do you want your graph in?"; co
CLS
LINE (320, 0)-(320, 480)
LINE (0, 240)-(640, 240)

Y = num1 * x ^ num2 + num3

top:
x = x + value / (10 ^ 3)
IF x > 30 THEN GOTO top2
Y = num1 * x ^ num2 + num3
PSET (320 + x * 15, 240 - Y * 15), co
GOTO top

top2:
x = x - value / (10 ^ 3)
IF x < -30 THEN GOTO finish
Y = num1 * x ^ num2 + num3
PSET (320 + x * 15, 240 - Y * 15), co
GOTO top2

finish:
INPUT &quot;You may input more if you wish. Y/N&quot;; Y$
IF Y$ = &quot;Y&quot; THEN GOTO begin
IF Y$ = &quot;y&quot; THEN GOTO begin
IF Y$ = &quot;N&quot; THEN END
IF Y$ = &quot;n&quot; THEN END
 
Hi, actibus.

I hope that by now you have sorted this out yourself, but just in case you haven't :

I would have expected your code to work with both positive and negative exponents while x>0, but when x<0 you cannot expect it to work with non-integer exponents.

eg (-1)^0.5 is the square root of -1, which is an imaginary number and cannot be plotted. similarly any non-integral power of any negative number.

 
austim is right, you have to come up with a procedure to handle i. i is a tricky consept, while i is imaginary roots of i are real. If I remember correctly the pattern goes like this

i = SQRT(-1)
i^2 = -1
1^3 = -i
1^4 = 1
and then that loops around in that pattern
you will only be able to graph i to the power of something even
 
I got a suggestion for you, you should run your program to understand and calculate strings, rather than numbers. Have your program see if there will be an imaginary number before it crunches anything, do the i, then using VAL do the calculation. The use a$ = str$(answer)+&quot;i&quot; to finish the calculation

When you graph it, take the &quot;i&quot; out, and draw the reals
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top