Help sending values through varargin?
古いコメントを表示
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
採用された回答
その他の回答 (1 件)
Eugenio Grabovic
2019 年 1 月 30 日
編集済み: Eugenio Grabovic
2019 年 1 月 30 日
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 件のコメント
Stephen23
2019 年 1 月 30 日
"Problem is function handles cant manage extra parameters and require always 2 inputs"
What does this mean?
Eugenio Grabovic
2019 年 1 月 30 日
編集済み: Eugenio Grabovic
2019 年 1 月 30 日
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.
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!