# Graphs questions

11 replies to this topic

### #1 mireazma

mireazma

GMC Member

• GMC Member
• 282 posts

Posted 07 May 2011 - 08:05 PM

Hello.
I came to see that in my games, no matter how hard I try to avoid using graphs, they're chillin' in a corner and from time to time they're like waiting for me to say "Ok, Ok I need you. Satisfied now?"
So I have to deal with them and start embracing them already.
Ok. My math level is like from secondary school but I'll be studying more from now on.
The general idea is that most of the times I have the graph in mind (several pairs of points) and need to find the function for it.
I know how to play with linear functions, how to offset them up/down and left/right, or how to make them steeper/milder. I could even draw something with graph lines.
The problem arises when it comes about non-linear graphs, like polynomial, exponential, logarithmic, trigonometric etc. Here I know nothing.

So I'm making an appeal to whoever is willing to help me, please tell me if there's a place anywhere to discuss about graphs and I (very noob, as I was saying) could learn some techniques about them.

On with the questions at hand:

1. How would I know what kind of function I need for a certain graph shape?

I can plug in the pairs of points into some best fit finder software but it gives back some weird complicated functions which I can't manipulate to alter the offset or shape. So if I'd want to nudge it to the right a bit, I wouldn't know what values to change in the function. If I plug the new pairs of points more to the right, it throws me a different function.
So I want to find it manually, from 2 pairs of points(Yes, I know they should be 3 for a non-linear)representing the general slope of the function, starting from a more general class of functions. Knowing the class mechanism, I'd work a bit on each of the coefficients and I'd get a simple function to pass through the 2 points and as close as possible by the middle curving point, like a bezier curve in a paint program.
The curving point precision shouldn't be more important than the overall function simplicity.

2. Can a non-linear graph function be edited in such a way that the graph is
A. mirrored
B. flipped
...?

3. Here is an example of a graph I'd like to find the function of:

It's the pink one.
I don't ask you for the specific function for the graph; instead please tell me how to approach the problem knowing these:
A. it's symmetrical by the yellow line.
B' it has a vertical asymptote at x=0 and a horizontal one at y=0.
OR
B" it may cross the axes further to negative x and y, at 0,8 and 8,0 respectively.
C the curvature shape doesn't matter (it may be very well a quarter of a circle)
D. I'd like to easily edit the function so based on it, I get new ones for the red, green, blue and light blue ones.

My long guess tells me that it should be a simple function. I'd say logarithmic for the pink one but what about editing it to become blue or light blue? So I guess it's not log.
• 0

### #2 YellowAfterlife

YellowAfterlife

GMC Member

• Global Moderators
• 3493 posts
• Version:GM:Studio

Posted 07 May 2011 - 08:30 PM

For ease, try power(i, n) where i is (0..1).
Check n's in range (0.1 .. 10). You'll sure find each of your functions there.
Though, first, check sqrt() and sqr().
• 0

### #3 xshortguy

xshortguy

GMC Member

• Global Moderators
• 4186 posts
• Version:GM:Studio

Posted 07 May 2011 - 09:46 PM

1. How would I know what kind of function I need for a certain graph shape?

Nonlinear fitting is hard to do in general. In fact, there is an entire section of statistics devoted to this: regression analysis.

If you only care about getting a curve that passes through n points on a small region, one can find an approximating polynomial. Given n points, one can find a unique polynomial that passes through each of these points. Here's one method on how to do this:

0. Have data of the form $(x_i, y_i)$ for a finite number of elements.

1. Pick your basis elements. In this case, we are working with the basis $\{1, x, x^2, ..., x^{n-1}\}$. What this means is that we are trying to fit our data to elements of the form $a_0 + a_1 x + a_2 x^2 + ... + a_{n-1} x^{n-1}$, which is often referred to as a linear combination of the basis elements.

2. Construct a matrix so that the elements of each column are the values of your $x_i$'s evaluated at each of the corresponding basis vectors.

3. We then have a matrix equation $A x = y$. To solve this, we'll use the method of least squares fitting: $A^T A x = A^T y \Rightarrow x = (A^T A)^{-1} A^T y$

Example:
Fit the following points to a degree two polynomial: (0, 3), (1, 6), (2, 11). (This should give us $y = x^2 + 2x + 3$.)

