Entering default values in a function.

14 views (last 30 days)
KieranSQ
KieranSQ on 26 Mar 2019
Edited: Walter Roberson on 28 Mar 2019
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 Comments
KieranSQ
KieranSQ on 28 Mar 2019
Edited: KieranSQ on 28 Mar 2019
@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 on 28 Mar 2019
So what is the problem with this code as is?

Sign in to comment.

Answers (1)

Rik
Rik on 28 Mar 2019
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 Comments
Rik
Rik on 28 Mar 2019
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 on 28 Mar 2019
Edited: Walter Roberson on 28 Mar 2019
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

Sign in to comment.

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!