Subscript out of range in qbasic loop of fibonacci series - qbasic

declare function fibo()
CALL fibo
END
SUB fibo ()
n(1) = 1
n(2) = 1
PRINT n(1); n(2)
FOR i = 3 TO 10
s(i) = n(i - 1) + n(i - 2)
PRINT s(i)
NEXT
END FUNCTION

Do like this:
declare function fibo()
PRINT "fibonacci series"
CALL fibo
END
SUB fibo ()
DIM n(10) AS INTEGER
FOR i = 3 TO 10
n(1) = 1
n(2) = 1
n(i) = n(i - 1) + n(i - 2)
PRINT n(i)
NEXT
END FUNCTION

Related

Sum of the Products of elements of a Vector

I have a vector, say Y2. I want to find the sum of the products up to some order, say 10. A naive way is the following. Is there a nice way to do this in matlab?
for tt=1:length(Y2)-10
LHS(tt) = Y2(tt) + Y2(tt)*Y2(tt+1) + Y2(tt)*Y2(tt+1)*Y2(tt+2) + Y2(tt)*Y2(tt+1)*Y2(tt+2)*Y2(tt+3) ...
+ Y2(tt)*Y2(tt+1)*Y2(tt+2)*Y2(tt+3)*Y2(tt+4) + Y2(tt)*Y2(tt+1)*Y2(tt+2)*Y2(tt+3)*Y2(tt+4)*Y2(tt+5) ...
+ Y2(tt)*Y2(tt+1)*Y2(tt+2)*Y2(tt+3)*Y2(tt+4)*Y2(tt+5)*Y2(tt+6) ...
+ Y2(tt)*Y2(tt+1)*Y2(tt+2)*Y2(tt+3)*Y2(tt+4)*Y2(tt+5)*Y2(tt+6)*Y2(tt+7) ...
+ Y2(tt)*Y2(tt+1)*Y2(tt+2)*Y2(tt+3)*Y2(tt+4)*Y2(tt+5)*Y2(tt+6)*Y2(tt+7)*Y2(tt+8) ...
+ Y2(tt)*Y2(tt+1)*Y2(tt+2)*Y2(tt+3)*Y2(tt+4)*Y2(tt+5)*Y2(tt+6)*Y2(tt+7)*Y2(tt+8)*Y2(tt+9);
end
Here is a solution that is O(n):
order_prod_sum.m
function [ result ] = order_prod_sum( v, k )
result = zeros(length(v)-k,1);
last = 1;
for i = 1:k
last = last * v(i);
result = result + last;
end
n = length(v) - k;
for i = 2:n
cur_sum = result(i-1)/v(i-1) -1;
last = v(i+k-1) * last / v(i-1);
result(i) = last + cur_sum;
end
end
comparison
three_loops.m (your solution)
function [ result ] = three_loops(v, k)
result = zeros(length(v)-k,1);
for tt=1:length(v)-k
for j=0:k-1
for i=0:j
YY = prod(v(tt:tt+i));
end
result(tt) = result(tt)+ YY;
end
end
end
k=10;
Num = 100000;
v = randi(10,1,Num);
tic;
result1 = three_loops(v, k);
toc;
tic
result2 = order_prod_sum(v, k);
toc;
assert(sum(result1-result2) == 0)
output
Elapsed time is 4.739388 seconds.
Elapsed time is 0.005321 seconds.
This will do the job:
k=10;
LHS = zeros(T-k-1,1);
for tt=1:length(Y2)-k
for j=0:k-1
for i=0:j
YY = prod(Y2(tt:tt+i));
end
LHS(tt) = LHS(tt)+ YY;
end
end

Matlab recursion

Need a little help understanding what is happening in this function particularly line 7 [Fnm1,Fnm2] = fibrecurmemo(N-1); I don't understand how a new variable can be declared here with in the array. an example of what is happening would be appreciated.
function [Fn,Fnm1] = fibrecurmemo(N)
% Computes the Fibonacci number, F(N), using a memoized recursion
if N <= 2
Fn = 1;
Fnm1 = 1;
else
[Fnm1,Fnm2] = fibrecurmemo(N-1);
Fn = Fnm1 + Fnm2;
end
end
Say we start with:
fibrecurmemo(3) %// N is 3
The else statements run (since N > 2):
[Fnm1,Fnm2] = fibrecurmemo(2); %//statement 1
Fn = Fnm1 + Fnm2; %//statement 2
Before statement 2 can run, fibrecurmemo(2) must first run.
The if statements in fibrecurmemo(2) run (since N <= 2):
Fn = 1;
Fnm1 = 1;
As a result, fibrecurmemo(2) returns 1, 1.
Contininuing from statement 1 above,
[1,1] = fibrecurmemo(2); %//statement 1
Fn = 1 + 1; %//statement 2
Finally,
[2, 1] = fibrecurmemo(3);
The function returns two values.
function [xFive,yFive] = addFive(x,y)
xFive = x + 5;
yFive = y + 5;
end
xx = (addFive(3,4))
xx will be equal to 8 in this example
the syntax for assignment for multiple return values is
[a,b,c,...] = someFunc();
where someFunc() has output of [a,b,c,...]
[aa,bb] = addFive(3,4);
cc = addFive(3,4);
if you do it this way you would get
aa == 8
bb == 9
cc == 8
in the case of cc instead of [aa,bb] Then you will just get the first return value.
i.e. you could do
x = fibrecurmemo(5)
[y,z] = fibrecurmemo(5)
in this case x == y