We will use the basis $\{1, x, x^2\}$.

The matrix will be:
$\\ A = \begin{pmatrix} 1 & 0 & 0 \\ 1 & 1 & 1 \\ 1 & 2 & 4 \end{pmatrix} \\ A^T = \begin{pmatrix} 1 & 1 & 1 \\ 0 & 1 & 2 \\ 0 & 1 & 4 \end{pmatrix} \\ A^T A = \begin{pmatrix} 3 & 3 & 5 \\ 3 & 5 & 9 \\ 5 & 9 & 17 \end{pmatrix} \\ (A^T A)^{-1} = \begin{pmatrix} 1 & -\frac{3}{2} & \frac{1}{2} \\ -\frac{3}{2} & \frac{13}{2} & -3 \\ \frac{1}{2} & -3 & \frac{3}{2} \end{pmatrix} \\ (A^T A)^{-1} A^T = \begin{pmatrix} 1 & 0 & 0 \\ -\frac{3}{2} & 2 & -\frac{1}{2} \\ \frac{1}{2} & -1 & \frac{1}{2} \end{pmatrix} \\ (A^T A)^{-1} A \begin{pmatrix} 1 \\ 4 \\ 9 \end{pmatrix} = \begin{pmatrix} 3 \\ 2 \\ 1 \end{pmatrix}$

The the first row of A corresponded to the constant term, so the constant term is 3. The second row corresponded to x, so the coefficient in front of x is 2, and the third row corresponded to the x^2, so the coefficient in front of the x^2 is 1. Y = x^2 + 2x + 3.

====

As for the pink line in your picture, that is characteristic of a 1/x graph. So you can look at basis elements of {1/x, 1, x} for example.
• 1

### #4 chance

chance

GMC Member

• Reviewer
• 5770 posts
• Version:GM:Studio

Posted 08 May 2011 - 12:00 PM

The problem arises when it comes about non-linear graphs, like polynomial, exponential, logarithmic, trigonometric etc. Here I know nothing.

Then spend a little time playing with other functions, and you'll get a feel for which one is appropriate for a given circumstance. Chances are you don't need a precise fit, just a reasonable approximation.

Check out a function grapher on the web. There's tons of them. Here's one my kids have used:
http://www.mathsisfun.com/data/grapher-equation.html

It handles all the basic algebraic functions like roots, powers, trig, exponential, logarithmic, etc. Just type y = whatever_function(x) and see the result. Then you can play with the function arguments, weighting factors, or combining various functions.
• 1

### #5 xshortguy

xshortguy

GMC Member

• Global Moderators
• 4186 posts
• Version:GM:Studio

Posted 08 May 2011 - 05:21 PM

$\begin{array}{|c|l|} \hline \begin{pmatrix} x \\ y \end{pmatrix} & \textnormal{Normal coordinates in terms of some basis} \\ \hline \begin{pmatrix} x + m \\ y \end{pmatrix} & \textnormal{Coordinates shifted by m units in x-direction} \\ \hline \begin{pmatrix} x \\ y + n \end{pmatrix} & \textnormal{Coordinates shifted by n units in y-direction} \\ \hline \begin{pmatrix} x \cos{\theta} - y \sin{\theta} \\ x \sin{\theta} + y \cos{\theta} \end{pmatrix} & \textnormal{Coordinates rotated around the origin by angle }\theta \\ \hline \begin{pmatrix} (x - m) \cos{\theta} - (y - n) \sin{\theta} + m \\ (x - m) \sin{\theta} + (y - n) \cos{\theta} + n\end{pmatrix} & \textnormal{Coordinates rotated around the point (m, n) by angle } \theta \\ \hline \end{array}$

These rules should get you any type of graph transformation that you would ever need, since you can combine them. (The last one is a combination of the three above it.) Keep in mind that these transformations actually change the coordinate system, and not the coordinates. So if you want to view these coordinates in your old system, simply do the opposite. For example, to shift a graph to the right, you want to use x - m instead of x + m. A good way to think about this is:
$\begin{pmatrix} x' \\ y' \end{pmatrix} = \begin{pmatrix} x + m \\ y + n \end{pmatrix}$
The primed coordinates will be the new coordinate system you are working in. To translate back to the old coordinates, we simply solve for x, y:
$\begin{pmatrix} x' - m \\ y' - n \end{pmatrix} = \begin{pmatrix} x \\ y \end{pmatrix}$

