Code to run script multiple times

조회 수: 28 (최근 30일)
Alice Shen
Alice Shen 2019년 4월 9일
편집: per isakson 2019년 4월 12일
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
per isakson
per isakson 2019년 4월 9일
So why did you write "fsolve(NFRO3Script)" in your question?
Alex Mcaulley
Alex Mcaulley 2019년 4월 9일
for loop??

댓글을 달려면 로그인하십시오.

채택된 답변

madhan ravi
madhan ravi 2019년 4월 9일
편집: madhan ravi 2019년 4월 9일
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
madhan ravi
madhan ravi 2019년 4월 10일
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 2019년 4월 10일
wow this is an amazing code! Thank you so much, it's very easy to understand and use :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

per isakson
per isakson 2019년 4월 9일
편집: per isakson 2019년 4월 9일
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
Stephen23
Stephen23 2019년 4월 9일
편집: Stephen23 2019년 4월 9일
"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 2019년 4월 11일
편집: per isakson 2019년 4월 11일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by