How to solve this equation? - math

I am creating a web shop and have to calculate how much to charge the customer to make sure that the fees are cancelled. The payment system adds the fees and I want the customer to pay them.
The fees are 2.45% and € 1.10.
This means that if the customer shops for € 100, and I report that value to the payment system, we will only get € 96.45. (100 - (2.45 + 1.1)).
That is not good.
How do I calculate the right value to send to the payment system, so that we get € 100?
It is not just to say € 100 + 2.45% + € 1.1 = € 103.55 and report this to the payment system. Because then the payment system will say
€ 103.55 - ((2.45% of 103.55) + 1.1)
€ 103.55 - (2,536975 + 1.1)
€ 103.55 - 3,636975
€ 99,913025
and that is, obviously, not correct.
So how do I calculate what to send to the payment system to get the desired value?
I have come so far, that it is the following equation:
X - (X * 0.0245) - 1.10 = Y
Here, X is the desired amount to send to the payment system and Y is the amount the customer has shopped for (100), therefore:
X - (X * 0.0245) - 1.10 = 100
But how do I solve that to find out what X is?
Thanks in advance

Wolfram Alpha will solve this for you. I'm working on a more programmatic solution now.
Your equation X - (X * 0.0245) - 1.10 = Y was accurate. Let's simplify this as follows:
X - (X * 0.0245) - 1.10 = Y
X - 0.0245 * X - 1.10 = Y
(1 - 0.0245) * X - 1.10 = Y
0.9755 * X = Y + 1.10
X = (Y + 1.10)/0.9755
Per your definition, X is the desired amount, and Y is the amount the customer pays. This equation gives you Y based on X. If one of my steps is unclear, let me know.

You just have to walk through it:
X - (X * 0.0245) - 1.10 = 100
X - (X * 0.0245) = 100 + 1.10
X (1 - 0.0245) = 101.10
101.10 / x = 1 - 0.0245
101.10 = (1 - 0.0245) * x
101.10 / (1 - 0.0245) = x
x = 103.639159
But like Steven Xu said Wolfram Alpha is your friend when you want to solve math problems.

x - 0.0245x = 101.1
(1 - 0.0245)x = 101.1
x = 101.1 / (1 - 0.0245)
x = 103.639

X - (X * 0.0245) - 1.10 = Y
X - (X * 0.0245) = Y + 1.10
X * (1 - 0.0245) = Y + 1.10
X = (Y + 1.10) / (1 - 0.0245) = (Y + 1.10) / 0.9755

I am not sure if you are serious, but if you are:
X - (X * 0.0245) - 1.10 = 100
-> 101.10 - 0.9755*x = 0
-> 101.1/0.9755 = x
-> x = 103,5366
Is there any particular programming language that you want to use ? (not that this makes to much of a difference)
btw:
Great answer Steven Xu!

Here's some Math videos:
http://www.khanacademy.org
Updated note: The K12-level Math question seems slightly offtopic on stackoverflow, it's not related to the programming profession. The videos are high-quality training in really basic math problems such as this one ... including percentages and basic algebra

Perhaps I've missed something throughout the discussion but are we really evaluating the correct equation in the first place?
The fees are 2.45% and €1.10. Adding those fees to a €100 order would be.
subtotal = €100
grandtotal = subtotal*(1 + 0.0245) + €1.1 = €103.55
= subtotal + subtotal*0.0245 + €1.1 = €103.55
This yields an equation of sub*(1 + pct) + flat = tot. Solving for sub:
sub*(1 + pct) + flat = tot
sub*(1 + pct) = tot - flat
sub = (tot - flat) / (1 + pct)
or distributing sub first
sub + sub*pct + flat = tot
sub + sub*pct = tot - flat
sub*(1 + pct) = tot - flat
sub = (tot - flat) / (1 + pct)
In the end it yields the same equation sub = (tot - flat) / (1 + pct). Therefore solving for the subtotal given a grand total:
grandtotal = €103.55
subtotal = (grandtotal - €1.1) / (1 + 0.0245) = €100
Did I miss something?

Related

Find maximum angle of box in slot

How would I find the maximum possible angle (a) which a rectangle of width (W) can be at within a slot of width (w) and depth (h) - see my crude drawing below
Considering w = hh + WW at the picture:
we can write equation
h * tan(a) + W / cos(a) = w
Then, using formulas for half-angles and t = tan(a/2) substitution
h * 2 * t / (1 - t^2) + W * (1 + t^2) / (1 - t^2) = w
h * 2 * t + W * (1 + t^2) = (1 - t^2) * w
t^2 * (W + w) + t * (2*h) + (W - w) = 0
We have quadratic equation, solve it for unknown t, then get critical angle as
a = 2 * atan(t)
Quick check: Python example for picture above gives correct angle value 18.3 degrees
import math
h = 2
W = 4.12
w = 5
t = (math.sqrt(h*h-W*W+w*w) - h) / (W + w)
a = math.degrees(2 * math.atan(t))
print(a)
Just to elaborate on the above answer as it is not necessarly obvious, this is why why you can write equation:
h * tan(a) + W / cos(a) = w
PS: I suppose that the justification for "why a is the maximum angle" is obvious

