Computer Science Canada Turing Graphing Calculator (Help) |
Author: | TW iz Rippin [ Mon Apr 02, 2012 10:14 pm ] |
Post subject: | Turing Graphing Calculator (Help) |
What is it you are trying to achieve? To create a graphing calculator in turing using GUI and the f(x) functions What is the problem you are having? Cant figure out how to take the f(x) = _x5+_x4+_x3+_x2+_x+c and graph it if someone can give me an idea it would be appreciated. Describe what you have tried to solve this problem Various different sequences of coding and searching different forums Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) Im not at school so I cant post the code atm Please specify what version of Turing you are using 4.1.1 |
Author: | DemonWasp [ Mon Apr 02, 2012 10:19 pm ] |
Post subject: | RE:Turing Graphing Calculator (Help) |
Well, the function y = f(x) implies the solution. For any given input, x, to the function, f, you get an output, y. I hope (x,y) looks familiar to you. So now you just need to take some sample points (x values), feed them through that equation, and get the samples (y values) out. Then, for each (x,y) pair, draw a dot. You can get more advanced from there. Advanced: drawing lines between each set of sample points to "connect" the graph; scaling and moving the graph so (0,0) is in the center of the screen; graphing multiple functions on the same axes. |
Author: | TW iz Rippin [ Tue Apr 03, 2012 9:57 am ] |
Post subject: | Re: RE:Turing Graphing Calculator (Help) |
DemonWasp @ Mon Apr 02, 2012 10:19 pm wrote: For any given input, x, to the function, f, you get an output, y.
can you elaborate on this further please |
Author: | DemonWasp [ Tue Apr 03, 2012 12:42 pm ] | ||
Post subject: | RE:Turing Graphing Calculator (Help) | ||
Not really. I could provide an example:
Let's go with the simple case of C = 0. Now we know that (x,y) = (2,55) is on the function. So, we can draw a dot at (2,55). Next, try x = 3... |
Author: | TW iz Rippin [ Tue Apr 03, 2012 3:39 pm ] | ||
Post subject: | Re: RE:Turing Graphing Calculator (Help) | ||
DemonWasp @ Tue Apr 03, 2012 12:42 pm wrote: Not really. I could provide an example:
Let's go with the simple case of C = 0. Now we know that (x,y) = (2,55) is on the function. So, we can draw a dot at (2,55). Next, try x = 3... except it wont always be the same... so what if the function was like f(x) = 5^5 + 4^4 + 3^3 + 2^2 + 1^1 + c (all the values are different... |
Author: | DemonWasp [ Tue Apr 03, 2012 4:03 pm ] |
Post subject: | RE:Turing Graphing Calculator (Help) |
Oh, you didn't mean x^3, you meant x3. I figured you had typed in superscripts and they had been destroyed. If you have multiple inputs, then your function is something like y = f(x1,x2,x3,x4,x5), which you cannot graph completely in two dimensions. If you have 5 inputs and one output, you need 6 dimensions to graph the function. You could graph some 2D "slice" of the function (by setting 4 inputs to constants, varying one input, and graphing that one changing input against the changing output). For example, with x1 = x2 = x3 = x4 = 0, and x5 variable; then y = f(0,0,0,0,x5) = x5, which you can plot (it's a line). |
Author: | TW iz Rippin [ Tue Apr 03, 2012 5:29 pm ] |
Post subject: | RE:Turing Graphing Calculator (Help) |
hmm I dont see what you mean by 6 dimensions to graph. because Im pretty sure thats not what we are to do. (im only in grade 9) some of the functions our teacher expects are program to do are: f(x)=x^2-4 f(x)=x^2+x+12 f(x)=x^2-7x+12 f(x)=x^3-2x^2-5x+6 f(x)=0.5x^4+2x^3-0.5^2-2x f(x)=0.05x^5-0.25x^4-0.65x^3+3.85x^2-3x f(x)=x^4-5x^2+4 |
Author: | Dreadnought [ Tue Apr 03, 2012 8:43 pm ] | ||
Post subject: | Re: Turing Graphing Calculator (Help) | ||
TW iz Rippin wrote: some of the functions our teacher expects our program to do are
f(x)=x^2-4 f(x)=x^2+x+12 f(x)=x^2-7x+12 f(x)=x^3-2x^2-5x+6 f(x)=0.5x^4+2x^3-0.5^2-2x f(x)=0.05x^5-0.25x^4-0.65x^3+3.85x^2-3x f(x)=x^4-5x^2+4 These are all functions that take only one value as input (x in this case) and evaluate to different values based on the x given. You can plot f(x) vs x on a graph by letting y=f(x) and computing f(x) for many values of x. For example:
|
Author: | TW iz Rippin [ Tue Apr 03, 2012 8:50 pm ] |
Post subject: | RE:Turing Graphing Calculator (Help) |
ok.. ok... then would I just keep it on a loop so it runs through like 1-9 for example and draws the dots? |
Author: | TW iz Rippin [ Tue Apr 03, 2012 9:27 pm ] | ||
Post subject: | RE:Turing Graphing Calculator (Help) | ||
*not my full code... just something I whipped up to test
It only shows like 2-3 dots very close together It dosnt seem to be working properly.. any tips? |
Author: | Dreadnought [ Tue Apr 03, 2012 9:35 pm ] | ||
Post subject: | Re: Turing Graphing Calculator (Help) | ||
Try something like
This is basically the function f(x) = 20*x. (EDIT: why did I say "basically"? IT IS that function) Maybe this will give you an idea of the problem (hint: think of your axes and their scale). |
Author: | TW iz Rippin [ Tue Apr 03, 2012 9:47 pm ] | ||
Post subject: | Re: Turing Graphing Calculator (Help) | ||
Dreadnought @ Tue Apr 03, 2012 9:35 pm wrote: Try something like
This is basically the function f(x) = 20*x. (EDIT: why did I say "basically"? IT IS that function) Maybe this will give you an idea of the problem (hint: think of your axes and their scale). while I ponder this do you know how do to this in real numbers because the syntax for draw.filloval is all integer so Draw.FillOval (i, fx, 3, 3, black) wont work if I have one of the inputs as real (which it should be) |
Author: | Dreadnought [ Tue Apr 03, 2012 10:01 pm ] |
Post subject: | Re: Turing Graphing Calculator (Help) |
Well, lets say you want x values in increments of 0.05 between 0 and 10. Well, what's an increment but a step of a certain size (0.05 in this case). So we start with 0, then 0.05 then 0.1, then 0.15, etc. Notice we can write this as 0*0.05, then 1*0.05, then 2*0.05, then 3*0.05, etc. For 0 to 10 by 0.05, you would have 200 increments (this does not include 0 which is the starting value), so you could run a loop 200 times and simply divide the number of times it has run by 20 to get the value of x for that iteration. Now, this brings us back, to your previous problem in a way (the scale of our axes). How do we graph (0.05, 1.25) with functions that draw at integer numbers of pixels? Well, we can change the scale of our axes if we require. Make each increment of 0.05 in the x direction on your graph be a increment of 1 pixel on your screen (or more if you want). You can do the same with the y coordinate. Of course, use round to convert from real to integer (since 1.0 is not considered an integer). Think of it as stretching your graph on the screen. Hope this helps. |
Author: | TW iz Rippin [ Tue Apr 03, 2012 10:34 pm ] | ||
Post subject: | RE:Turing Graphing Calculator (Help) | ||
I understand what your saying, im just turing illiterate (first year)... when i do like for i : 0 .. 10 by 0.05 it says it has to be an int so idk how i can do that lol and for the graphing of real numbers as points. I try to do round on i sooo
I get an error on the round because its not a procedure and on the fill oval because its the wrong type. P.S. Im so sorry for how bad I am at this, must be frustrating for you, but this assignment is far above my skill level (and tbh I think its the same for most of the people in my class) P.S.S I guess what im saying is, can you help with the syntax without telling me the exact code cause I can do the math ok |
Author: | Dreadnought [ Wed Apr 04, 2012 2:04 am ] | ||||
Post subject: | Re: Turing Graphing Calculator (Help) | ||||
Ok. So you are right, we can't have a for loop give us real numbers directly. But, what is the difference between increments of 1 and increments of 0.05? Well, its factor of 0.05, so we can just multiply the value incremented by the for loop by 0.05 to obtain the value we want. Example: print 0..1 in increments of 0.05
This is an easy way of getting around the limitation that a for loop must have integer increments. Note, that I set the upper value of the for loop manually, but you could have computed that you needed the for loop to go up to 20 by dividing 1 by 0.05 (you would have to round). TW iz Rippin wrote: I get an error on the round because its not a procedure and on the fill oval because its the wrong type. Well, that is because round is a not a procedure, but a function (kinda like your f(x) function you want to graph). Functions, are pieces of code that take one or more (or zero in rare cases) values as input (we call these arguments) and produce a value as output. Procedures are pieces of code that can take value as input but produce no output. They generally make changes to variables, the screen or other things (since they don't have output). The problem you are having is that procedure calls (when you ask Turing to run them) are different from function calls.
Ok, so if you're still reading this, the main idea is that functions like round produce a value and you need to do something with that value. Since, f(x) will be a real value much of the time, we will need to use round to convert to the nearest integer if we want to use it in a procedure that accepts only integer arguments (like Draw.Oval). Hope this helps. |
Author: | TW iz Rippin [ Wed Apr 04, 2012 6:24 am ] | ||
Post subject: | RE:Turing Graphing Calculator (Help) | ||
Ok one last question.... Why does this not work?:
|
Author: | mirhagk [ Wed Apr 04, 2012 8:32 am ] |
Post subject: | RE:Turing Graphing Calculator (Help) |
you can't use a variable in a constant declaration. It needs to know the value of x before the program starts. Just use a var for x. |
Author: | Dreadnought [ Wed Apr 04, 2012 11:02 am ] | ||
Post subject: | Re: Turing Graphing Calculator (Help) | ||
mirhagk wrote: you can't use a variable in a constant declaration. It needs to know the value of x before the program starts. Just use a var for x.
Sure you can, mirhagk please read the documentation for const. http://compsci.ca/holtsoft/doc/const.html wrote: In the Pascal language, constants must have values known at compile time; Turing has no such restriction.
The real problem is that Draw.FillOval expects integers as arguments (I'm talking about x and fx) because it draws at specific pixel locations on the screen. Think about it, you're telling it to draw an oval centered at pixel 0.05 on the screen (in the x direction), which doesn't really make sense since there is pixel 1 and pixel 2 but nothing in between. You can use round to round a real number to the nearest integer, but in this case that wont be enough, since all your values for x are between 0 and 1. What you need to do is multiply x by some number (try 10 or 20) and then round the result of that. Think of it as stretching a graph that goes from 0 to 1 on the x axis across 5 to 10 pixels on screen. (Note: I'm saying you should do this when you draw the dot, not when you compute fx) Notice, that since fx will be real in this case too, you will need to do something similar for its value. |
Author: | TW iz Rippin [ Wed Apr 04, 2012 11:03 am ] | ||
Post subject: | RE:Turing Graphing Calculator (Help) | ||
yay i figured it out ![]()
Now.. can someone help me drawline between all the dots so it looks good? ![]() |
Author: | Dreadnought [ Wed Apr 04, 2012 11:21 am ] |
Post subject: | Re: Turing Graphing Calculator (Help) |
TW iz Rippin wrote: Now.. can someone help me drawline between all the dots so it looks good?
![]() Well, the dots are drawn at values you computed, just keep track of where the last dot was (using a variable or two) and draw a line between the last dot and the new one. (Note: you may need a special case for the first dot) I hope that made sense. |
Author: | TW iz Rippin [ Wed Apr 04, 2012 1:06 pm ] |
Post subject: | Re: Turing Graphing Calculator (Help) |
Dreadnought @ Wed Apr 04, 2012 11:21 am wrote: TW iz Rippin wrote: Now.. can someone help me drawline between all the dots so it looks good?
![]() Well, the dots are drawn at values you computed, just keep track of where the last dot was (using a variable or two) and draw a line between the last dot and the new one. (Note: you may need a special case for the first dot) I hope that made sense. sure does! ty |
Author: | Raknarg [ Wed Apr 04, 2012 1:24 pm ] |
Post subject: | RE:Turing Graphing Calculator (Help) |
Just an interestiong note, in Turing you can state anything anywhere, but it will only be active for that space. for instance, if I did this: loop var x end loop It would work, but I would not be allowed to use it outside that loop |
Author: | TW iz Rippin [ Wed Apr 04, 2012 2:19 pm ] | ||
Post subject: | RE:Turing Graphing Calculator (Help) | ||
YES!!!!!!!!!!! Thanks so much for all the help guys here is the code... (not posting full code with gui just the function:
ty again I <3 you all |
Author: | mirhagk [ Wed Apr 04, 2012 3:11 pm ] |
Post subject: | Re: Turing Graphing Calculator (Help) |
Dreadnought @ Wed Apr 04, 2012 11:02 am wrote: mirhagk wrote: you can't use a variable in a constant declaration. It needs to know the value of x before the program starts. Just use a var for x.
Sure you can, mirhagk please read the documentation for const. http://compsci.ca/holtsoft/doc/const.html wrote: In the Pascal language, constants must have values known at compile time; Turing has no such restriction. Huh, did not know that. Just figured it was like a lot of languages where constants are inserted at compile time, rather than just being variables you can't change. |
Author: | TW iz Rippin [ Thu Apr 05, 2012 7:27 am ] |
Post subject: | RE:Turing Graphing Calculator (Help) |
bump |