The space complexity of Fibonacci Sequence - space-complexity

I saw some textbook about the worst-case space complexity of Fibonacci Sequence. However, I have the following question:

You can start with a concrete example and generalize. Start with n = 5.
S(5) = S(4) + c
= (S(3) + c) + c
= ((S(2) + c) + c) + c
= (((S(1) + c) + c) + c) + c
= S(1) + 4c
There are 4 c's when n = 5. In general, there are n-1 c's.

Related

What is the time complexity of a recursive algorithm given by the recurrence T(n) = 2T(n-1) + 1

I am completely new to this so a step by stem would help greatly, thank you.
First, you can start with iteration method to understand how this behaves.
T(n) = 2T(n-1) + 1 =
= 2*(2T(n-2) + 1) + 1 = 4T(n-2) + 3
= 4(2T(n-3) + 1) + 3 = 8T(n-3) + 7
= 8*(2T(n-4) + 1) + 7 = 16T(n-4) + 15
= 16*(2T(n-5) + 1) + 15 = 32T(n-5) + 31
Now, that we understand the behavior, we can tell
T(n) = 2^i * T(n-i) + (2^i - 1)
Now, we need to use the base clause (which is not given in question), and extract for i=n. For example, if T(0) = 0:
T(n) = 2^n * T(0) + (2^n - 1) = 2^n - 1
This is in O(2^n), when calculating asymptotic complexity.
Note: Iteration method is good and easy to follow - but it is not a formal proof. To formally prove the complexity, you are going to need another tool, such as induction.

Index exceeds the number of array elements (1)

