Defining and calling functions in matlab

15 views (last 30 days)
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

Accepted Answer

Matt J
Matt J on 5 Feb 2023
function fx = wbl(X)

More Answers (1)

Image Analyst
Image Analyst on 5 Feb 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 Comments
Image Analyst
Image Analyst on 6 Feb 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. 🙂
Matt J
Matt J on 6 Feb 2023
The vote was mine. I'm not sure @okoth ochola knows about upvoting.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!