MATLAB Answers

Help sending values through varargin?

1 view (last 30 days)
This is my first time writing code using varargin, so appologies if I'm doing it completely wrong.
I'm trying to keep it as general as possible, and the function file is below.
For example, say I set the inputs as
fun = @(x, Q) x^3 + Q
xi=[-1, -2]
es= 10^-5
If I want to send Q=7 through varargin, how would I set it up, both in the function file and the line of code I call it from?
function [xroot,residual,ea,iter_count] = root_finder(fun,xi,es,max_it,varargin)
% fun - The function to examine
% xi - A vector containing the inital guesses for the root value
% es - Stopping criteria. How close you want it to calculate to. Default is 0.00001
% max_it - Maximum number of times the program will run. Default is 30
% varagin - Any other criteria needed for the function
% xroot - Root estimate
% residual - The residual of the function
% ea - The relative error
% iter_counter - number of iterations you went through
if isempty(es) %Sets stopping criteria default to 10^-5
es = 10^-5;
if isempty(max_it) %Sets maximum iteration count default to 30
max_it = 30;
for loops=1:max_it;
fun_at_x1 = fun(xi(1)); %Finds residual of function at each guess
fun_at_x2 = fun(xi(2)); %
new_root = xi(2) - fun_at_x2 * ((xi(2) - xi(1)) / (fun_at_x2 - fun_at_x1)); %Calculates new root to check function at using Newtons Method
fun_at_new_root = fun(new_root); %Calculates function at the new root
xi= [xi(2) new_root]; %Sets xi to store the two most recently tested roots
if abs(fun_at_new_root) < es %If the residual of the function at new_root is within the stopping criteria, exit the for loop
residual = fun_at_new_root
iter_count = loops
ea = (xi(2) - xi(1)) / xi(2)


Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 30 Jan 2019
Edited: Stephen Cobeldick on 30 Jan 2019
The simplest solution is that whenever you call your function handle, add this at the end:
For example:
fun_at_x1 = fun(xi(1),varargin{:});
fun_at_x2 = fun(xi(2),varargin{:});
And then call your main function with the extra required parameters that your function requires:
F = @(x, Q) x^3 + Q;
[...] = root_finder(F,[-1,-2],1e-5,50,7)
% ^ include function parameters here.
This solution does not require that your function has a fixed number of parameters: simply define your function handle with whatever input arguments you need, pass the required values as extra input arguments to your main function, and varargin does the rest for you!
Explanation: varargin is simple to understand: it simply collects any trailing input arguments into one cell array named varargin. You can access that cell array just like you would any other cell array:
This is why a comma separated list tcan be used o pass those input arguments as input arguments to fun when it is called.

  1 Comment

Kaitlin Jones
Kaitlin Jones on 30 Jan 2019
Perfect. Exactly what I was looking for. Thank you so much.

Sign in to comment.

More Answers (1)

Eugenio Grabovic
Eugenio Grabovic on 30 Jan 2019
Edited: Eugenio Grabovic on 30 Jan 2019
Inside the function varargin is a cell with all the extra inputs, if you want to call it inside you function:
function [xroot,residual,ea,iter_count] = root_finder(fun,xi,es,max_it,varargin)
extra_parameter1 = varargin{1};
extra_parameter2 = varargin{2};
extra_parameter3 = varargin{3}; % and so on ...
... other stuff
Problem is that your function handle always require 2 inputs, and will give an error if the varargin parameter (Q) is not given; to overcome this you can set a default Q if its not passed by the user in you rootfinder function:
function [xroot,residual,ea,iter_count] = root_finder(fun,xi,es,max_it,varargin)
if nargin == 4
Q = 0; % example default if not user specified
elseif nargin == 5
Q = varargin{1}; % if user specifies the extra parameter
error(" too many input arguments") % if user specifies more than 1 extra parameter
... other stuff
Anyway here is the complete documentation about varargin:


Stephen Cobeldick
Stephen Cobeldick on 30 Jan 2019
"Problem is function handles cant manage extra parameters and require always 2 inputs"
What does this mean?
Eugenio Grabovic
Eugenio Grabovic on 30 Jan 2019
i meant if he doesnt pass the extra parameter, which goes into a function handle with 2 inputs, he will recieve an error message of not enough input arguments.
Edited to make it more clear.

Sign in to comment.

Sign in to answer this question.