Entering default values in a function.

5 次查看(过去 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 个评论
KieranSQ
KieranSQ 2019-3-28
编辑:KieranSQ 2019-3-28
@Stephen Cobeldick apologies for my delay. My code is below and run it by typing [counter,ratio,x,TOL,ROC]=FixedPointIteration(n,x0, TOL) for a specified n.
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
Adam
Adam 2019-3-28
So what is the problem with this code as is?

请先登录,再进行评论。

回答(1 个)

Rik
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
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
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 CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by