Now for any function f(x), we can simply replace y with f(x) in the coordinates.
• 1

### #6 mireazma

mireazma

GMC Member

• GMC Member
• 282 posts

Posted 08 May 2011 - 09:26 PM

Thank you all very much for the help. Now I have plenty stuff to chew on. I wanted to reply just as a thanks giving as it'll take some time to actually pertinently respond to each of your posts.
Special thanks to xshortguy who's been replying to my posts since I can't remember, even when I think it's hard to get an answer.
• 0

### #7 xDanielx

xDanielx

GMC Member

• GMC Member
• 1001 posts
• Version:Unknown

Posted 23 May 2011 - 12:50 AM

If you have a simple curve in your mind and you want to turn it into code, polynomial fitting probably isn't the best option. Here are a few reasons:
• You have to write out a bunch of points, of course. If you need to change the curve slightly, you might need to repeat the process.
• If you approximate a simple function with a high-degree polynomial, you get verbose and inefficient code.
• Overfitting. If you give n points to a polynomial regression tool, you will get a perfect fit for the points that you specified, but it might look like this.
• Underfitting. If you use too low degree, the resulting curve will be off. This is a trade-off that you have to make if you only have some points and don't know what the whole curve should look like, but in your case the trade-off can be avoided.

For most purposes, you can just start with a simple function like 1/x or 1-x and plot it using a tool like Wolfram Alpha (or Mathematica, if you have access to it). Then tweak it until you get what you want. You can move a curve horizontally by adding a constant to each "x" in your equation, or move it vertically by adding just one constant to the whole equation. You can scale horizontally by multiplying each "x" in the equation by a constant, or scale vertically by multiplying the whole equation by a constant. Changing curvature can usually be accomplished by adding/changing exponents.

If you need the function to pass through a couple specific points, you can devise a more general formula and then solve for some constants. For example, say you want something that looks something like x^2 but passes through (10, 10). You could type this in Wolfram Alpha: "solve y=c*x^2, x=10, y=10". It will give you back "c = 1/10", indicating that x^2/10 is the function you want.
• 0

### #8 xshortguy

xshortguy

GMC Member

• Global Moderators
• 4186 posts
• Version:GM:Studio

Posted 23 May 2011 - 02:47 AM

You can use the method of least squares (post 3) to find "best fit" polynomials of a certain degree to match the data; granted it may be unlikely that any of your points will fit that curve, its still a good approximation in many cases.
• 0

### #9 DerrenFreeman

DerrenFreeman

GMC Member

• New Member
• 16 posts

Posted 31 May 2011 - 09:23 PM

Just passing through, and I felt it needed commenting, that those are some of the best replies to a question I have ever seen on a forum xshortguy.

Anyway, I feel I need to contribute:

http://en.wikipedia....ange_polynomial <- This is a way to estimate a polynomial from a given set of points.

It takes a while to get your head around, so I'd suggest Googling about more for a better explanation.

Edited by DerrenFreeman, 31 May 2011 - 09:24 PM.

• 1

### #10 numbers

numbers

GMC Member

• New Member
• 460 posts

Posted 21 June 2011 - 06:59 PM

In your case, I think x^n+y^n=8^n is probably what you're looking for. Varying n will do what you want. 0<n<1 will make it closer to the pink one, n=1 will be the straight line and n>1 will be the bulging ones. There is no general method for doing what you want, but when you said 'quarter circle' you were ridiculously close. You noticed that circles are vertical and horizontal at the right places, and symmetrical about the line y=x. The symmetry, just so you know, is there because switching the values of x and y would give you the same equation. Just fool around with things you know, seeing what transformations do, until you get what you're looking for. As far as I'm aware, the best (and only) way to really do this is practice. I'm sure there are others solutions you might like better, but I thought this one was easier to grasp.
• 0

### #11 mireazma

mireazma

GMC Member

• GMC Member
• 282 posts

Posted 22 June 2011 - 09:27 AM

