原函数为:
function x = diedai(A,c,x0,method,w,tol,trace)
if nargin < 6 | isempty(tol)
tol = 1e-6;
end
if nargin >= 6 & tol == 0
tol = 1e-6;
end
if nargin < 7 | isempty(trace)
trace = 0;
end
if trace
fprintf('\n Initial guess:\n')
fprintf('%8.6g ',x0)
end
c = (c(.')';
x0 = (x0(.')';
n = length(c);
[nr nc] = size(A);
if nr ~= nc
error('Coefficient matrix is not square.')
end
if nr ~= n
error('Coefficient matrix and vector of constants do not have the same length.')
end
if length(x0) ~= n
error('Vector of unknowns and vector of constants do not have the same length.')
end
if det(A) == 0
fprintf('\n Rank = %7.3g\n',rank(A))
error('The coefficient matrix is singular.')
end
iter = 0;
if method = 1
D = diag(diag(A));
a0 = inv(D) * A - eye(n);
c0 = inv(D) * c;
x = x0;
x0 = x + 2 * tol;
while max(abs(x-x0)) >= tol
x0 = x;
x = c0-a0 * x0;
end
end
if method = 2
L = tril(A);
U = triu(A);
c0 = inv(L) * c;
a0 = inv(L) * (U-diag(diag(A)));
x = x0;
x0 = x + 2 * tol;
while max(abs(x-x0)) >= tol
x0 = x;
x = c0 - a0 * x0;
end
end
if method = 3
w = input('请输入松弛因子:亚松弛0<w<1;超松弛1<w<2.w= ');
if w == 0
w = 1.3;
end
D = diag(diag(A));
L = tril(A) - D;
U = triu(A) - D;
f = D + w * L;
c0 = w * inv(f) * c;
a0 = inv(f) * (D- w * triu(A));
x = x0;
x0 = x + 2 * tol;
while max(abs(x-x0)) >= tol
x0 = x;
x = c0 + a0 * x0;
end
end
if trace
iter = iter + 1;
fprintf('\n Iteration no. %3d\n',iter)
fprintf('%8.6g ',x)
end
在命令窗口输入 A=[6 2 1 -1;2 4 1 0;1 1 4 -1;-1 0 -1 3];c=[6 1 5 -5]';x0=[0 0 0 0]; diedai(A,c,x0,1,1)
错误为 Error: File: diedai.m Line: 2 Column: 29
The input character is not valid in MATLAB statements or expressions. 作者: gooboy1236 时间: 2008-10-27 22:32
呵呵,大家帮忙看一下,
function y = sefilter2(x, f1, f2, extmod, shift)
% SEFILTER2 2D seperable filtering with extension handling
%
% y = sefilter2(x, f1, f2, [extmod], [shift])
%
% Input:
% x: input image
% f1, f2: 1-D filters in each dimension that make up a 2D seperable filter
% extmod: [optional] extension mode (default is 'per')
% shift: [optional] specify the window over which the
% convolution occurs. By default shift = [0; 0].
%
% Output:
% y: filtered image of the same size as the input image:
% Y(z1,z2) = X(z1,z2)*F1(z1)*F2(z2)*z1^shift(1)*z2^shift(2)
%
% Note:
% The origin of the filter f is assumed to be floor(size(f)/2) + 1.
% Amount of shift should be no more than floor((size(f)-1)/2).
% The output image has the same size with the input image.
%
% See also: EXTEND2, EFILTER2
if ~exist('extmod', 'var')
extmod = 'per';
end
if ~exist('shift', 'var')
shift = [0; 0];
end
% Make sure filter in a row vector
f1 = f1(;
f2 = f2(;