Solving the recurrence equation T(n) = 3 + m * T(n - m)

I have a Computer Science Midterm tomorrow and I need help determining the complexity of a particular recursive function as below, which is much complicated than the stuffs I've already worked on: it has two variables
T(n) = 3 + mT(n-m)
In simpler cases where m is a constant, the formula can be easily obtained by writing unpacking the relation; however, in this case, unpacking doesn't make the life easier as follows (let's say T(0) = c):
T(n) = 3 + mT(n-m)
T(n-1) = 3 + mT(n-m-1)
T(n-2) = 3 + mT(n-m-2)
...
Obviously, there's no straightforward elimination according to these inequalities. So, I'm wondering whether or not I should use another technique for such cases.
Don't worry about m - this is just a constant parameter. However you're unrolling your recursion incorrectly. Each step of unrolling involves three operations:
Taking value of T with argument value, which is m less
Multiplying it by m
Adding constant 3
So, it will look like this:
T(n) = m * T(n - m) + 3 = (Step 1)
= m * (m * T(n - 2*m) + 3) + 3 = (Step 2)
= m * (m * (m * T(n - 3*m) + 3) + 3) + 3 = ... (Step 3)
and so on. Unrolling T(n) up to step k will be given by following formula:
T(n) = m^k * T(n - k*m) + 3 * (1 + m + m^2 + m^3 + ... + m^(k-1))
Now you set n - k*m = 0 to use the initial condition T(0) and get:
k = n / m
Now you need to use a formula for the sum of geometric progression - and finally you'll get a closed formula for the T(n) (I'm leaving that final step to you).

Avoid round off error in exponential calculation