Although I've started to (seriously) learn math for some time now, I realize I'm yet so far from being able to fluently work with equations. It's like I know the theory but can't put it into practice, that is, I can't put it to work with what I need.
So, I guess I'll postpone this till I'll be able to comfortably juggle with math principles. I didn't want to mention, so I wouldn't make a fool out of myself, but I never operated with matrices and I'm even having (slight) difficulties with negative and rational powers(!) due to lack of exercises . I thought I could fast forward math and get to making games. Now when I see myself in the situation of putting game making aside for quite a while to study math, it's very discouraging. But I suppose I could build them up in parallel. Don't even know why I'm writing these, maybe because of the envy I can feel for those who manipulate math like a ninja fights with nunchaku

OK. Back to the topic
I tried power(i,n) but it wasn't symmetrical. It wasn't obvious to me then as it is now.
I found to work best with Graph 4.3 as it's the simplest graph soft I found that does what I needed so far.
Honestly, I think I won't use polynomial functions in this particular case due to nature of the graph. Indeed, I can see that using a polynomial (especially) is a complexity-precision trade. I'll remember "Wolfram Alpha" to try.
I also tried f(x)=(8^n - x^n)^(1/n), starting from x^n+y^n=8^n. I must be doing something wrong as I get an asymmetrical graph. So good to have learned the x=y line symmetry principle but what am I missing?

As a conclusion to this particular case, (with the care for not ignoring the others) the closest "approaches" are of the (user) numbers as thought around symmetry and the deceivingly simple and yet "fitty" 1/x suggested by xshortguy.
I'd go with the 1/x but I can't figure what to do with it further. I tried changing 1 to a higher value and it goes towards the green line but I got the (math nooby) feeling that it won't get to be straight (albeit curved the other way), no matter what. I'll try and use xDanielx guidelines to change the curvature.

Edited by mireazma, 22 June 2011 - 09:33 AM.

• 0

### #12 numbers

numbers

GMC Member

• New Member
• 460 posts

Posted 15 July 2011 - 07:43 AM

1/x will never go straight, since no matter what constant goes at the top, it will always go to infinity as x goes to zero from the right, and go to zero as x goes to infinity. I got a symmetrical graph for f(x)=(8^n-x^n)^(1/n). Make sure that you take note of how your graphing program is scaling the axes. They may be scaling unequally, giving the impression that the graph is asymmetrical.

When trying to find functions that fit shapes i want, I always look for:
1. Key points i want the function to pass through.
2. Symmetries. Typically around y=x, or one of the axes.
3. Limits/Asymptotes. What causes various kinds, and how to recognize them.

Keep note of a few things:
shifts and stretches. If your function needs to be stretched and moved, there are general methods for this. learn these.
Inverses and limits.
For example (open WolframAlpha for this one): I wanted a function that assigned every value of x a unique number in (-1, 1). Couldn't find one I liked. So, since I'm more familiar with vertical asymptotes and domain restrictions, I looked for a function that was valid from (-1,1) and had a range of (-inf, inf). I already knew that a semicircle (y=(1-x^2)^.5) was valid from (-1,1), so I started with that. At least the domain is right. Now I know the right side goes to zero, and I want it to go to infinity. Knowing what I know about 1/x (as I said at the beginning of this post), I did just that. The function (y=1/(1-x^2)^.5) has a range of [1,inf). Now I recognized that I had an even function. An even function is where replacing x with -x yields the same equation (and so the same value for y, making it symmetrical across the y axis). An easy way to correct this (and get that (-inf, inf) range) would be to make it an odd function. An odd function is where f(-x)=-f(x). In other words, what's on one half of the function is the negative of what's on the other half (rotational symmetry about the origin). and for functions, even*odd=odd. So I just had to multiply the function I had with an odd function, the simplest of which is y=x. and true enough, y=x/(1-x^2)^.5 has a range of (-inf,inf) and a domain of (-1,1). awesome. But remember, I wanted the inverse of this. I want to swap the domain and range. this is done by exchanging the x and y variables and solving for y. This can only be done if the function doesn't pass over the same y value twice (which this one doesn't). When all's said and done, I got y=x/(x^2+1)^.5 as my function, and it does what I wanted! yay!

NOTE: There are probably easier ways to arrive at this answer (please post any if you have them guys), but I figured this was more thorough. Also, there are much simpler answers (y=atan(x) springs to mind), but again, I wanted this to go through more ideas about how functions can be categorized and manipulated. This is just as much of a guess and check thing as it seems, but the key is to find the tools you need to make educated guesses. I hope this wall of text helps somehow.
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users