Just started a new course (data struct) and having some trouble with a question:
For what F(n) function is this true?
My direction is that it should happen when The N exponent is 1 or less, because that will match the definition of thetha as the function will be closer and bounded by C1*F(n) and C2*F(n), but im not sure about that. Thanks!
Notice that
0n + 1n + 2n + ... + n·n
= n(0 + 1 + 2 + ... + n)
= n(n(n+1)/2)
with that last step following from Gauss's sum. Therefore, the summation is Θ(n3).
I'm studying the same course right now, and I think that if you apply the next rule it might work out.
lim n->inf f(x) / g(x) = c ,
Related
I've been tasked with finding the answers for the questions below, but honestly I'm completely lost as where to start. I'm not looking for straight answers per say (although they would be appreciated), but rather how I can find/derive them. I understand recursion, I just don't understand how to find a recurrence equation.
a.) Give the recurrence for the expected running time of RANDOM.
b.) Give the exact recurrence equation for the expected number of recursive calls executed by a call to RANDOM(n).
c.) Give the exact recurrence equation for the expected number of times the rerun statements on line 14 is executed, in all called to RANDOM(n), recursive or not.
Pseudocode:
Function RANDOM(u)
if u = 1 then
return(1)
else
assign x=0 with probability 1/2, or
assign x=1 with probability 1/3, or
assign x=2 with probability 1/6
if x=0 then
return(RANDOM(u-1) + RANDOM(u-2))
end-if
if x=1 then
return(RANDOM(u) + 2*RANDOM(u-1))
end-if
if x=2 then
return(3*RANDOM(u) + RANDOM(u) + 3)
end-if
end-if
end-RANDOM
First of all it is important to note that, since the questions ask for run time / no. of calls, the coefficients in front of the recursive calls to RANDOM don't matter (because none of the answers depend on the actual return value).
Also, since the questions ask for expected quantities, you can mix the appropriate recursive calls probabilistically.
a)
Starting off quite easy. Probabilistic mixing of functions:
T(u) = [1/2] * [T(u-1) + T(u-2)] +
[1/3] * [T(u) + T(u-1)] +
[1/6] * [T(u) + T(u) ] // + constant amount of work
b)
Same as before, but remember to add one for each call:
N(u) = [1/2] * [N(u-1) + N(u-2) + 2] +
[1/3] * [N(u) + N(u-1) + 2] +
[1/6] * [N(u) + N(u) + 2] // no constants here
c)
This is trickier than the other two. It seems contradictory that the question asks for "[all calls to RANDOM(u)] whether recursive or not", but only the ones on line 14 which are recursive...
Anyways ignoring this minor detail, the key thing to note is that the call to RANDOM(u) on line 11 can also produce the required recursive calls, without contributing to the total count itself. Adapting the above:
R(u) = [1/3] * [R(u) ] + // don't add 1 here
[1/6] * [R(u) + R(u) + 2] // add 2 as before
Note that the question probably expects you to rearrange all of the T(u), N(u), R(u) terms to the LHS. I'll leave that to you since it is trivial.
I was taking a derivative in Mathematica and the result containted the term Sqrt', and I was wondering what the ' on the end ment? I believe in means 1/Sqrt from doing the derivative by hand but if someone could confirm this is how the result is displayed I would appreciate it. Here is my input and output.
In f[p_] := cSqrt[(m^2)*(c^2) + (p - eA/c)^2] + e*phi
In f'[p]
Out 2 (-(eA/c) + p) Derivative[1][cSqrt][c^2 m^2 + (-(eA/c) + p)^2]
Best,
Ben
This may help:
http://blog.wolfram.com/2011/05/20/mathematica-qa-three-functions-for-computing-derivatives/
Apparently ' is the standard shorthand notation for the derivative.
Given sum of A and B let S
i have to find maximum product of A*B
but there is one condition value of A will be in range [P,Q]
How to proceed for it ?
If there is no range then task is pretty simple.
Using derivation method .
How to find Maximum product for eg.
A+B=99
value of A will be range [10,20]
then what will be the maximum product of A and B.
O(N) will not sufficient for the problem
Clearly, B = S - A and you need to maximize A * (S - A).
You know from algebra that A * (S - A) achieves a maximum when A = S / 2.
If S / 2 falls in the allowed range [P Q], then the maximum value is A^2 / 4.
Otherwise, by monotonicity the maximum value is reached at one of the bounds and is the largest of P * (S - P) and Q * (S - Q).
This is an O(1) solution.
This is actually a maths question, nothing to do with programming
Even if you are given it as a programming question, You should first understand it mathematically.
You can think of it geometrically as "if the perimeter of my rectangle is fixed at S, how can I achieve the maximum area?"
The answer is by making sides of equal length, and turning it into a square. If you're constrained, you have to get as close to a square as possible.
You can use calculus to show it formally:
A+B = S, so B = S-A
AB therefore = A(S-A)
A is allowed to vary, so write it as x
y = x(S-x) = -x^2 + Sx
This is a quadratic, its graph Will look like an upsidedown parabola
You want the maximum, so you're looking for the top of the parabola
dy/dx = 0
-2x + S = 0
x = S/2
A better way of looking at it would be to start with our rectangle Pq = A, and say P is the longer edge.
So now make it more oblique, by making the longer edge P slightly longer and the shorter edge q slightly shorter, both by the same amount, so P+q does not change, and we can show that the area goes down:
Pq = A
(P+delta) * (q-delta)
= Pq + (q-P)*delta + delta^2
= A + (q-P)delta
and I throw away the delta^2 as it vanishes as delta shrinks to 0
= A + (something negative)*delta
= A - something positive
i.e. < A
Using code FullSimplify[Abs[q + I*w], Element[{q, w}, Reals]] results in
Abs[q + I w]
and not
Sqrt[q^2 + w^2]
What am I missing?
P.S. Assuming[{q \[Element] Reals, w \[Element] Reals},
Abs[q + I*w]] does not work either.
Note: Simplify[Abs[w]^2, Element[{q, w}, Reals]] and Simplify[Abs[I*q]^2, Element[{q, w}, Reals]] work.
The problem is that what you assume to be "Simple" and what MMA assumes to be simple are two different things. Taking a look at ComplexityFunction indicates that MMA primarily looks at "LeafCount". Applying LeafCount gives:
In[3]:= Abs[q + I w] // LeafCount
Out[3]= 8
In[4]:= Sqrt[q^2 + w^2] // LeafCount
Out[4]= 11
So, MMA considers the Abs form to be better. (One can visually explore the simplicity using either TreeForm or FullForm). What we need to do is tell MMA to treat MMA as more expensive. To do this, we take the example from ComplexityFunction and write:
In[7]:= f[e_] := 100 Count[e, _Abs, {0, Infinity}] + LeafCount[e]
FullSimplify[Abs[q + I w], Element[{q, w}, Reals],
ComplexityFunction -> f]
Out[8]= Sqrt[q^2 + w^2]
As requested. Basically, we are telling MMA through f[e] that the count of all parts of the form Abs should count as 100 leaves.
EDIT: As mentioned by Brett, you can also make it more general, and use _Complex as the rule to look for:
In[20]:= f[e_] := 100 Count[e, _Complex, {0, Infinity}] + LeafCount[e]
FullSimplify[Abs[q + I w], Element[{q, w}, Reals],
ComplexityFunction -> f]
Out[21]= Sqrt[q^2 + w^2]
I suggest using ComplexExpand, which tells the system that all variables are real.
In[28]:= Abs[q + I*w] // ComplexExpand
Out[28]= Sqrt[q^2 + w^2]
These comments are not helpful. Mathematica is failing to evaluate complex numbers, as in Abs[5+i20] is left unchanged. The i is coded correctly. Making abstract observations about 'what is or is not simple' is unrelated and wrong. There is a float that should result, not some algebra. N and ImportForm do not work, either.
Does anyone know how to minimize a function containing an integral in MATLAB? The function looks like this:
L = Int(t=0,t=T)[(AR-x)dt], A is a system parameter and R and x are related through:
dR/dt = axRY - bR, where a and b are constants.
dY/dt = -xRY
I read somewhere that I can use fminbnd and quad in combination but I am not able to make it work. Any suggestions?
Perhaps you could give more details of your integral, e.g. where is the missing bracket in [AR-x)dt]? Is there any dependence of x on t, or can we integrate dR/dt = axR - bR to give R=C*exp((a*x-b)*t)? In any case, to answer your question on fminbnd and quad, you could set A,C,T,a,b,xmin and xmax (the last two are the range you want to look for the min over) and use:
[x fval] = fminbnd(#(x) quad(#(t)A*C*exp((a*x-b)*t)-x,0,T),xmin,xmax)
This finds x that minimizes the integral.
If i didn't get it wrong you are trying to minimize respect to t:
\int_0^t{(AR-x) dt}
well then you just need to find the zeros of:
AR-x
This is just math, not matlab ;)
Here's some manipulation of your equations that might help.
Combining the second and third equations you gave gives
dR/dt = -a*(dY/dt)-bR
Now if we solve for R on the righthand side and plug it into the first equation you gave we get
L = Int(t=0,t=T)[(-A/b*(dR/dt + a*dY/dt) - x)dt]
Now we can integrate the first term to get:
L = -A/b*[R(T) - R(0) + Y(T) - Y(0)] - Int(t=0,t=T)[(x)dt]
So now all that matters with regards to R and Y are the endpoints. In fact, you may as well define a new function, Z which equals Y + R. Then you get
L = -A/b*[Z(T) - Z(0)] - Int(t=0,t=T)[(x)dt]
This next part I'm not as confident in. The integral of x with respect to t will give some function which is evaluated at t = 0 and t = T. This function we will call X to give:
L = -A/b*[Z(T) - Z(0)] - X(T) + X(0)
This equation holds true for all T, so we can set T to t if we want to.
L = -A/b*[Z(t) - Z(0)] - X(t) + X(0)
Also, we can group a lot of the constants together and call them C to give
X(t) = -A/b*Z(t) + C
where
C = A/b*Z(0) + X(0) - L
So I'm not sure what else to do with this, but I've shown that the integral of x(t) is linearly related to Z(t) = R(t) + Y(t). It seems to me that there are many equations that solve this. Anyone else see where to go from here? Any problems with my math?