Determine a function f(n) that describes number of times a statement is executed - discrete-mathematics

How can I determine a function f(n) describing number of times the statement x = x + 2 is executed in terms of n in the following algorithm?
input j
x = 0,
for i: j to j^2 do
x: = x + 1
Note: I'm not looking for run time.

f(n) = n^2-n
essentially just directly calculating x

Related

How to determine the result of a code without running it

If I am given the code below, how can I tell what the resulting y-value would be. I apologize if this is a simple question but I find these types of questions very difficult.
For foo(-1,10)
function y = foo(x, a)
for k=-1:0
b=x-k;
while (x > -2) && (x < 2)
x=x+a+1;
end
end
y = b + x;
end
When running the programme I can see that b=10 but I don't understand how you get that. I would appreciate if someone could make this clearer for me.
Thank you!
Start from the top:
foo(x, a) has two parameters: x and a
foo(-1, 10) would mean that x = -1 and a = 10.
From there go down each line.
b = x - k would start out as b = -1 + (the value of k on that current iteration of the loop
Then you would do the same for the while loop.
x = -1 + 10 + 1
So,
x = 10
Now take that value and plug it into the while loop condition:
(10 > -2) and (10 < 2)
Is this condition true? No. So you move on to the next iteration of the for loop
At the end you set y equal to the value you got for b + the value you got for x

vectorizing function in octave / matlab

I have a function that I was wondering if it was possible to vectorize it and not have to use a for loop. The code is below.
a=1:2:8
for jj=1:length(a)
b(jj)=rtfib(a(jj)); %fibbonacci function
end
b
output below:
a =
1 3 5 7
>>>b =
1 3 8 21
I was trying to do it this way
t = 0:.01:10;
y = sin(t);
but doing the code below doesn't work any suggestions?
ps: I'm trying to keep the function rtfib because of it's speed and I need to use very large Fibonacci numbers. I'm using octave 3.8.1
a=1:2:8
b=rtfib(a)
Here's the rtfib code below as requested
function f = rtfib(n)
if (n == 0)
f = 0;
elseif (n==1)
f=1;
elseif (n == 2)
f = 2;
else
fOld = 2;
fOlder = 1;
for i = 3 : n
f = fOld + fOlder;
fOlder = fOld;
fOld = f;
end
end
end
You can see that your function rtfib actually computes every Fibonacci number up to n.
You can modify it so that is stores and returns all these number, so that you only have to call the function once with the maximum number you need:
function f = rtfib(n)
f=zeros(1,n+1);
if (n >= 0)
f(1) = 0;
end
if (n>=1)
f(2)=1;
end
if (n >= 2)
f(3) = 2;
end
if n>2
fOld = 2;
fOlder = 1;
for i = 3 : n
f(i+1) = fOld + fOlder;
fOlder = fOld;
fOld = f(i+1);
end
end
end
(It will return fibonnaci(n) in f(n+1), if you don't need the 0 you could change it so that it returns fibonnaci(n) in f(n) if you prefer)
Then you only need to call
>>f=rtfib(max(a));
>>b=f(a+1)
b =
1 3 8 21
If you don't want to store everything you could modify the function rtfib a little more, so that it takes the the array a as input, compute the Fibonacci numbers up to max(a) but only stores the one needed, and it would directly return b.
Both solutions will slow down rtfib itself but it will be a lot faster than calculating the Fibonacci numbers from 0 each time.

Counting Number of Specific Outputs of a Function

If I have a matrix and I want to apply a function to each row of the matrix. This function has three possible outputs, either x = 0, x = 1, or x > 0. There's a couple things I'm running into trouble with...
1) The cases that output x = 1 or x > 0 are different and I'm not sure how to differentiate between the two when writing my script.
2) My function isn't counting correctly? I think this might be a problem with how I have my loop set up?
This is what I've come up with. Logically, I feel like this should work (except for the hiccup w/ the first problem I've stated)
[m n] = size(matrix);
a = 0; b = 0; c = 0;
for i = 1 : m
x(i) = function(matrix(m,:));
if x > 0
a = a + 1;
end
if x == 0
b = b + 1;
end
if x == 1
c = c + 1;
end
end
First you probably have an error in line 4. It probably should be i instead of m.
x(i) = function(matrix(i,:));
You can calculate a, b and c out of the loop:
a = sum(x>0);
b = sum(x==0);
c = sum(x==1);
If you want to distinguish x==1 and x>0 then may be with sum(xor(x==1,x>0)).
Also you may have problem with precision error when comparing double values with 0 and 1.