Recursive Function to generate / print a Fibonacci series

I am trying to create a recursive function call method that would print the Fibonacci until a specific location:
1 function f = fibonacci(n)
2 fprintf('The value is %d\n', n)
3 if (n==1)
4 f(1) = 1;
5 return;
6 elseif (n == 2)
7 f(2) = 2;
8 else
9 f(n) = fibonacci(n-1) + fibonacci(n-2);
10 end
11 end
As per my understanding the fibonacci function would be called recursively until value of argument n passed to it is 1. Then the function stack would rollback accordingly. So when I call this function from command:
>> fibonacci(4)
The value of n is 4, so line 9 would execute like:
9 f(4) = fibonacci(3) + fibonacci(2);
Now I believe that that first fibonacci(3) would be called - hence again for fibonacci(3)
9 if(3) = fibonacci(2) + fibonacci(1);
The ifs in line number 3 and 6 would take care.
But now how fibonacci(2) + fibonacci(1) statement would change to:
if(3) = 2 + 1;
I am receiving the below error and unable to debug further to resolve it:
>> fibonacci(4)
The value is 4
The value is 3
The value is 2
The value is 1
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in fibonacci (line 9)
f(n) = fibonacci(n-1) + fibonacci(n-2);
Error in fibonacci (line 9)
f(n) = fibonacci(n-1) + fibonacci(n-2);
Please provide some insight for the solution and with which parameter would fibonacci function be recursively called at line number 9 first and consequently.
Ex For n = 4
f(n) = fibonacci(3) + fibonacci(2);
So will MATLAB call fibonacci(3) or fibonacci(2) first?
Shouldn't the code be some thing like below:
1 function f = fibonacci(n)
2 fprintf('The valus is %d\n', n)
3 if (n==1)
4 f(1) = 1;
5 return f(1);
6 elseif (n == 2)
7 f(2) = 2;
8 return f(2);
9 else
10 f(n) = fibonacci(n-1) + fibonacci(n-2);
11 end
12 end
fibonacci(4)
Error: File: fibonacci.m Line: 5 Column: 12
Unexpected MATLAB expression.
Why return expression in a function is resulting in an error?
Try this:
function f = fibonacci(n)
if (n==1)
f= 1;
elseif (n == 2)
f = 2;
else
f = fibonacci(n-1) + fibonacci(n-2);
end
Note that this is also a recursion (that only evaluates each n once):
function f=fibonacci(n)
f=additive(n,1,2);
function a=additive(n,x0,x1)
if(n==1)
a=x0;
else
if(n==2)
a=x1;
else
a=additive(n-1,x1,x0+x1);
end
end
If you HAVE to use recursive approach, try this -
function out = fibonacci(n)
fprintf('The valus is %d\n', n)
if (n==1)
out = 1;
return;
elseif (n == 2)
out = 2;
return;
else
out = fibonacci(n-1) + fibonacci(n-2);
end
return;
Unlike C/C++, in MATLAB with 'return', one can't return a value, but only the control goes back to the calling function. The output to be returned to the calling function is to be stored in the output variable that is defined at the start of the function.
EDIT 1: For the entire fibonacci series and which assumes that the series starts from 1, use this -
N = 16; %// Number of fibonacci numbers needed
all_nums = zeros(1,N);
all_nums(1) = 1;
for k = 2:N
all_nums(k) = fibonacci(k-1);
end
Gives -
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
Create a M-file for fibonacci function and write code as given below
function [ result ] = fibonacci( n )
if n==0|n==1
result = n;
else
result = fibonacci(n-2)+fibonacci(n-1);
end
end
Write following code in command window of matlab
for n = 0:10
fprintf('Fibonacci(%d)= %d\n', n, fibonacci(n));
end
Output :-
Fibonacci(0)= 0
Fibonacci(1)= 1
Fibonacci(2)= 1
Fibonacci(3)= 2
Fibonacci(4)= 3
Fibonacci(5)= 5
Fibonacci(6)= 8
Fibonacci(7)= 13
Fibonacci(8)= 21
Fibonacci(9)= 34
Fibonacci(10)= 55
Create a function, which returns Integer:
func fibonacci(number n : Int) -> Int
{
guard n > 1 else {return n}
return fibonacci(number: n-1) + fibonacci(number: n-2)
}
This will return the fibonacci output of n numbers, To print the series You can use this function like this in swift:
for _ in 0...10
{
print(fibonacci(number : 10))
}
It will print the series of 10 numbers.

Matlab error : Subscript indices must either be real positive integers or logicals

