Call arguments in a function in MATLAB

Viewed 42

I tried to run this function in MATLAB:

function llik = log_likelihood(p)
global d;
N = length(d);
tau = fzero(@(t) (t - (t^2 * p + 1 - p) / (2 * (t * p + 1 - p))), [0,1]);

loglik = 0;
for i = 1 : N
    loglik = loglik + log(isnan(d(i)) * (1 - p * (1 - tau) + ~isnan(d(i))* p * (1 - tau)));
end

llik = loglik / N;
end

Here, p is a scalar. MATLAB gives me an error warning saying

Error using fzero>localFirstFcnEval
FZERO cannot continue because user-supplied function_handle ==>
@(t)(t-(t^2*p+1-p)/(2*(t*p+1-p))) failed with the error below.

 Unrecognized function or variable 'p'.

I am confused since p should be the argument of the function. How can it be unrecongized? Thank you for your help!

1 Answers

Everything seems okay with my Matlab if i assign the value d inside the function, where do you define the variable d, if it's a global variable, it must be define as:

global d;

This is my result:

result with assign d variable

Related