Matlab: How to solve the system of nonlinear equations with additional parameters?

I would like to create a function that finds the parameters p and q of Bass diffusion model, given the data of two time periods.
The model (equation) is the following:
n(T) = p*m + (q-p)*n(T-1) + q/m*n(T-1)^2
where
n(T) = number of addoptions occuring in period T
n(T-1) = number of cumulative adoptions that occured before T
p = coefficient of innovation
q = coefficient of imitation
m = number of eventual adopters
for example if m = 3.000.000
and the data for the years below is the following:
2000: n(T) = 820, n(T-1) = 0
2005: n(T) = 25000, n(T-1) = 18000
then the following equation system has to be solved (in order to determine the values of p and q):
p*m + (q-p)*0 + q/3.000.000 * 0^2 == 820
p*m + (q-p)*18000 + q/3.000.000 * 18000^2 == 25000
By following Matlab documentation I tried to create a function Bass:
function F = Bass(m, p, q, cummulativeAdoptersBefore)
F = [p*m + (q-p)*cummulativeAdoptersBefore(1) + q/m*cummulativeAdoptersBefore(1).^2;
p*m + (q-p)*cummulativeAdoptersBefore(2) + q/m*cummulativeAdoptersBefore(2).^2];
end
Which should be used in fsolve(#Bass,x0,options) but in this case m, p, q, cummulativeAdoptersBefore(1), and cummulativeAdoptersBefore(2) should be given in x0 and all variables would be considered as unknown instead of just the latter two.
Does anyone know how to solve the system of equations such as above?
Thank you!
fsolve() seeks to minimize the function you supply as argument. Thus, you have to change your equations to
p*m + (q-p)*0 + q/3.000.000 * 0^2 - 820 == 0
p*m + (q-p)*18000 + q/3.000.000 * 18000^2 - 25000 == 0
and in Matlab syntax
function F = Bass(m, p, q, cumulativeAdoptersBefore, cumulativeAdoptersAfter)
F = [p*m + (q-p)*cumulativeAdoptersBefore(1) ...
+ q/m *cumulativeAdoptersBefore(1).^2
- cumulativeAdoptersAfter(1);
p*m + (q-p)*cumulativeAdoptersBefore(2) ...
+ q/m *cumulativeAdoptersBefore(2).^2
- cumulativeAdoptersAfter(2)];
end
Note: There is a typo in your Bass function (multiplication instead of sum).
Now you have a function, which takes more parameters than there are unkowns.
One option is to create an anonymous function, which only takes the unknowns as arguments and to fix the other parameters via a closure.
To fit the unkowns p and q, you could use something like
cumulativeAdoptersBefore = [0, 1800];
cumulativeAdoptersAfter = [820, 25000];
m = 3e6;
x = [0, 0]; %# Probably, this is no good starting guess.
xopt = fsolve(#(x) Bass(m, x(1), x(2), cumulativeAdoptersBefore, cumulativeAdoptersAfter), x0);
So fsolve() sees a function taking only a single argument (a vector with two elements) and it also returns a vector value.

sum the first n prime reciprocals such that the sum exceeds k (Matlab)

I am trying to write a program in matlab, such that the sum of the reciprocals of the n first prime numbers exceeds a given value k. To clearify, I am trying to make a function
SumPrime(k)
And it is supposed to return an integer n such that
\sum_{i=1}^{n} 1/p_i > k
sum of primes and reciprocals of them and plot in matlab?
I tried looking here, but this does not quite answer my question. Neither did the command
sumInversePrimes = sum(1./primes(n));
Here is my attempt. First i define a function for finding the n`th prime number.
function Y = NthPrime(n)
if n==1
Y = 2;
return
end
if n < 1 || round(n)~=n
return
end
j = 2;
u = 0;
while u < n
T = primes(j);
u = numel(T);
j = 1 + j;
end
Y = T(numel(T));
After doing this (lengthy?) code for finding the n`th prime number, the rest is a cakewalk.
function Y = E(u)
sum = 0
n = 0
while sum < u
n = n + 1
sum = sum + 1/( NthPrime(n) )
end
Y = n;
Return the proper values. This somewhat works. Alas it is very slow, and I guess this is very bad code. I have merely started learning coding in matlab, Could someone please help me either write a better code or optimize mine ?
XOXOXOX
Nebby
Here's how to precompute the sums then find the first that exceeds a threshold:
>> p = primes(1000);
>> cs = cumsum(1./p);
>> find(cs > 1.8, 1)
ans = 25