I trying to use matlab to solve a polynomial with variable parameters. I am using fsolve. The code is given below:
function my_fsolve3()
% Constant Parameters
A = 6.24;
B = 5.68e-5;
C = 1.7e-3;
D = 6.55e-8;
E = 5.3e-8;
F = 9.46e-1;
t = [0;600;1200;1800;2400;3000;10200;17400;24600;31800;...
39000;46200;53400;60600;67800;75000;82200;89400;96600;103800;...
111000;118200;125400;132600;139800;147000;154200;161400;168600;175800;183000];
G = [0.00;4.88E-06;4.88E-06;5.11E-06;5.11E-06;5.35E-06;5.35E-06;5.36E-06;5.36E-06;5.07E-06;....
5.07E-06;4.93E-06;5.30E-06;5.30E-06;5.30E-06;9.46E-06;9.46E-06;1.13E-05;1.15E-05;1.10E-05;...
1.10E-05;1.04E-05;1.04E-05;1.04E-05;1.03E-05;1.04E-05;1.06E-05;1.13E-05;1.13E-05;1.13E-05;1.03E-05];
H = [0.00;3.34E-01;6.79E-01;1.04E+00;1.41E+00;6.00E+00;1.07E+01;1.56E+01;2.07E+01;2.59E+01;...
3.14E+01;3.67E+01;4.18E+01;4.66E+01;5.09E+01;5.51E+01;5.90E+01;6.23E+01;6.56E+01;6.87E+01;...
7.12E+01;7.36E+01;7.59E+01;7.78E+01;7.95E+01;8.11E+01;8.24E+01;8.24E+01;8.23E+01;8.21E+01;8.20E+01];
I = [0.00;4.88E-06;4.88E-06;5.11E-06;5.11E-06;5.35E-06;5.35E-06;5.36E-06;5.36E-06;5.07E-06;...
5.07E-06;4.93E-06;5.30E-06;5.30E-06;5.30E-06;9.46E-06;9.46E-06;1.13E-05;1.15E-05;1.10E-05;...
1.10E-05;1.04E-05;1.04E-05;1.04E-05;1.03E-05;1.04E-05;1.06E-05;1.13E-05;1.13E-05;1.13E-05;1.03E-05];
J = [1.78E-07;7.41E-06;9.33E-06;1.20E-05;1.05E-05;1.74E-05;3.72E-05;3.55E-05;1.00E-04;4.07E-02;...
2.45E-01;6.17E-01;1.32E+00;2.29E+00;2.34E+00;2.40E+00;1.82E+00;1.38E+00;2.09E+00;1.82E+00;...
1.58E+00;2.29E+00;1.62E+00;1.12E+00;8.91E-01;8.51E-01;7.59E-01;8.71E-01;1.12E+00;1.00E+00;8.51E-01];
K = [9.75;8.13;8.03;7.92;7.98;7.76;7.43;7.45;7.00;4.39;...
3.61;3.21;2.88;2.64;2.63;2.62;2.74;2.86;2.68;2.74;...
2.8;2.64;2.79;2.95;3.05;3.07;3.12;3.06;2.95;3.00;3.07];
for i = 1:length(t)
s(i) = fsolve(#(x) x(i) + 2.* G(i) - ((H(i).* A.*x(i))/(x(i).^2 + A.*x(i) + A.*B))- 2.*((H(i).*A.*B)/(x(i).^2 ...
+ A.*x(i) + A.*B))-((I(i).*C.*x(i))/(x(i).^2 + C.*x(i) + C.*D))- 2.*((I(i).*C.*D)/(x(i).^2 ...
+ C.*x(i) + C.*D))- E/x(i), F);
end
L = 6 - log10(s);
plot(t,L)
hold on
plot (t,K,'v')
xlabel('Time (sec)')
ylabel('pH')
hold off
legend('pH-Mod','pH-Exp')
end
I do not know how can I ensure that the index value does not exceed the number of the array elements. In line 34, I indexed to the end of the independent variable, thinking that it will prevent the error. The error message is
Index exceeds the number of array elements (1).
Error in
my_fsolve3>#(x)x(i)+2.*G(i)-((H(i).*A.*x(i))/(x(i).^2+A.*x(i)+A.*B))-2.*((H(i).*A.*B)/(x(i).^2+A.*x(i)+A.*B))-((I(i).*C.*x(i))/(x(i).^2+C.*x(i)+C.*D))-2.*((I(i).*C.*D)/(x(i).^2+C.*x(i)+C.*D))-E/x(i)
(line 36)
s(i) = fsolve(#(x) x(i) + 2.* G(i) - ((H(i).* A.*x(i))/(x(i).^2 + A.*x(i) + A.*B))- 2.*((H(i).*A.*B)/(x(i).^2 ...
Error in fsolve (line 242)
fuser = feval(funfcn{3},x,varargin{:});
Error in my_fsolve3 (line 36)
s(i) = fsolve(#(x) x(i) + 2.* G(i) - ((H(i).* A.*x(i))/(x(i).^2 + A.*x(i) + A.*B))- 2.*((H(i).*A.*B)/(x(i).^2 ...
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
Your anonymous function has x as an argument which is initialized to the scalar F.
You try to access x(i) which fails for i>1 because x is scalar. Solution: replace x(i) by x.
s(i) = fsolve(#(x) x + 2.* G(i) - ((H(i).* A.*x)/(x.^2 + A.*x + A.*B))- 2.*((H(i).*A.*B)/(x.^2 ...
+ A.*x + A.*B))-((I(i).*C.*x)/(x.^2 + C.*x + C.*D))- 2.*((I(i).*C.*D)/(x.^2 ...
+ C.*x + C.*D))- E/x, F);
By the way, you do not need the dot operator in multiplications of scalars, a simple * will do in your code.

is log(n!) equivalent to nlogn? (Big O notation)

In my book there is a multiple choice question:
What is the big O notation of the following function:
n^log(2) +log(n^n) + nlog(n!)
I know that log(n!) belongs to O(nlogn), But I read online that they are equivalent. how is log(n!) the same thing as saying nlogn?
how is:
log(n!) = logn + log(n-1) + ... + log2 + log1
equivalent to nlogn?
Let n/2 be the quotient of the integer division of n by 2. We have:
log(n!) = log(n) + log(n-1) + ... + log(n/2) + log(n/2 - 1) + ... + log(2) + log(1)
>= log(n/2) + log(n/2) + ... + log(n/2) + log(n/2 - 1) + ... + log(2)
>= (n/2)log(n/2) + (n/2)log(2)
>= (n/2)(log(n) -log(2) + log(2))
= (n/2)log(n)
then
n log(n) <= 2log(n!) = O(log(n!))
and n log(n) = O(log(n!)). Conversely,
log(n!) <= log(n^n) = n log(n)
and log(n!) = O(n log(n)).

Complexity classes examples

