Formula Error?
Author |
Message |
YuNoMad?
|
Posted: Wed Jun 15, 2011 9:43 pm Post subject: Formula Error? |
|
|
What is it you are trying to achieve?
To graph an equation y = 2x -3 on a graph when x is -20 to 20 by an increment of 1 each time
What is the problem you are having?
The graphed line will not appear on the screen or if it does it stops after one segment
Describe what you have tried to solve this problem
I have tried fixing this by changing what x is and it still isn't working, but since my teacher doesn't really teach us he just posts tutorials from this site, I don't know how to fix it. Also, I tried making the boxes that get drawn larger by 1000 pixels and they barely show up in the top left
Post any relevant code
Turing: |
var xx, yy : int
i := - 1
xx := (version one was the formula - 20 * 15 + 335, version two I tried simply putting 200)
yy := 2 * xx - 3
loop
i := i + 1
delay (5)
drawfillbox (xx - 2, yy - 2, xx + 2, yy + 2, 2)
drawline (xx, yy, xx + 15, yy + 15, 2)
xx := (- 20 + i ) * 15 + 335
yy := 2 * x - 3
exit when i = 40
end loop
|
Please specify what version of Turing you are using
At school I'm not sure what version we use but at home I use 4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
RandomLetters
|
Posted: Wed Jun 15, 2011 10:11 pm Post subject: Re: Formula Error? |
|
|
It's working for me, it draws green squares with segments attached to them.
Are you trying to connect them as one line?
Turing: |
var xx, yy : int
var i := - 1
xx := 20 * 15 + 335
yy := 2 * xx - 3
loop
i := i + 1
delay (5)
drawfillbox (xx - 2, yy - 2, xx + 2, yy + 2, 2)
drawline (xx, yy, xx + 15, yy + 15, 2)
xx := (- 20 + i ) * 15 + 335
yy := 2 * xx - 3
exit when i = 40
end loop
|
|
|
|
|
|
|
Zren
|
Posted: Wed Jun 15, 2011 11:16 pm Post subject: RE:Formula Error? |
|
|
You can ignore the top bit, but seeing as your teach is...
You can learn more about functions and subroutines in the Turing Walkthrough.
Turing: |
% y = 2x-3
% f(x) = 2x-3
% First off, if you haven't learnd functions in math+compsci yet, this 'll make
% it easier to read, and change the formula later.
% The basics of it are
% 1 2 3 4
function f (x : int) : int
% 5 6
result 2 * x - 3
% 7
end f
% 1: Declare the next block of code is part of a function. All the way to 7.
% 2: The name of the function. Must be unique.
% 3: Local variables that can only be used within the function. Sort of like
% with the varibles you use in loops.
% 4: This function must return (be equal to) something when you call it. In
% this example, we return a number.
% 5: Since a function must return something, we have to have some way to tell
% what that is.
% You can also have other bits of code and other local variables.
function anotherF (x : int) : int
var y := 2 * x - 3
result y
end anotherF
% Using the function. It returns a number to be printed.
put f (42)
|
Now onto your assumed problem. Where the graph goes way offscreen. You need to scale it. More precisely, you need to scale the numbers you calculate of the equation to be used for the coordinates.
Turing: |
function f (x : int) : int
result 2 * x - 3
end f
% Originally just drawing points at the coordinates on the screen.
for x : 0 .. maxx
Draw.Dot (x, f (x ), black)
end for
% Find the highest y coordinate in the range used of the function.
var gMaxY := 0
for x : 0 .. maxx
var y := f (x )
if y > gMaxY then
gMaxY := y
end if
end for
% We want to scale the graph so that the top of the screen is the highest point.
% To do that, we want each pixel on the screen to represent more than one unit.
% Like when you have a dash count for 2 or 5 in a graph on paper. To how much
% we count by, we divide the number of dashes we'd have (in this case, pixels),
% with the maximum number we desire.
var scaleY := maxy / gMaxY
% Then we scale all the points by that ratio.
for x : 0 .. maxx
Draw.Dot (x, round (f (x ) * scaleY ), red)
end for
|
That was either overkill, or completely off the mark. Oh well. |
|
|
|
|
|
YuNoMad?
|
Posted: Wed Jun 15, 2011 11:39 pm Post subject: Re: Formula Error? |
|
|
if I had the time I would throw out what I have now and start over with functions, but I need it finished tomorrow so I'm going to change it up a bit as I have it below. it's uncommented, sorry, don't have the time >.> (two projects and this for tomorrow)
Turing: |
var i, xx, yy : int
procedure progi
i := - 9
xx := - 10 * 15 + 335
yy := (2 * - 10 - 3) * 15 + 335
loop
i := i + 1
delay (50)
drawfillbox (xx - 2, yy - 2, xx + 2, yy + 2, 2)
drawline (xx, yy, (i + 1) * 15 + 335, (2 * (i + 1) - 3) * 15 + 335, 2)
xx := i * 15 + 335
yy := (2 * i - 3) * 15 + 335
exit when i = 11
end loop
drawfillbox (xx - 2, yy - 2, xx + 2, yy + 2, 2)
end progi
|
the drawline code is just to go through to the next step of the formula and draw a line to there.
The next step of this program is to use a "sin(x)" and I know that has to do with wavelengths but we haven't learnt those yet and I don't know how to convert my xx to something that can be used in a sin, or vice versa, and still be graphed in pixel graphics.
Here is what I have now:
Turing: |
var xx3, yy3 : int
procedure progiv
xx3 := - 20 * 15 + 335
yy3 := sin (xx3 )
loop
drawfillbox (xx3 - 2, yy3 - 2, xx3 + 2, yy3 + 2, 2)
drawline (xx3, yy3, xx3 + 15, yy3 + 15, 2)
xx3 := xx3 + 15
yy3 := xx3
exit when xx >= 640
end loop
drawfillbox (xx - 2, yy - 2, xx + 2, yy + 2, 2)
end progiv
|
should I make a yy3r/xx3r as reals to be used in a sin and round them into ints? |
|
|
|
|
|
Zren
|
Posted: Wed Jun 15, 2011 11:48 pm Post subject: RE:Formula Error? |
|
|
> should I make a yy3r/xx3r as reals to be used in a sin and round them into ints?
Yes.
Just so you know, sin() returns fractions from 0 .. 1. Eg: 0.745113. You might need to scale the number up a bit before rounding it. |
|
|
|
|
|
YuNoMad?
|
Posted: Wed Jun 15, 2011 11:52 pm Post subject: RE:Formula Error? |
|
|
is there anything else specific I should know about sins before attempting to use them in a program, such as limit to the input?
this is what I am going to end up using, but my lines that I'm trying to draw from dot to dot are going off on some odd tangent. why is this?
Turing: |
var y1, yy3, xx3 : int
var yyy3 : real
procedure progiv
i := - 20
xx3 := - 20 * 15 + 335
yyy3 := sin (i )
yy3 := (round (sin (i )) * 15 + 335
y1 := yy3
yy3 := (round (sin (i + 1))) * 15 + 335
loop
i := i + 1
drawline (xx3, y1, i * 15 + 335, (2 * yy3 - 3) * 15 + 335, 5)
yyy3 := sin (i )
yy3 := (round (yyy3 )) * 15 + 335
drawfillbox (xx3 - 2, yy3 - 2, xx3 + 2, yy3 + 2, 5)
xx3 := xx3 + 15
exit when i = 20
y1 := yy3
end loop
drawfillbox (xx3 - 2, yy3 - 2, xx3 + 2, yy3 + 2, 5)
end progiv
|
Fixed. I accidentally accounted for the scale of pixel to graph twice. |
|
|
|
|
|
|
|