Help sending values through varargin?

23 visualizaciones (últimos 30 días)
Kaitlin Jones
Kaitlin Jones el 30 de En. de 2019
Comentada: Kaitlin Jones el 30 de En. de 2019
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
max_it=50
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)
%Inputs
% 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
%Outputs
% 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;
end
if isempty(max_it) %Sets maximum iteration count default to 30
max_it = 30;
end
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
break
end
end
xroot=new_root
residual = fun_at_new_root
iter_count = loops
ea = (xi(2) - xi(1)) / xi(2)
end

Respuesta aceptada

Stephen23
Stephen23 el 30 de En. de 2019
Editada: Stephen23 el 30 de En. de 2019
The simplest solution is that whenever you call your function handle, add this at the end:
fun(...,varargin{:})
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 comentario
Kaitlin Jones
Kaitlin Jones el 30 de En. de 2019
Perfect. Exactly what I was looking for. Thank you so much.

Iniciar sesión para comentar.

Más respuestas (1)

Eugenio Grabovic
Eugenio Grabovic el 30 de En. de 2019
Editada: Eugenio Grabovic el 30 de En. de 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
end
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
else
error(" too many input arguments") % if user specifies more than 1 extra parameter
end
... other stuff
end
Anyway here is the complete documentation about varargin: https://it.mathworks.com/help/matlab/ref/varargin.html
  2 comentarios
Stephen23
Stephen23 el 30 de En. de 2019
"Problem is function handles cant manage extra parameters and require always 2 inputs"
What does this mean?
Eugenio Grabovic
Eugenio Grabovic el 30 de En. de 2019
Editada: Eugenio Grabovic el 30 de En. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by