Defining and calling functions in matlab
39 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, am trying to defing the fuction pasted below in a separate file and then call it in another script saved in a different file. the function is nothing but just the log-likelihood fuction of Weibull distribution function. However am getting error that have also pasted below. What can be possibly wrong? Am using R2018a, Please help.
this was my trial
function out = wbl(X)
n=numel(X)
A1=X(:,1);
A2=X(:,2);
%objective function
fx=n*log(A1)-n*log(A2)-sum((X./A2).^A1)+(A1-1)*sum(log(X))
%define penalty term
end
the error that I got is pasted below
Output argument "out" (and maybe others) not assigned during call to "wbl2".
Error in Gwo7 (line 20)
fx=fun(pos);
the corresponding script that i called the function in, is pasted below
clear all
clc
format short
fun = @wbl2;
X=rand(34,2)
N=300;
D=2;
lb=[0 0];
ub=[1 1];
itermax=500;
%generating intial popilation size
for i=1:N
for j=1:D
pos(i,j)=lb(j)+rand.*(ub(j)-lb(j));
end
end
%Evaluation of objective function
fx=fun(pos);
[fminvalue,ind]=min(fx);
% GWO main loop
iter=1;
while iter<=itermax
Fgbest=fminvalue;
a=2-2*iter/itermax;
for i=1:N
X=pos(i,:);
pos1=pos;
A1=2.*a.*rand(1,D)-a;
C1=2.*rand(1,D);
[alpha, alphaind]=min(fx);
alphapos=pos1(alphaind,:);
Dalpha=abs(C1.*alphapos-X);
X1=alphapos-A1.*Dalpha;
pos1(alphaind,:)=[];
fx1=fun(pos);
%finding beta position
[bet,betind]=min(fx1);
betpos=pos1(betind,:);
A2=2.*a.*rand(1,D)-a;
C2=2.*rand(1,D);
Dbet=abs(C2.*betpos-X);
X2=betpos-A2.*Dbet;
pos1(betind,:)=[];
fx1=fun(pos1);
%Delta position
[delta,deltaind]=min(fx1);
deltapos=pos1(deltaind,:);
A3=2.*a.*rand(1,D)-a;
C3=2.*rand(1,D);
Ddelta=abs(C3.*betpos-X);
X3=deltapos-A3.*Ddelta;
Xnew=(X1+X2+X3)./3;
%check bound
Xnew=max(Xnew,lb);
Xnew=min(Xnew,ub);
fnew=fun(Xnew);
%greedy slection
if fnew<fx(i)
pos(i,:)=Xnew;
fx(i,:)=fnew;
end
end
%Update Gbest
[fmin,find]=min(fx);
if fmin<Fgbest
Fgbest = fmin;
gbest=pos(find,:);
end
%memorize
[optval,optind]=min(fx);
BestFx(iter)=optval;
BestX(iter,:)=pos(optind,:);
%show iteration infomation
plot(BestFx, 'LineWidth',2);
iter=iter+1
end
out=BestX
0 commentaires
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 5 Fév 2023
Your script calls wbl2, not wbl. And your original wbl function
function out = wbl(X)
n=numel(X)
A1=X(:,1);
A2=X(:,2);
%objective function
fx=n*log(A1)-n*log(A2)-sum((X./A2).^A1)+(A1-1)*sum(log(X))
%define penalty term
end
never assigns "out" though it tries to return it. That's what gives the error
So, what is out? Is if fx? If so you can either do
function out = wbl(X)
n=numel(X)
A1=X(:,1);
A2=X(:,2);
%objective function
out = n*log(A1)-n*log(A2)-sum((X./A2).^A1)+(A1-1)*sum(log(X))
%define penalty term
end
or send out fx instead of out.
function fx = wbl(X)
n=numel(X)
A1=X(:,1);
A2=X(:,2);
%objective function
fx = n*log(A1)-n*log(A2)-sum((X./A2).^A1)+(A1-1)*sum(log(X))
%define penalty term
end
3 commentaires
Image Analyst
le 6 Fév 2023
You're welcome. I'm glad my explanation helped you understand how it works, and why yours didn't work. Thanks for voting for, and accepting, the answers. 🙂
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!