Smooth damp or tween algorithm - algorithm

I would like to know an algorithm for smooth damp or as some people call it, tween. I would like it in Lua preferably but anything will help.
I have tried watching unity tutorials but can't transfer the code without a algorithm to substitute for the smooth damp function.

If I understand the question correctly, you're looking for an easing function. There is a Lua library that provides a set of easing functions on GitHub: https://github.com/EmmanuelOga/easing
An example would be:
local function inOutQuad(t, b, c, d)
t = t / d * 2
if t < 1 then
return c / 2 * pow(t, 2) + b
else
return -c / 2 * ((t - 1) * (t - 3) - 1) + b
end
end
Where t = time, b = begin value, c = change in value, and d = duration.
More information on these easing functions is available directly from Robert Penner here (this is where the function above is derived from): http://www.robertpenner.com/easing/

Related

PineScript Question - How to test calculations/debugging

I'm currently converting an old Basic program into Pine Script. In Basic it is easy to output variable's values to test they are correct, using the PRINT statement. Would you be able to advise how I can see the values of calculations in Pine Script, please? Here is an example:
fn_z_to_v(c,u) =>
u = abs(c)
z3 = int(u)
q = int(z3 / 30) + 1
z7 = int(fnw((z3 / 30 - int(z3 / 30)) * 30))
It should be noted that these are intermediate calculations and are not intended for display in a chart. Their values would make no sense in the context of time or price.
How might I see the values of u, z3, q and z7 (assuming I correctly pass the values c and u to the function and that the function fnw is correctly formed)?
Thanks,

Integral changes variable subscripts

I have been away from Mathematica for quite a while and am trying to fix some old notebooks from v4 that are no longer working under v11. I'm also a tad rusty.
I am attempting to use functional minimization to fit a polynomial of variable degree to an arbitrary function (F) given a starting guess (ao) and domain of interest (d). Note that while F is arbitrary, its nature is such that the integral of the product of F and a polynomial (or F^2) can always be evaluated algebraically.
For the sake of example, I'll use the following inputs:
ao = { 1, 2, 3, 4 }
d = { -1, 1 }
F = Sin[x]
To do so, I create an array of 'indexed' variables
polyCoeff = Array[a,Length[a],0]
Result: polycoeff = {a[0], a[1], a[2], a[3]}
I then create the polynomial itself using the following
genPoly[{},x_] := 0
genPoly[a_List,x_] := First[a] + x genPoly[Rest[a],x]
poly = genPoly[polyCoeff,x]
Result: poly = a[0] + x (a[1] + x (a[2] + x a[3]))
I then define my objective function as the integral of the square of the error of the difference between this poly and the function I am attempting to fit:
Q = Integrate[ (poly - F[x])^2, {x, d[[1]],d[[2]]} ]
result: Q = 0.545351 - 2. a[0.]^2 + 0.66667 a[1.]^2 + .....
And this is where things break down. poly looks just as I expected: a polynomial in x with coefficients that look like a[0], a[1], a[2], ... But, Q is not exactly what I expected. I expected and got a new polynomial. But not the coefficients contained a[0.], a[1.], a[2.], ...
The next step is to create the initial guess for FindMinimum
init = Transpose[{polyCoeff,ao}]
Result: {{a[0],1},{a[1],2},{a[3],3},{a[4],4}}
This looks fine.
But when I make the call to FindMinimum, I get an error because the coefficients passed in the objective (a[0.],a[1.],...) do not match those passed in the initial guess (a[0],a[1],...).
S = FindMinimum[Q,init]
So I think my question is how do I keep Integrate from changing the arguments to my coefficients? But, I am open to other approaches as well. Keep in mind though that this is "legacy" work that I really don't want to have to completely revamp.
Thanks much for any/all help.

envelope function (spatstat) - error "unused arguments"