I have the following error in MATLAB:
??? Subscript indices must either be real positive integers or
logicals.
Error in ==> Lloyd_Max at 74 D(w_count) = mean((x -
centers(xq)).^2);
This is my code :
function [ xq,centers,D ] = Lloyd_Max( x,N,min_value,max_value )
%LLOYD_MAX Summary of this function goes here
% Detailed explanation goes here
x = x';
temp = (max_value - min_value)/2^N;
count=1;
for j=0:temp:((max_value - min_value)-temp),
centers(count) = (j + j + temp )/2;
count = count + 1;
end
for i=1:length(centers),
k(i) = centers(i);
end
w_count = 0;
while((w_count < 2) || (D(w_count) - D(w_count - 1) > 1e-6))
w_count = w_count + 1;
count1 = 2;
for i=2:(count-1),
T(i) = (k(i-1) + k(i))/2;
count1 = count1 +1 ;
end
T(1) = min_value;
T(count1) = max_value;
index = 1;
for j=2:count1,
tempc = 0;
tempk = 0;
for k=1:10000,
if(x(k) >= T(j-1) && x(k) < T(j))
tempk = tempk + x(k);
tempc = tempc + 1;
end
end
k(index) = tempk;
k_count(index) = tempc;
index = index + 1;
end
for i=1:length(k),
k(i) = k(i)/k_count(i);
end
for i=1:10000,
if (x(i) > max_value)
xq(i) = max_value;
elseif (x(i) < min_value)
xq(i) = min_value;
else
xq(i) = x(i);
end
end
for i=1:10000,
cnt = 1;
for l=2:count1,
if(xq(i) > T(l-1) && xq(i) <= T(l))
xq(i) = cnt;
end
cnt = cnt +1 ;
end
end
D(w_count) = mean((x - centers(xq)).^2);
end
end
and i call it and have these inputs :
M = 10000
t=(randn(M,1)+sqrt(-1)*randn(M,1))./sqrt(2);
A= abs(t).^2;
[xq,centers,D] = Lloyd_Max( A,2,0,4 );
I tried to comment the while and the D, Results :
I got the xq and the centers all normal, xq in the 1-4 range, centers 1-4 indexes and 0.5-3.5 range.
I dont know whats going wrong here...Please help me.
Thank in advance!
MYSTERY SOVLED!
Thank you all guys for your help!
I just putted out of the while the for loop :
for i=1:10000,
if (x(i) > max_value)
xq(i) = max_value;
elseif (x(i) < min_value)
xq(i) = min_value;
else
xq(i) = x(i);
end
end
and it worked like charm.... this loop was initilizing the array again. Sorry for that. Thank you again!
There is an assignment xq(i) = x(i) somewhere in the middle of your function, but you pass A as x from outside where you calculate A from t which is sampled by randn, so you can't promise xq is an integer.
I'm not sure exactly what you are aiming to do, but your vector xq does not contain integers, it contains doubles. If you want to use a vector of indices as you do with centers(xq), all elements of the vector need to be integers.
Upon a little inspection, it looks like xq are x values, you should find some way to map them to the integer of the closest cell to which they belong (i'm guessing 'centers' represents centers of cells?)

B-Spline Basic function Matlab recursive

I'm trying to make a B-Spline Function
first i set the variables and made the Knot vector
# cmpp.m
% Set variables
k = 3; % (8 mod 2 + 2 + 1)
p = k - 1; % Order = 2
n = 2*k - 1; % Control points = 5
l = n + p + 1; % Vector size n + p + 1 = 8
% Create the Knot vector
% Kv = [0 0 0 1 2 3 3 3] size = 8
knoten = 0; % set all knots to 0
Kv = [];
for j=1:1:l
Kv = [ Kv 0 ];
end
for i=1:1:l
if (i > n)
if (i <= n)
Kv(i) = knoten + 1;
knoten = knoten + 1;
else
Kv(i) = knoten;
end
else
Kv(i) = knoten;
end
end
then i worte a function to create the basic function
# f.m
function N = f(N,t,i,k,u,x,s)
if (u < x)
N(i,k) = ((((u-t(i)).*f(t,i,k-1,u+s,x,s)) / (t(i+k-1) - t(i))) + (((t(i+k)-u).*f(t,i+1,k-1,u+s,x,s)) / (t(i+k) - t(i+1))));
if ((u >= t(i)) && (u < t(i+1)))
N(i,1) = 1;
else
N(i,1) = 0;
end
end
end
and called it in cmpp.m
# cmpp.m
...
...
...
N = zeros(l,k);
x = (n+1) - (k-1);
s = 1;
N = [N f(N,Kv,1,k,0,x,s)];
but i get always this error in Matlab
>> cmpp
Subscripted assignment dimension mismatch.
Error in f (line 3)
N(i,k) = ((((u-t(i)).*f(t,i,k-1,u+s,x,s)) / (t(i+k-1) - t(i))) +
(((t(i+k)-u).*f(t,i+1,k-1,u+s,x,s)) / (t(i+k) - t(i+1))));
Error in cmpp (line 32)
N = [N f(N,Kv,1,k,0,x,s)]