Graph-er
Author |
Message |
Insectoid

|
Posted: Fri Jun 25, 2010 12:33 pm Post subject: Graph-er |
|
|
So, I was bored and made this program that takes functions and graphs them. Replace the equation in the function 'y' with whatever function you want, and play around with the window settings (all the constants at the top) so that your function doesn't look like crap. It could use some tweaking but I'm lazy so you can do that yourself. I may eventually make it draw the table but I'm lazy. I don't recommend really, really small xScale values (anything smaller than 0.001 takes AGES to run) and graphing negatives looks really weird and I can't be bothered to fix it. Have fun!
I could have made it draw while generating the table but what the heck.
Turing: |
%Graphing and whatnot
%graph settings, adjust these to see the graph better
const yMax := 100 %More of a minumum, really. the graph will show AT LEAST this much.
const xMax := 700 % Dito
const xMin := 0 %x value to start the table at
const xScale := 0. 001 %X will count by this. Scale of 0.1 will count 0, 0.1, 0.2, ... Big = fast, small = accurate
const xOffset := 30 %shifts the graph around. Haven't really bothered
const yOffset := 20
var font := Font.New ("Times New Roman:12")
type coord :
record
x : real
y : real
end record
var table : flexible array xMin .. xMin of coord
function y (n : real) : real
% if n = 0 then %uncomment this if you may have division by 0 and adjust the conditional to your needs
% result 0
%end if
result sind (n )* 50+ 50 %Replace with your formula, n is parameter
end y
proc setTable
table (xMin ).x := xMin
table (xMin ).y := y (xMin )
for x : xMin .. round (xMax/xScale )
new table, upper (table ) + 1
table (upper (table )).x := x*xScale
table (upper (table )).y := y (x*xScale )
end for
end setTable
proc drawStuff
% for x: 1..xMax by round (600/(xMax))
% Font.Draw (intstr (round(x*xScale)), round (5*x*xScale)+ xOffset, round (yOffset/2), font, black)
%end for
% for y: 1..yMax by round (
Font.Draw (intstr (xMax ), 590, yOffset - 20, font, black)
Font.Draw (intstr (yMax ), xOffset - 30, 340, font, black)
Font.Draw ("0", xOffset- 20, yOffset- 20, font, black)
Draw.Line (20, yOffset, 600, yOffset, black)
Draw.Line (xOffset, 20, xOffset, 350, black)
for n : xMin .. upper (table )- 1
Draw.Line (round (table (n ).x* (600/xMax ) + xOffset ), round (table (n ).y* (350/yMax ) + yOffset ), round (table (n+ 1).x* (600/xMax ) + xOffset ), round (table (n+ 1).y* (350/yMax )+yOffset ), black)
end for
end drawStuff
setTable
drawStuff
|
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Cezna

|
Posted: Fri Jun 25, 2010 8:18 pm Post subject: RE:Graph-er |
|
|
Very cool, but I didn't really understand how to use it...
I was looking for the one line I would have to change and put my formula in, but didn't find it.
Anyway, this looks great (you should post it in that topic with that guy looking for a good graphing calculator for university, since I think you already posted there)
lol |
|
|
|
|
 |
Insectoid

|
Posted: Fri Jun 25, 2010 8:31 pm Post subject: RE:Graph-er |
|
|
replace everything after 'result' in code: | result sind (n)*50+50 |
with your equation. If you get a 'division by 0' error, just uncomment the 3 lines above and change the 'if n = 0' to whatever value is causing the fault.
This doesn't have even close to the functionality that you need for university. Those calculators can derive functions and calculate all sorts of things that I wouldn't have the first clue how to do in code. All this does is graph simple functions. |
|
|
|
|
 |
andrew.
|
Posted: Fri Jun 25, 2010 8:44 pm Post subject: RE:Graph-er |
|
|
That's pretty cool. It made me feel like making one of my own. I hope you don't mind that I took the structure of your code.
Turing: |
var xMax : int := 500
var yMax : int := 400
var yScl : int := 50
var xScl : int := 1
var font : int := Font.New ("Arial:10")
setscreen("graphics:"+ intstr(xMax )+ ","+ intstr(yMax )+ ",offscreenonly")
fcn calculate (n : int) : real
var x : real := 1/xScl*n
result yScl* (sind(x )) %function
end calculate
proc draw ()
drawline(0, yMax div 2, xMax, yMax div 2, black)
drawline(xMax div 2, 0, xMax div 2, yMax, black)
Font.Draw("0", xMax div 2 - 10, yMax div 2 - 15, font, black)
for i : -xMax div 2..xMax div 2
drawline(i + xMax div 2, round(calculate (i )) + yMax div 2, i + 1 + xMax div 2, round(calculate (i + 1)) + yMax div 2, black)
View.Update
end for
end draw
draw
|
|
|
|
|
|
 |
Insectoid

|
Posted: Fri Jun 25, 2010 9:13 pm Post subject: RE:Graph-er |
|
|
Yeah, that works a lot faster. I'm working right now on making it show the x and y of the list at the mouse X, but it's a lot harder than I thought due to scaling (if the mouse X is at 70, due to scaling the actual position in the graph might be 500). |
|
|
|
|
 |
Insectoid

|
Posted: Fri Jun 25, 2010 10:26 pm Post subject: RE:Graph-er |
|
|
So, yeah, I got the mouse bit semi-working. X is always xx.029 with the current scale (0.001) but w/e.
Just add Turing: |
var mx, my, mb : int
loop
Mouse.Where (mx, my, mb )
if mx > xMin and mx < xMax then
Draw.FillBox (10, maxy- 25, maxx, maxy, white)
Font.Draw ("Value: " + realstr (table (round (mx/xScale )+xOffset ).x, 5) + ", " + realstr (table (round (mx/xScale )+xOffset ).y, 5), 15, maxy - 20, font, black)
end if
end loop
| to the bottom of the file. |
|
|
|
|
 |
andrew.
|
Posted: Fri Jun 25, 2010 10:51 pm Post subject: RE:Graph-er |
|
|
Okay, so I was bored (again) and I decided to try to expand it a bit. This one uses Newton's method to approximate roots, and can find numerical derivatives by using first principles and two close points.
Turing: | var xMax : int := 500
var yMax : int := 400
var yScl : real := 0. 5
var xScl : real := 5
var font : int := Font.New ("Arial:10")
fcn func (x : real) : real
result x** 2 - 4
end func
fcn diff (x : real) : real
if x = 0 then
result (func (x + (0. 0000001 * (1 / (10 ** 7)))) - func (x )) / (0. 0000001 * (1 / (10 ** 7)))
else
result (func (x + (x * (1 / (10 ** 7)))) - func (x )) / (x * (1 / (10 ** 7)))
end if
end diff
fcn root (initial : real) : real
% Uses Newton's Method
var n : real := initial
for i : 1 .. 10
if diff (n ) = 0 then
n := n - (func (n )) / (1 / (10 ** 9))
else
n := n - (func (n )) / (diff (n ))
end if
end for
result n
end root
fcn calculate (n : int) : real
var x : real := 1 / xScl * n
result yScl*func (x )
end calculate
proc plot ()
var window : int := Window.Open("graphics:" + intstr (xMax ) + "," + intstr (yMax ) + ",offscreenonly")
drawline (0, yMax div 2, xMax, yMax div 2, black)
drawline (xMax div 2, 0, xMax div 2, yMax, black)
Font.Draw ("0", xMax div 2 - 10, yMax div 2 - 15, font, black)
for i : -xMax div 2 .. xMax div 2
drawline (i + xMax div 2, round (calculate (i )) + yMax div 2, i + 1 + xMax div 2, round (calculate (i + 1)) + yMax div 2, black)
View.Update
end for
end plot
put "f(x) = x\^2 - 4"
put "One root is: ", root (1)
put "The derivative at x = 5 is: ", diff (5)
plot
|
|
|
|
|
|
 |
Insectoid

|
Posted: Fri Jun 25, 2010 10:54 pm Post subject: RE:Graph-er |
|
|
Quit stealing my thunder! It's mine! My own! My precious... |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
andrew.
|
Posted: Fri Jun 25, 2010 11:00 pm Post subject: RE:Graph-er |
|
|
Haha, sorry Insectoid. I was really bored. I'll stop now. |
|
|
|
|
 |
|
|