How to solve error " Undefined function or variable "

2 ビュー (過去 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 Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by