
-----------------------------------
TW iz Rippin
Mon Apr 02, 2012 10:14 pm

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

-----------------------------------
DemonWasp
Mon Apr 02, 2012 10:19 pm

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.

-----------------------------------
TW iz Rippin
Tue Apr 03, 2012 9:57 am

Re: RE:Turing Graphing Calculator (Help)
-----------------------------------
For any given input, x, to the function, f, you get an output, y.

can you elaborate on this further please

-----------------------------------
DemonWasp
Tue Apr 03, 2012 12:42 pm

RE:Turing Graphing Calculator (Help)
-----------------------------------
Not really. I could provide an example:

[code]
If x = 2 then
y = x ^ 5 + x ^ 4 + x ^ 3 + x ^ 2 + x + C
y = 2 ^ 5 + 2 ^ 4 + 2 ^ 3 + 2 ^ 2 + 2 + C
y = 25 + 16 + 9 + 4 + 1 + C
y = 55 + C
[/code]

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...

-----------------------------------
TW iz Rippin
Tue Apr 03, 2012 3:39 pm

Re: RE:Turing Graphing Calculator (Help)
-----------------------------------
Not really. I could provide an example:



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...

-----------------------------------
DemonWasp
Tue Apr 03, 2012 4:03 pm

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).

-----------------------------------
TW iz Rippin
Tue Apr 03, 2012 5:29 pm

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

-----------------------------------
Dreadnought
Tue Apr 03, 2012 8:43 pm

Re: Turing Graphing Calculator (Help)
-----------------------------------
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: [code]Consider f(x) = 0.05*x^5 - 0.25*x^4 - 0.65*x^3 + 3.85*x^2 - 3*x

We can compute f(x) at various values of x (here x=1, and x=2)

f(1) = 0.05*(1)^5 - 0.25*(1)^4 - 0.65*(1)^3 + 3.85*(1)^2 - 3*(1)
f(1) = 0.05 - 0.25 - 0.65 + 3.85 - 3
f(1) = 0

f(2) = 0.05*(2)^5 - 0.25*(2)^4 - 0.65*(2)^3 + 3.85*(2)^2 - 3*(2)
f(2) = 0.05*32 - 0.25*16 - 0.65*8 + 3.85*4 - 3*2
f(2) = 1.8

Now we can plot two points on a graph (1, 0) and (2, 1.8).[/code]

-----------------------------------
TW iz Rippin
Tue Apr 03, 2012 8:50 pm

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?

-----------------------------------
TW iz Rippin
Tue Apr 03, 2012 9:27 pm

RE:Turing Graphing Calculator (Help)
-----------------------------------
*not my full code... just something I whipped up to test


[code]
View.Set ("graphics:400;400")

var text1, text2, text3, text4, text5, text6 : int
var fx : int

get text1
get text2
get text3
get text4
get text5
get text6


%%%%%%%%%%%%%
for i : 1 .. 9
    fx := (text1 * (i ** 5)) + (text2 * (i ** 4)) + (text3 * (i ** 3)) + (text4 * (i ** 2)) + (text5 * i) + text6
    Draw.FillOval (i, fx, 3, 3, black)
end for
[/code]


It only shows like 2-3 dots very close together
It dosnt seem to be working properly.. any tips?

-----------------------------------
Dreadnought
Tue Apr 03, 2012 9:35 pm

Re: Turing Graphing Calculator (Help)
-----------------------------------
Try something like
[code]test1 = 0
test2 = 0
test3 = 0
test4 = 0
test5 = 20
test6 = 0[/code]

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).

-----------------------------------
TW iz Rippin
Tue Apr 03, 2012 9:47 pm

Re: Turing Graphing Calculator (Help)
-----------------------------------
Try something like


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)

-----------------------------------
Dreadnought
Tue Apr 03, 2012 10:01 pm

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 [tdoc]round[/tdoc] 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.

-----------------------------------
TW iz Rippin
Tue Apr 03, 2012 10:34 pm

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

[code]
for i : 1 .. 9
    fx := 0
    fx := (text1 * (i ** 5)) + (text2 * (i ** 4)) + (text3 * (i ** 3)) + (text4 * (i ** 2)) + (text5 * i) + text6
    round (fx)
    Draw.FillOval (i, fx, 3, 3, black)
end for
[/code]

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

