Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have tried to write a function in order to calculate the n-th element of the Fibonacci series:
function [F] = Fibonacci(n)
if n==0
F = 0 ;
elseif n==1
F = 1 ;
else
F = Fibonaccie(n-1) + Fibonacci(n-2) ;
end
end
but as a result I get this:
Undefined function 'Fibonaccie' for input arguments of type 'double'.
Error in Fibonacci (line 14)
F = Fibonaccie(n-1) + Fibonacci(n-2) ;
I don't know where the problem really is.
There is a typo in line 14. You had just written "Fibonaccie" but must have been Fibonacci..
Btw, just note that you dont have to write elseif condition. You can do it with only one else using or statement. However to make your code more robust you should consider n < 0 case:
function F = Fibonacci(n)
if n < 0
F = 0;
elseif n == 0 || n == 1
F = n ;
else
F = Fibonacci(n-1) + Fibonacci(n-2) ;
end
end
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
Hi i am trying to get two returns from the following code in MATLAB:
function [Xq, SNq] = cuantificacion(x,xmax,xmin,b)
N = input('Introduce un numero de muestras: ');
L = 2^b;
delta = (xmax-xmin)/L;
if(abs(x)<xmax)
Xq = (fix((abs(x)/delta)) + 1/2)*delta*sign(x);
else
Xq = ((L-1)/2)*delta*sign(x);
end
p = 0;
q = 0;
for i = 0:N
p = p+x^2;
q = q + (Xq - x);
end
Px = 1/N*p;
Pq = 1/N*q;
SNq = 10*log(Px/Pq);
end
But i only get one return and I don't understand why.
Thanks to Ander i was just calling it wrong. I had to call it:
[return1, return2] = cuantificacion(x,xmax,xmin,b);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'd like to write the Matlab code for the following equation:
z(k)=lamda*x(k)+(1-lamda)*z(k-1)
lamda can be of any value. x is a 1000x22 matrix. Z(0)=0.
Can anyone help me please?
You can use iteration function.same thing like this
function z = itrationFunctio(k,x,lambda)
if(k == 0)
z = 0;
else
z = lambda*x+(1-lambda)*itrationFunctio((k-1),x,lambda);
end
and in your code just call itrationFunctio(k,x,lambda).
Does that vectorized solution work for you?
% parameters
x = rand(10,1);
lambda = 2;
% init z, normalize with (1-lambda)
z(2:numel(x)) = lambda/(1-lambda)*x(2:end);
% cumsum, denormalize with (1-lambda)
z = cumsum(z)*(1-lambda)
However I don't get why your x is a matrix and not a vector. What is z supposed to be, in what dimension works k? So in case xrepresents a couple of vectors you want to calculate in parallel, that could work:
% parameters
x = rand(1000,22);
lambda = 2;
% init z, normalize with (1-lambda)
z(2:size(x,1),:) = lambda/(1-lambda)*x(2:end,:);
% cumsum, denormalize with (1-lambda)
z = cumsum(z,1)*(1-lambda)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a function (the hill function) when X and K are the same (x==k) the output should be 0.5, when I test the function it gives a result of 0.5, when I try to plot it, I do not get 0.5 for my Y. Can anyone explain what I am doing wrong?
n = 1;
k = 1;
x = [0:0.01:2].';
y = (x.^n)/((x.^n)+(k.^n));
plot(x,y);
n = 1;
k = 1;
x = [0:0.01:2].';
y = (x.^n)./((x.^n)+(k.^n));
plot(x,y);
You missed a dot before the division.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Given the following code:
t = -5:.1:5;
w = pi;
x = zeros(101,1);
Xt = zeros(101,1);
for i = 1 : 101;
x = exp((-3*t)+(-1i*w*t));
Xt = trapz(t, x);
end
disp (length(x))
disp (length(Xt))
disp (Xt)
The values of Xt do not change, which is a problem.
How should this be coded in order for Xt to change when x is changed?
Side note:
Xt(i) = trapz(t,x);
Reduces the vector from length 101 to length 1 and therefore cannot be used.
i am not sure if this is what you wanted. anyways while working on imaginary numbers it is always a good idea to not use i and j as common variables, just to avoid confusion (IMO)
xt = zeros(101,1);
x = exp((-3.*t)+(-1i*w.*t));
for k=2:101
xt(k)=trapz(t(1:k),x(1:k));
end
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a MALTAB function:
find(x > [x(1)-1;x(1:n-1)] & x > [x(2:n);x(n)-1]);
How can I unvectorize this? I am not quite sure what it is even testing for!
Ideas?
Something like this:
result = []
for i = 1:n
if i == 1 % special case, since x(-1) does not exist
x_below = x(1) - 1;
else
x_below = x(i - 1);
end
if i == n % special case, since x(n + 1) does not exist
x_above = x(n) - 1;
else
x_above = x(i + 1);
end
if x(i) > x_below && x(i) > x_above
result = [result, i]; %add found index to result
end
end
So as Eric mentioned, it returns the index of all elements in x that are larger then their neighbors. For x(1), a 'fake' lower neighbor of x(1) - 1 is made, so that x(1) is always larger. Index 1 is thus returned if x(1) > x(2). A similar trick is done for x(n).