Output argument <Variable> (and maybe others) not assigned during call to <Function>.

조회 수: 2 (최근 30일)
I have a function that currently works perfectly:
function [x,cv] = mybasic(A,b,M,x,TOL,nmax)
% Basic iteration x <- x+M\(b-A*x)
x = x(:); % make sure initial guess is column vector
r = b-A*x;
e = M\r;
err0 = norm(e); % euclidean magnitude
rerr = 1; % relative error
n = 0;
if nargout==2
cv = zeros(nmax+1,2+length(x)); % preallocate space for cv
cv(1,:) = [n,x.',rerr]; % iter. #, iterate (row vector), rel. error
end
while (rerr>TOL && n<nmax)
x = x+e;
r = r-A*e;
e = M\r;
rerr = norm(e)/err0;
n = n+1;
if nargout==2
cv(n+1,:) = [n,x.',rerr];
end
end
n
if nargout==2
cv(n+2:end,:) = []; % delete unused space in cv
end
if rerr>TOL && n==nmax
warning('MYBASIC:noCV','no cv within %i iterations',n)
end
end
However, if all I do is change the function output to [n,x,cv] instead and try and run the function with a script file, I end up getting the error Output argument "cv" (and maybe others) not assigned during call to "mybasic". Here is the script I am trying to use:
A = [1,.375,.375;.375,1,.375;.375,.375,1];
b = [1;1;1];
M = diag(diag(A));
TOL = 1e-5;
nmax = 100;
x0 = zeros(3,1);
[n,x,cv] = mybasic(A,b,M,x0,TOL,nmax);
Please help. Thanks!

채택된 답변

Guillaume
Guillaume 2019년 3월 23일
The cv variable is only created iff nargout is 2. With 3 outputs, indeed cv will not exist.
Change
if nargout == 2
to
if nargout == 3
if you move cv to the 3rd ouptut.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by