I would like to ask your help for finding the reason why when I use the function envelope, my arguments are not accepted, but defined "unused arguments".
The data I'm using are ppp without marks and I would like to create a L function graph with simulated data and my data.
Here the code for my ppp data:
map2008MLW = ppp(xy2008_BNGppp$x, xy2008_BNGppp$y, window = IoM_polygon_MLWowin)
And then:
L2008 = Lest(map2008MLW,correction="Ripley")
OP = par(mar=c(5,5,4,4))
plot(L2008, . -r ~ r, ylab=expression(hat("L")), xlab = "d (m)"); par(OP)
L2008$iso = L$iso - L$r
L2008$theo = L$theo - L$r
Desired number of simulations
n = 9999
Desired p significance level to display
p = 0.05
And at this point the envelope function doesnt seem very happy:
EL2008 = envelope(map2008MLW[W], Lest, nsim=n, rank=(p * (n + 1)))
Error in envelope(map2008MLW[W], Lest, nsim = n, rank = (p * (n + 1))) :
unused arguments (nsim = n, rank = (p * (n + 1)))
It seems a generic error and I am not sure it is caused by the package spatstat. Please, help me in finding a solution to this, as I can't proceed with my analyses.
Thank you very much,
Martina
The argument rank should be nrank.
Also the relationship between the significance level and the argument nrank is not correct in the example. For a two-sided test, the significance level is alpha = 2 * nrank/(nsim+1), so nrank= alpha * (nsim+1)/2.
You have chosen a significance level of 0.95 but I assume you mean 0.05. So with nsim=9999 you want nrank=0.05 * 10000/2 = 250 to get a test with significance level 0.05.
Such a large number of simulations (9999) is unnecessary in this kind of application. Monte Carlo tests are valid with small values of nsim. In your example I would normally use nsim=39 and nrank=1.
See Chapter 10 of the spatstat book.

Derivative of sigmoid

I'm creating a neural network using the backpropagation technique for learning.
I understand we need to find the derivative of the activation function used. I'm using the standard sigmoid function
f(x) = 1 / (1 + e^(-x))
and I've seen that its derivative is
dy/dx = f(x)' = f(x) * (1 - f(x))
This may be a daft question, but does this mean that we have to pass x through the sigmoid function twice during the equation, so it would expand to
dy/dx = f(x)' = 1 / (1 + e^(-x)) * (1 - (1 / (1 + e^(-x))))
or is it simply a matter of taking the already calculated output of f(x), which is the output of the neuron, and replace that value for f(x)?
Dougal is correct. Just do
f = 1/(1+exp(-x))
df = f * (1 - f)
The two ways of doing it are equivalent (since mathematical functions don't have side-effects and always return the same input for a given output), so you might as well do it the (faster) second way.
A little algebra can simplify this so that you don't have to have df call f.
df = exp(-x)/(1+exp(-x))^2
derivation:
df = 1/(1+e^-x) * (1 - (1/(1+e^-x)))
df = 1/(1+e^-x) * (1+e^-x - 1)/(1+e^-x)
df = 1/(1+e^-x) * (e^-x)/(1+e^-x)
df = (e^-x)/(1+e^-x)^2
You can use the output of your sigmoid function and pass it to your SigmoidDerivative function to be used as the f(x) in the following:
dy/dx = f(x)' = f(x) * (1 - f(x))

Converting an algerberic expression into a function?

I have been trying to create an expression to calculate brick and glass prices.
I'm working with a, b and c
so the price is $30m^2 for bricks and $20m^2 for glass
A and B are walls C is a round window radius
A = 3m (not m^2)
B = 2m (not m^2)
C = 1m (not m^2)
I believe that my expression (a*b)*30 – (c^2*20) works but how can I turn this into a java script function that in future I could use in a calculator to calculate prices etc...
Bit of a newbie with java script.
thanks all!
I believe the following function cost(a,b,c) should do what you want, assuming I've understood the question:
function cost(a,b,c) {
return (a * b * 30) - (Math.PI * Math.pow(c,2) * 20)
}
This returns 117.16814692820414 given the parameters a = 3, b = 2 and a = 1.
An example of the cost function in use would be:
var price = cost(3,2,1);

Resources