Entering default values in a function.
조회 수: 16 (최근 30일)
이전 댓글 표시
I am trying to add default values into my function but whenever I use nargin or varargin i still get 'undefined function or variable 'x0'', could someone advise? I would like to have it for x0 and TOL not being entered. The function requires you to input n, say n=10.
function code(n,x0,TOL)
switch nargin
case 0
ee=ones(n,1);
x0 = ee;
end
break
end
%Or using this.
% if nargin < 3
% Tol = 10^-6;
% if nargin < 2
% x0 = ee;
% end
end
As requested, my full code:
function [counter,ratio,x,TOL,ROC]=FixedPointIteration(n,x0, TOL)
if nargin < 1
error('n required');
end
if nargin < 2
x0 = ones(n, 1);
end
if nargin < 3
TOL = 10^-6;
end
A=abs(rand(n)*10^0);
A=A*transpose(A);
% A=[1,5;5,5];
ee=ones(n,1);
x0=ee;
x=x0;
% x=[1;2] %column vector
D=zeros(length(x),length(x));
for i=1:length(x)
D(i,i)=x(i);
end
e=ones(length(x),1);
MaxTOL=10^(-6);
Tolerance=norm(D*A*D*e-e,1);
counter=1;
while Tolerance>TOL
TOLprevious=Tolerance;
counter=counter+1;
y=x;
x_k1=1./(A*x); %Calculates the iterative solution
ratio=x_k1./y; %Ratio of current solution against previous
x=x_k1./(ratio).^0.5;
for i=1:length(x)
D(i,i)=x(i);
end %Builds the matrix D(x)
P=D*A*D;
Tolerance=norm(D*A*D*e-e,1);
ratio_list(counter-1)=Tolerance/TOLprevious;
end
figure(1)
% max(ratio_list)
semilogy(1:counter-1,(ratio_list))
title('Error Ratio Against Step Number')
xlabel('Step Number')
ylabel('Log Error Ratio')
% hold off
if length(A)>2
t=eig(P);
ROC=t(3)/t(2); %Extracts the initial rate of convergence
else
t=eig(p);
ROC=t(2)/t(1);
end
end
댓글 수: 16
답변 (1개)
Rik
2019년 3월 28일
If you only want to specify n, you should only provide that as the input:
n=4;
[counter,ratio,x,TOL,ROC]=FixedPointIteration(n)
댓글 수: 3
Rik
2019년 3월 28일
There is a difference between the function definition and a function call. If you provide fewer input variables than are defined in the function header, Matlab will try to run the function without defining the variables. That is why you define them after checking nargin.
The following two lines are equivalent in Matlab:
n=4;
[counter,ratio,x,TOL,ROC]=FixedPointIteration(n);
[counter,ratio,x,TOL,ROC]=FixedPointIteration(4);
Walter Roberson
2019년 3월 28일
편집: Walter Roberson
2019년 3월 28일
Arguments are positional in MATLAB unless you deliberately parse the input such as using name/value pairs. So you cannot simply call your function with n and TOL because TOL is your third argument and when you pass it in the second position, MATLAB would have no way of knowing that the value was intended to be a tolerance rather than an x0 value.
The typical convention in MATLAB is for the user to pass [] (empty array) in the places they want to default the input, and for the code to detect those [] and use default values. For example,
if nargin < 3 || isempty(x0)
x0 = ones(1,n);
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!