-----------------------------------
Dreadnought
Wed Apr 04, 2012 2:04 am

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
for i:0..20
    const x := i * 0.05 % store the value we want (I'm using a constant to mimic the way a for loop works)
    put x
end for

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 
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 % We call a procedure like so
procName (arg1, agr2, ...) % note that if there are no arguments we don't need brackets

% Examples of procedures are 

cls    % it takes no arguments, you could also call "cls ()"
View.Set (SetupString)    % takes a string as an argument (Ex "graphics;400:300")
Draw.Oval (x ,y, xrad, yrad, )   % takes five arguments of different types and draws an oval on the screen


% To call a function is similar, but it returns a value. We need to do something with that value
% Either we assign the value to a variable, or we use it as an argument to some other procedure or function
var varName :  % type could be int, real, string, etc.
varName := fcnName (arg1, arg2, ...) 

% Examples of functions

var two : int := round (1.5) % The round function rounds a real real number toward the nearest integer
var tenIsBigger : int := max (7, 10) % The max functions returns the largest of its arguments
var ch : char : getchar  % The getchar function takes no arguments and produces character corresponding to the key pressed by the user

% We can also do this
put max (round (1.5), 1.5) % Here we round 1.5 and use that as an argument to max, and we print the result to the screen (it will be 2 in this case)

Ok, so if you're still reading this, the main idea is that functions like [tdoc]round[/tdoc] 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 [tdoc]round[/tdoc] 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.

-----------------------------------
TW iz Rippin
Wed Apr 04, 2012 6:24 am

RE:Turing Graphing Calculator (Help)
-----------------------------------
Ok one last question....

Why does this not work?:

[code]

for i : 0 .. 10
    const x := i * 0.05
    fx := (text1 * (x ** 5)) + (text2 * (x ** 4)) + (text3 * (x ** 3)) + (text4 * (x ** 2)) + (text5 * x) + text6
    Draw.FillOval (x, fx, 3, 3, black)
end for

[/code]

-----------------------------------
mirhagk
Wed Apr 04, 2012 8:32 am

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.

-----------------------------------
Dreadnought
Wed Apr 04, 2012 11:02 am

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.

Sure you can, mirhagk please read the documentation for  In the Pascal language, constants must have values known at compile time; Turing has no such restriction.
% This is a example I used earlier, IT WORKS
for i:0..20 
    const x := i * 0.05 % store the value we want (I'm using a constant to mimic the way a for loop works) 
    put x 
end for

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 [tdoc]round[/tdoc] 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.

-----------------------------------
TW iz Rippin
Wed Apr 04, 2012 11:03 am

RE:Turing Graphing Calculator (Help)
-----------------------------------
yay i figured it out :)

[code]
 var fx : real
    for i : -200 .. 200
        const x := i / 20
        fx := (in1 * (x ** 5)) + (in2 * (x ** 4)) + (in3 * (x ** 3)) + (in4 * (x ** 2)) + (in5 * x) + in6
        Draw.FillOval (floor (x * 20) + 200, floor (fx * 20) + 200, 1, 1, brightred)
    end for
[/code]

Now.. can someone help me drawline between all the dots so it looks good? :)

-----------------------------------
Dreadnought
Wed Apr 04, 2012 11:21 am

Re: Turing Graphing Calculator (Help)
-----------------------------------
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.

-----------------------------------
TW iz Rippin
Wed Apr 04, 2012 1:06 pm

Re: Turing Graphing Calculator (Help)
-----------------------------------
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

-----------------------------------
Raknarg
Wed Apr 04, 2012 1:24 pm

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

-----------------------------------
TW iz Rippin
Wed Apr 04, 2012 2:19 pm

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:

[code]
for i : -200 .. 200
    const x := i / 20
    var xx := x
    fx := (text1 * (xx ** 5)) + (text2 * (xx ** 4)) + (text3 * (xx ** 3)) + (text4 * (xx ** 2)) + (text5 * xx) + text6
    Draw.FillOval (floor (xx * 20) + 200, floor (fx * 20) + 200, 1, 1, brightred)
    var track1 : real := floor (xx * 20) + 200
    var track12 : real := floor (fx * 20) + 200
    xx := xx + 1
    fx := (text1 * (xx ** 5)) + (text2 * (xx ** 4)) + (text3 * (xx ** 3)) + (text4 * (xx ** 2)) + (text5 * xx) + text6
    Draw.FillOval (floor (xx * 20) + 200, floor (fx * 20) + 200, 1, 1, brightred)
    var track2 : real := floor (xx * 20) + 200
    var track22 : real := floor (fx * 20) + 200
    drawline (round (track1), round (track12), round (track2), round (track22), brightred)
end for
[/code]

ty again I 