I wanted to know if my answers are indeed correct for the following statements:
3(n^3) + 5(n^2) + 25n + 10 = BigOmega(n^3) -> T
->Grows at a rate equals or slower
3(n^3) + 5(n^2) + 25n + 10 = Theta(n^3) -> T
-Grows at a rate exactly equals
3(n^3) + 5(n^2) + 25n + 10 = BigO(n^3) -> T
-Grows at a rate equals or faster
Thanks!!!
Formal definitions of O notations are:
f(n) = O(g(n)) means that there exists some constant c and n0 that
f(n) <= c*g(n) for n >= n0.
f(n) = Ω(g(n)) means that there exists some constant c and n0 that
f(n) >= c*g(n) for n >= n0.
f(n) = Θ(g(n)) means that there exists some constants c1 and c2 and n0
that f(n) >= c1*g(n) and f(n) <= c2*g(n) for n >= n0.
Proof for O:
3(n^3) + 5(n^2) + 25n + 10 < 3*n^3 + n^3 + n^3 = 5*n^3
You can see that for n >= 10 this formula is true.
So there exists c = 5 and n0 = 10, thus it is O(n^3).
Proof for Ω:
3(n^3) + 5(n^2) + 25n + 10 > 3*n^3
So c = 3 and n0 = 1, thus it is Ω(n^3).
Because both O and Ω apply then the 3rd statement for Θ is also true.

textbox values won't assign to a variable in vba

I was running tests on my software today and found that some of the values it was producing weren't correct.
I decided to step through the code and noticed that the variables I had assigned to textbox values on my userform when hovered over said empty, even though when hovering over the textbox assigned to it, the value inputted by the user showed.
For Example,
n = BiTimeSteps_TextBox.Value
when hovered over
n = empty
even though
BiTimeSteps_TextBox.Value = 2
when hovered over.
So say I have a formula shortly after that says
d = n*2 ,
n when hovered over says empty and d is made 0 when it shouldn't be.
Someone told me if I switch it around to
BiTimeSteps_TextBox.Value = n
it should be recognised but it is still not.
What could possibly be causing this?
See full code below: (it aims to price options using the binomial tree pricing method)
S = BiCurrentStockPrice_TextBox.Value
X = BiStrikePrice_TextBox.Value
r = BiRisk_Free_Rate_TextBox.Value
T = BiexpTime_TextBox.Value
Sigma = BiVolatility_TextBox.Value
n = BiTimeSteps_TextBox.Value
Dim i, j, k As Integer
Dim p, V, u, d, dt As Double
dt = T / n ' This finds the value of dt
u = Exp(Sigma * Sqr(dt)) 'formula for the up factor
d = 1 - u 'formula for the down factor
'V value of option
'array having the values
Dim bin() As Double 'is a binomial arrays, it stores the value of each node, there is a loop
'work out the risk free probability
p = (1 + r - d) / (u - d)
'probability of going up
ReDim bin(n + 1) As Double
'it redims the array, and n+1 is used because it starts from zero
'------------------------------------------------------------------------------------------------------------------------------
''European Call
If BiCall_CheckBox = True Then
For i = 0 To n 'payoffs = value of option at final time
bin(i + 1) = Application.WorksheetFunction.Max(0, (u ^ (n - i)) * (d ^ i) * S - X)
'It takes the max payoff or 0
Cells(i + 20, n + 2) = bin(i + 1) 'to view payoffs on the isolated column on the right
Next i
End If
'european put
If BiPut_CheckBox = True Then
For i = 0 To n 'payoffs = value of option at final time
bin(i + 1) = Application.WorksheetFunction.Max(0, X - (S * (u * (n - i)) * (d * i)))
' European Put- It takes the max payoff or 0
Cells(i + 20, n + 2) = bin(i + 1) 'to view payoffs on the isolated column on the right
Next i
End If
For k = 1 To n 'backward column loop
For j = 1 To (n - k + 1) 'loop down the column loop
bin(j) = (p * bin(j) + (1 - p) * bin(j + 1)) / (1 + r)
Cells(j + 19, n - k + 2) = bin(j)
'' print the values along the column, view of tree
Next j
Next k
Worksheets("Binomial").Cells(17, 2) = bin(1) ' print of the value V
BiOptionPrice_TextBox = bin(1)