How to solve error " Undefined function or variable "

2 views (last 30 days)
Hi everybody. I'm trying to run the following code:
clear
clc
% We call the function Gauss_lik
mu_0=2;
sigma2_0=5;
T=100;
X=mu_0+randn(T,1)*sqrt(sigma2_0);
theta_0=[mu_0,sigma2_0]; % I want to compute the loglikelihood in theta_0
[AvG_0,loglik_0]=Gauss_loglik(theta_0,X);
calling a function previously defined in another .m file, which is:
clear
clc
function [AvgLogL,loglik]=Gauss_loglik(theta,X);
mu=theta(1);
sigma2=theta(2);
T=size(X,1);
loglik=zeros(T,1);
loglik=-0.5*T*log(2*pi)-0.5*T*log(sigma2)-0.5*(((X-mu).^2)/sigma2);
AvgLogL=mean(loglik);
end;
but MATLAB dislays me this result:
Undefined function or variable 'Gauss_loglik'.
Error in MyExercises8and9 (line 13)
[AvG_0,loglik_0]=Gauss_loglik(theta_0,X);
How can I solve this problem?

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 6 Apr 2020
Edited: KALYAN ACHARJYA on 6 Apr 2020
Save the function (Gauss_loglik.m) in the same directory (No clc or clear in function file)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!