6 views (last 30 days)

When I run my script, I get this error from my function. I can't see why

This is my function:

function pdf = apost(m);

sd1 = 300^2;

A = sd1*ones(1,12);

C_m = diag(A);

Cm = inv(C_m);

sd2 = 1;

B = sd2*ones(1,12);

C_d = diag(B);

Cd = inv(C_d);

[N,~] = size(m);

if N==5

Cm = inv(C_m);

Cd = inv(C_d);

m0 = [210.540977721001,336.865564353602,437.925233659682,516.527198675522,572.671459401123,599.339983245783,595.129163691363,574.075065919263,523.545231266222,442.136053214102,305.986220954521,179.661634321921];

g = [-5.57961065006546,-9.24249212739290,-13.3900010001821,-17.3638621584906,-20.6030599093808,-22.2586614660132,-21.9922416252264,-20.6883184326565,-17.7504996477641,-13.5862938793517,-8.19100512552534,-4.94137181487074]

y = [-15.0 -24.0 -31.2 -36.8 -40.8 -42.7 -42.4 -40.9 -37.3 -31.5 -21.8 -12.8];

L = (y-g)'.*C_d.*(y-g);

rho = (m-m0)'.*Cm.*(m-m0);

sigma = -0.5.*(rho + L);

else

disp('Incorrect dimension of input vector')

end

This is my script:

% (4) Metropolis algorithm

K = 10000; %Number of samples

pts = zeros(length(m0),K); %Array with sample output points

hfuncval = zeros(1,K); %Array with function value outputs

pts(:,1) = m0; %Set starting points

hfuncval(1) = apost(pts(:,1)); %Compute function in starting point

step = 0.5; %Set step length

return

%Start sampling

for k = 2:K

ptpert = pts(:,k-1) + (2*rand(12,1)-1)*step; % propose perturbed point

hfuncpert = sigma(ptpert); % Compute function in perturbed point

u = rand; % Generate random number in [0,1]

if u < hfuncpert/hfuncval(k-1) % accepting new points

% if u < exp(log(hfuncpert)-log(hfuncval(k-1)))

pts(:,k) = ptpert;

hfuncval(k) = hfuncpert;

else % Rejecting new points

pts(:,k) = pts(:,k-1);

hfuncval(k) = hfuncval(k-1);

end

end

Allen
on 9 Jan 2020

Currently, you have only decleared pdf as an output argument at the start of your function, but have neglected to assign a value to it within the function. You need to define pdf somewhere within your function.

function pdf = apost(m);

sd1 = 300^2;

A = sd1*ones(1,12);

C_m = diag(A);

Cm = inv(C_m);

sd2 = 1;

B = sd2*ones(1,12);

C_d = diag(B);

Cd = inv(C_d);

[N,~] = size(m);

if N==5

Cm = inv(C_m);

Cd = inv(C_d);

m0 = [210.540977721001,336.865564353602,437.925233659682,516.527198675522,572.671459401123,599.339983245783,595.129163691363,574.075065919263,523.545231266222,442.136053214102,305.986220954521,179.661634321921];

g = [-5.57961065006546,-9.24249212739290,-13.3900010001821,-17.3638621584906,-20.6030599093808,-22.2586614660132,-21.9922416252264,-20.6883184326565,-17.7504996477641,-13.5862938793517,-8.19100512552534,-4.94137181487074]

y = [-15.0 -24.0 -31.2 -36.8 -40.8 -42.7 -42.4 -40.9 -37.3 -31.5 -21.8 -12.8];

L = (y-g)'.*C_d.*(y-g);

rho = (m-m0)'.*Cm.*(m-m0);

sigma = -0.5.*(rho + L);

%pdf = 'Assign value during this case'

else

disp('Incorrect dimension of input vector')

%pdf = 'This case might require a different value for pdf. eg - pdf = [];

end

end

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

Start Hunting!
## 4 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/499585-output-argument-variable-and-maybe-others-not-assigned-during-call-to-function#comment_784484

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/499585-output-argument-variable-and-maybe-others-not-assigned-during-call-to-function#comment_784484

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/499585-output-argument-variable-and-maybe-others-not-assigned-during-call-to-function#comment_784485

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/499585-output-argument-variable-and-maybe-others-not-assigned-during-call-to-function#comment_784485

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/499585-output-argument-variable-and-maybe-others-not-assigned-during-call-to-function#comment_784488

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/499585-output-argument-variable-and-maybe-others-not-assigned-during-call-to-function#comment_784488

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/499585-output-argument-variable-and-maybe-others-not-assigned-during-call-to-function#comment_784494

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/499585-output-argument-variable-and-maybe-others-not-assigned-during-call-to-function#comment_784494

Sign in to comment.