Due to rounding error cannot get ratio between two numbers:
Ratio=exp(x)/(exp(x)+exp(y)) such that x=-1.11e4 and y=-1.12e4.
Any mathematical or computational trick to do?
You can simplify it like this:
R = exp(x) / (exp(x) + exp(y))
= exp(x) / (exp(x) * (1 + exp(y) / exp(x)))
= 1 / (1 + exp(y) / exp(x))
= 1 / (1 + exp(y - x))
(This is the same result as derived by DiltihiumMatrix, but obtained without going into the log domain and back again.)
How about some mathematical manipulation in log-space...
R = exp(x)/[exp(x)+exp(y)]
log(R) = log[exp(x)] - log[exp(x)+exp(y)]
= log[exp(x)] - log[exp(x)*(1+exp(y)/exp(x))]
= log[exp(x)] - log[exp(x)*(1+exp(y-x)]
= log[exp(x)] - log[exp(x)] - log[(1+exp(y-x))]
= - log[(1+exp(y-x))]
Now, exp(y-x) should be a reasonable number, so you can calculate that easily. Then convert back to normal space using R = exp(log(R)).
If that still doesn't work, you can actually taylor expand the last line:
log[(1+z)] ~ 1 + z^2/2 - z^3/3 ...
for small z, in this case z = exp(y-x).

Quadratic Bezier Curve: Calculate t given x

Good day. I am using a Quadratic Bezier Curve with the following configurations:
Start Point P1 = (1, 2)
Anchor Point P2 = (1, 8)
End Point P3 = (10, 8)
I know that given a t, I know I can solve for x and y using the following equation:
t = 0.5; // given example value
x = (1 - t) * (1 - t) * P1.x + 2 * (1 - t) * t * P2.x + t * t * P3.x;
y = (1 - t) * (1 - t) * P1.y + 2 * (1 - t) * t * P2.y + t * t * P3.y;
where P1.x is the x coordinate of P1, and so on.
What I've tried now is that given an x value, I calculate for t using wolframalpha and then I plug that t in to the y equation and I get a my x and y point.
However, I want to automate finding t and then y. I have a formula to get x and y given a t. However, I don't have a formula to get t based on x. I'm a bit rusty with my algebra and expanding the first equation to isolate t doesn't look too easy.
Does anyone have a formula to get t based on x? My google search skills are failing me as of now.
I think it's also worth noting that my Bezier curve faces right.
Any help will be very much appreciated. Thanks.
problem is that what you want to solve is not function in general
for any t is just one (x,y) pair
but for any x there can be 0,1,2,+inf solutions of t
I would do this iteratively
you already can get any point p(t)=Bezier(t) so use iteration of t to minimize distance |p(t).x-x|
for(t=0.0,dt=0.1;t<=1.0;t+=dt)
find all local mins of d=|p(t).x-x|
so when d start rising again set dt*=-0.1 and stop if |dt|<1e-6 or any other threshold. Stop if t is out of interval <0,1> and remember the solution to some list. Restore original t,dt and reset the local min search variables
process all local mins
eliminate all that has bigger distance then some threshold/accuracy compute y and do what you need with the point ...
It is much slower then algebraic approach but you can use this for any curvature not just quadratic
Usually cubic curves are used and do this algebraically with them is a nightmare.
Look at your Bernstein polynomials B[i]; you have...
x = SUM_i ( B[i](t) * P[i].x )
...where...
B[0](t) = t^2 - 2*t + 1
B[1](t) = -2*t^2 + 2*t
B[2](t) = t^2
...so you can rearrange (assuming I did this right)...
0 = (P[0].x - 2*P[1].x + P[2].x) * t^2 + (-2*P[0].x + 2*P[1].x) * t + P[0].x - x
Now you should just be able to use the quadratic formula to find if the solutions for t exist (i.e., are real, not complex), and what they are.
import numpy as np
import matplotlib.pyplot as plt
#Control points
p0=(1000,2500); p1=(2000,-1500); p2=(5000,3000)
#x-coordinates to fit
xcoord = [1750., 2750., 3950.,4760., 4900.]
# t variable with as few points as needed, considering accuracy. I found 30 is good enough
t = np.linspace(0,1,30)
# calculate coordinates of quadratic Bezier curve
x = (1 - t) * (1 - t) * p0[0] + 2 * (1 - t) * t * p1[0] + t * t * p2[0];
y = (1 - t) * (1 - t) * p0[1] + 2 * (1 - t) * t * p1[1] + t * t * p2[1];
# find the closest points to each x-coordinate. Interpolate y-coordinate
ycoord=[]
for ind in xcoord:
for jnd in range(len(x[:-1])):
if ind >= x[jnd] and ind <= x[jnd+1]:
ytemp = (ind-x[jnd])*(y[jnd+1]-y[jnd])/(x[jnd+1]-x[jnd]) + y[jnd]
ycoord.append(ytemp)
plt.figure()
plt.xlim(0, 6000)
plt.ylim(-2000, 4000)
plt.plot(p0[0],p0[1],'kx', p1[0],p1[1],'kx', p2[0],p2[1],'kx')
plt.plot((p0[0],p1[0]),(p0[1],p1[1]),'k:', (p1[0],p2[0]),(p1[1],p2[1]),'k:')
plt.plot(x,y,'r', x, y, 'k:')
plt.plot(xcoord, ycoord, 'rs')
plt.show()

Line/Plane intersection based on points

I have two points in space, L1 and L2 that defines two points on a line.
I have three points in space, P1, P2 and P3 that 3 points on a plane.
So given these inputs, at what point does the line intersect the plane?
Fx. the plane equation A*x+B*y+C*z+D=0 is:
A = p1.Y * (p2.Z - p3.Z) + p2.Y * (p3.Z - p1.Z) + p3.Y * (p1.Z - p2.Z)
B = p1.Z * (p2.X - p3.X) + p2.Z * (p3.X - p1.X) + p3.Z * (p1.X - p2.X)
C = p1.X * (p2.Y - p3.Y) + p2.X * (p3.Y - p1.Y) + p3.X * (p1.Y - p2.Y)
D = -(p1.X * (p2.Y * p3.Z - p3.Y * p2.Z) + p2.X * (p3.Y * p1.Z - p1.Y * p3.Z) + p3.X * (p1.Y * p2.Z - p2.Y * p1.Z))
But what about the rest?
The simplest (and very generalizable) way to solve this is to say that
L1 + x*(L2 - L1) = (P1 + y*(P2 - P1)) + (P1 + z*(P3 - P1))
which gives you 3 equations in 3 variables. Solve for x, y and z, and then substitute back into either of the original equations to get your answer. This can be generalized to do complex things like find the point that is the intersection of two planes in 4 dimensions.
For an alternate approach, the cross product N of (P2-P1) and (P3-P1) is a vector that is at right angles to the plane. This means that the plane can be defined as the set of points P such that the dot product of P and N is the dot product of P1 and N. Solving for x such that (L1 + x*(L2 - L1)) dot N is this constant gives you one equation in one variable that is easy to solve. If you're going to be intersecting a lot of lines with this plane, this approach is definitely worthwhile.
Written out explicitly this gives:
N = cross(P2-P1, P3 - P1)
Answer = L1 + (dot(N, P1 - L1) / dot(N, L2 - L1)) * (L2 - L1)
where
cross([x, y, z], [u, v, w]) = x*u + y*w + z*u - x*w - y*u - z*v
dot([x, y, z], [u, v, w]) = x*u + y*v + z*w
Note that that cross product trick only works in 3 dimensions, and only for your specific problem of a plane and a line.
This is how I ended up doing it in come code. Luckily one code library (XNA) had half of what I needed, and the rest was easy.
var lv = L2-L1;
var ray = new Microsoft.Xna.Framework.Ray(L1,lv);
var plane = new Microsoft.Xna.Framework.Plane(P1, P2, P3);
var t = ray.Intersects(plane); //Distance along line from L1
///Result:
var x = L1.X + t * lv.X;
var y = L1.Y + t * lv.Y;
var z = L1.Z + t * lv.Z;
Of course I would prefer having just the simple equations that takes place under the covers of XNA.

Resources