调用期间未分配输出参数(MATLAB)

时间:2017-02-26 10:51:47

标签: matlab

我在MATLAB中遇到这个功能有点困难,我无法弄清问题是什么。错误是,

Output argument "x" (and maybe others) not assigned during call to "SDLS".

Error in NO_wk4_Q3c (line 7)
x = SDLS( A, x0, b, 1e-3 );

据我所知,这与' x'的输出有关,但我已经指定了' x'在函数文件中。是否与“'同时'循环?

以下是功能文件:

function [ x, k ] = SDLS( A, x0, b, tol )
% Steepest descent with exact line search.
% A is the input matrix, x0 is the initial guess, b is a vector, tol is the
% tolerence level for convergence.

if nargin < 4
    tol=eps;
end

g = A*x0+b; % Derivative of Q(x)=(1/2)x'Ax+b'x+c;
p = zeros(1,1);
k = 0;
x=zeros(1,1);

while norm(g,2)>=1e-3
    g = A*x0 + b;
    p = -g; % direction vector
    alpha = -(transpose(g)*p)/(transpose(p)*A*p); % step length
    x = x0 + alpha*p; % updates approximation
    x0 = x;
    k = k+1;
end
end

调用该函数的脚本文件如下:

clear all; 

A = [4 2; 2 3]; % Matrix
x0 = [ -1; -1]; % Initial guess
b = [ 3; 1];

[ x, k] = SDLS( A, x0, b, 1e-3 );

任何建议都会很棒: - )

0 个答案:

没有答案