Use Variables to Create Function

In summary, Dan helped Silva figure out how to create a function/expression based on several variables. The relations that the variables should follow are a ≥ x ≥ b > 0, a ≥ 1 ≥ b > 0, m ≥ s ≥ 0, m ≥ p ≥ 0, and when s = p then x = 1, when p = m and s = 0 then x = a, when s = m and p = 0 then x = b. x can be shown as a function using these variables using an exponential relation.
  • #1
Silva1
5
0
Hi everyone.

I'm currently trying to create a function/expression based on several variables. I've so far figured out the rules that the variables should follow but I'm struggling to put them together into a formula. I'm hoping that someone here might tell me if this is even possible, and give me some advice or direct me somewhere specific I can read about my problem (I haven't studied any maths in some time and I don't recall an exact name for what I'm trying to do).

To give my variables names I'll be using: a, b, m, p, s, x
The relations I'd like to have are:

Code:
a ≥ x ≥ b > 0
a ≥ 1 ≥ b > 0
m ≥ s ≥ 0
m ≥ p ≥ 0
when s = p then x = 1
when p = m and s = 0 then x = a
when s = m and p = 0 then x = b

I'd like to show x as a function using these variables.

Thanks in advance for any help. :D
 
Last edited:
Mathematics news on Phys.org
  • #2
Silva said:
Hi everyone.

I'm currently trying to create a function/expression based on several variables. I've so far figured out the rules that the variables should follow but I'm struggling to put them together into a formula. I'm hoping that someone here might tell me if this is even possible, and give me some advice or direct me somewhere specific I can read about my problem (I haven't studied any maths in some time and I don't recall an exact name for what I'm trying to do).

To give my variables names I'll be using: a, b, m, p, s, x
The relations I'd like to have are:

Code:
a ≥ x ≥ b > 0
a ≥ 1 ≥ b > 0
m ≥ s ≥ 0
m ≥ p ≥ 0
when s = p then x = 1
when p = m and s = 0 then x = a
when s = m and p = 0 then x = b

I'd like to show x as a function using these variables.

Thanks in advance for any help. :D
It might be possible to do it from here but it might be easier if we knew what we were trying to achieve. Would this be some kind of polynomial? An exponential? A sinusoidal? Can you tell us the whole problem?

-Dan
 
  • #3
Hi Dan, thanks for your response. I'll try my best to explain what I'm doing. I'm extending a game engine to implement a system where the players character has a particular parameter, while other characters have a similar parameter. These parameter influences the effect of a skill/ability.

x will determine the multiplier that's applied to the skill/ability.
a is the maximum the multiplier can be (maximum for x)
b is the minimum the multiplier can be (minimum for x)
s is one of the character's parameter value
p is the other character's parameter value
m is the maximum the parameter value can be
As p increases relative to s, x should increase. If p > s, x > 1. If p < s, x < 1. If p = s, m = 1.

I think an exponential best suits this, rate of change for p-s (I think).
 
  • #4
I've managed to solve this by creating a new variable, x, and defining it as m+s-p, thank you, Dan, for helping me see that I could do this. I created a quadratic regression formula using the variables to calculate known values, ran them together as simultaneous equations to find an expression for the first "constant", substituted this into one of the original formulas to find an expression for the second constant. I'm not sure it consistently meets all the rules I'd defined but from the tests I've done it seems to be fine.

The end result should anyone be interested:

View attachment 8910
 

Attachments

  • TheSolve.gif
    TheSolve.gif
    1.6 KB · Views: 83
  • #5
Hi Silva, welcome to MHB!

Let's define $P=\frac p m$ and $S=\frac s m$ to simplify the relations.
And let's call our function $x(P,S)$.
Then we have:
\begin{cases}x(1,0)=a\\ x(0,1)=b\\ x(q,q)=1&\text{for any }q
\end{cases}

The simplest relation would be a linear relation in $P$ and $S$, but we can see that it won't work, since it will put a restriction on $a$ and $b$ that is undesired.

Let's try a polynomial of degree 2:
$$x(P,S)=AP^2+BS^2+CPS +DP+ES+F$$
Then we have:
$$\begin{cases}x(1,0)=A+D+F=a\\
x(0,1)=B+E+F=b\\
x(q,q)=(A+B+C)q^2+(D+E)q+F=1
\end{cases}\implies
\begin{cases}A+D+F=a\\
B+E+F=b\\
A+B+C=0\\
D+E=0\\
F=1
\end{cases}$$
If we pick $D=E=0$, we get:
$$
\begin{cases}A=a-1\\
B=b-1\\
C=2-a-b\\
D=E=0\\
F=1
\end{cases}\implies
x(P,S)=(a-1)P^2+(b-1)S^2+(2-a-b)PS+1
$$

So we can use:
$$x(p,s)=(a-1)\left(\frac pm\right)^2+(b-1)\left(\frac sm\right)^2+(2-a-b)\frac{ps}{m^2}+1$$

EDIT: I was writing this before I saw you posted again.
 
  • #6
Silva said:
I've managed to solve this by creating a new variable, x, and defining it as m+s-p, thank you, Dan, for helping me see that I could do this.
Ummmm... You're welcome? I'm glad you got it, but I doubt I did any thing. (Yes)

-Dan
 
  • #7
Hi Klaas van Aarsen, glad to be here.

Thanks for finding a solution for this. I've tested your solution and it's not quite functioning as I'd intended (nor is mine after testing that further). It works correctly when s = p, when s = 0 and p = m, when p = 0 and s = m. The issue I have with both mine and your solution is that when s = m and p is slightly more than 0 then the result doesn't always meet this condition:

a ≥ x ≥ b > 0

As x < b. Using your equation this condition is only satisfied when:

1 - b ≥ a - 1
so, a = 1.7, b = 0.3 is okay but a = 1.8, b = 0.3 is not.

Similarly for my equation this condition is only satisfied when:

(1 - b) * 3 ≥ a - 1

Can you think of any way we can solve so that the condition a ≥ x ≥ b ≥ 0 is always met, or is this not possible?

Dan, had you not asked what kind of equation I wanted I wouldn't have known what my options were for solving it. Credit where credit's due. :)
 

FAQ: Use Variables to Create Function

What is the purpose of using variables to create a function?

Variables allow us to store and manipulate data in a function. This makes our code more flexible and reusable, as we can change the value of a variable without having to rewrite the entire function.

How do you declare a variable in a function?

To declare a variable in a function, we use the keyword "var" followed by the name of the variable and an equal sign. For example: var num = 5;

Can a variable be used in different functions?

Yes, a variable can be used in different functions as long as it is declared within the scope of those functions. This means that the variable must be declared inside the function or passed in as a parameter.

Can a function return a variable?

Yes, a function can return a variable by using the "return" keyword followed by the variable name. This allows us to use the value of the variable in other parts of our code.

What is the difference between global and local variables?

Global variables are declared outside of a function and can be accessed from anywhere in the code. Local variables are declared inside a function and can only be accessed within that function. Local variables have a limited scope, while global variables have a global scope.

Similar threads

Replies
7
Views
1K
Replies
13
Views
2K
Replies
12
Views
585
Replies
2
Views
1K
Replies
7
Views
2K
Replies
1
Views
2K
Back
Top