How to solve error " Undefined function or variable "

3 visualizaciones (últimos 30 días)
Noemi Giaconi
Noemi Giaconi el 6 de Abr. de 2020
Editada: KALYAN ACHARJYA el 6 de Abr. de 2020
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?

Respuestas (1)

KALYAN ACHARJYA
KALYAN ACHARJYA el 6 de Abr. de 2020
Editada: KALYAN ACHARJYA el 6 de Abr. de 2020
Save the function (Gauss_loglik.m) in the same directory (No clc or clear in function file)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by