How to solve error " Undefined function or variable "

조회 수: 3 (최근 30일)
Noemi Giaconi
Noemi Giaconi 2020년 4월 6일
편집: KALYAN ACHARJYA 2020년 4월 6일
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?

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 4월 6일
편집: KALYAN ACHARJYA 2020년 4월 6일
Save the function (Gauss_loglik.m) in the same directory (No clc or clear in function file)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by