Hello,
New to the forum, and wanted to ask about some code that I have working, but
could use improvement.
In using fsolve to evaluate non-linear equation(s), I wanted to vary some of
the constants in the evaluated equation.
The go-to method seems to be using global defined variables. It seems a bit
messy(in needing to update global list everywhere it was used), so I was
looking for something cleaner to use within functions.
After seeing a note about the movement away from inline functions, I was
able to use an anonymous function (which has access to variables in the
enclosing scope). A detail of this is that the anonymous function has to be
re-stated each time the constants within it are redefined elsewhere.
Questions:
1) It works, but am I sacrificing something by doing this?
2) Is there a non-for-loop method you might think of to evaluating fsolve
across different parameters?
*Example code*, passing script level variable to anonymous function
C = [1 2]; % Parameters in the evaluated equation that do not vary with the
independent variable
anonymous_function = @(x) C(1)*x(1)^2+C(2)*sqrt(x(2)) %Non-linear equation
system to be evaluated
x0=[1,1]; % Set initial values of independent variables in fsolve execution
xout = fsolve(anonymous_function,x0);
*Example Code*, iterating across parameters sent to anonymous
function/fsolve
C_matrix=[1 2;2 3;3 4] % Set of parameters to be used in evaluation
[m n] = size(C_matrix);
x_values = zeros(m,2); % Matrix for storing fsolve output, 2 independent
variables
for i=1:m
C = C_matrix(i,:); % Select relevant constant parameter set
anonymous_function = @(x) C(1)*x(1)^2+C(2)*sqrt(x(2))-15; % Re-instantiate
anonymous function to include updated parameters
x0=[10,100]; % Set initial values of independent variables in fsolve
execution
xout = fsolve(anonymous_function,x0);
x_values(i,:) = xout; % Save output of fsolve result for independent
variables
endfor
Thanks for the feedback, massive appreciation for octave and the people who
keep it alive
--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html