Code to run script multiple times

29 views (last 30 days)
Alice Shen
Alice Shen on 9 Apr 2019
Edited: per isakson on 12 Apr 2019
I have two codes, one is a big system of equations (NFRO3), and one is a script that runs fsolve(NFRO3Script). How do I write a code that runs the script multiple times while changing one number in the original code each time? (basically, one of my equations is like f(50) = x(49) - .r and I want to change that r from in .001 increments from like .7 to 1 [r = .7:.001:1]). (the equations only converge for very specific r values and i don't want to manually test them)
  4 Comments
per isakson
per isakson on 9 Apr 2019
So why did you write "fsolve(NFRO3Script)" in your question?

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 9 Apr 2019
Edited: madhan ravi on 9 Apr 2019
https://in.mathworks.com/help/matlab/math/parameterizing-functions.html - parameterize your function (by turning your script into a function file) [Make use of function! FUNCTION] and run a loop through it.
  5 Comments
madhan ravi
madhan ravi on 10 Apr 2019
See the attached file. If your using version prior to 2016b you may need to save the function separately in the name of the function (I believe you know how to do it).
Alice Shen
Alice Shen on 10 Apr 2019
wow this is an amazing code! Thank you so much, it's very easy to understand and use :)

Sign in to comment.

More Answers (1)

per isakson
per isakson on 9 Apr 2019
Edited: per isakson on 9 Apr 2019
Assumtions:
  1. NFRO3Rec is a function that "accepts a vector x and returns a vector F"
  2. r is not changed in your code
Alternate solutions:
  1. see Passing Extra Parameters ( recommended)
  2. make r global in NFRO3Rec and NFRO3Script (not recommended)
  3. use persistent according to the outline below (not tested)
  4. modify the code of NFRO3Rec.m (not recommended)
  5. and others, which I cannot think of
%% NFRO3Script
fun = @NFRO3Rec;
x0 = [(some guesses)];
for r = 0.7 : 0.001 : 1
NFRO3Rec( [], r );
x = fsolve( fun, x0 );
% assign x together with r to an array
x0 = x;
end
and
function F = NFRO3Rec( x, varargin )
persistent r
if isempty( r )
r = nan; % anything that will produce an error
end
if nargin == 2
r = varargin{1};
F = [];
return
end
% your code
f(50) = x(49) - r; % I assume the period, ".", is a typo
% more code
end
It's easier to test NFRO3Rec if it's in a separate file.
  8 Comments
Stephen23
Stephen23 on 9 Apr 2019
Edited: Stephen23 on 9 Apr 2019
"The two first "embedd" the parameters values in the function handle. "
Nested functions do not have to "embed" any parameters in the function handle.
Why would it be necessary that "the function handle must be recreated in every iteration" for a nested function? I use nested function often for parameterizing functions, and I have not experienced any need to "recreate" the function handle just because a variable in the parent workspace has changed. That would entirely defeat the point of nested functions!
A nested function would also be simpler than using persistent (which requires special cases added to the objective function).
per isakson
per isakson on 11 Apr 2019
Edited: per isakson on 11 Apr 2019
Yes, I was mistaken regarding nested function.
An even bigger mistake was that I answered the question without knowing that the documentation includes good solutions. I searched the documentation and added the link as an afterthought.
In this thread of comments I showed a somewhat better version of my "persistent" construct. And here is my current version (still missing error checking and documentation):
function F = objective_function_proxy( x, varargin )
%
% Syntax
% x = objective_function_proxy( x )
% x = objective_function_proxy( [], objective_function )
% x = objective_function_proxy( [], [], p1, p2, p3, ... )
persistent objective_function parameters
if isempty( objective_function )
parameters = {nan}; % something that will produce an error
objective_function = '';
end
if nargin == 2
objective_function = varargin{1};
F = [];
return
end
if nargin >= 3
parameters = varargin(2:end);
F = [];
return
end
F = feval( objective_function, x, parameters{:} );
end
Example of using objective_function_proxy()
%% NFRO3Script
fun = @objective_function_proxy;
x0 = [0;0];
opt = optimoptions('fsolve','Display','none');
Result = struct( 'x', cell(11,1), 'r', cell(11,1) );
jj = 0;
fun( [], 'NFRO3Rec' ); % set objective function
for r = 0.40 : 0.01 : 0.60
fun( [], [], r ); % set parameter value
x = fsolve( fun, x0, opt ); % call fsolve
jj = jj + 1;
Result(jj).r = r;
Result(jj).x = x;
x0 = x;
end
figure;
plot( [Result.r], [Result.x], 'd' );
where
function F = NFRO3Rec( x, r )
F(1) = exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2);
F(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - r;
end
Comments:
  1. objective_function_proxy() doesn't contain any case specific code, i.e. no references to NFRO3.
  2. objective_function_proxy() should be possible to use in different cases without any modification.
  3. NFRO3Script includes two calls of objective_function_proxy(), but no code that depends on the inner workings of objective_function_proxy().
  4. The objective function, NFRO3Rec(), has no special code added.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!