Problem solving system of nonlinear equations with fsolve trying to change inputs
10 views (last 30 days)
Show older comments
I am trying to solve a system of six nonlinear equations using fsolve (see below). There are additional parameters in my equations that I would like to be able to input into the system or change the functionality without having to go into my function and manually editing them each time.
They are F, rho, and A. Right now I have set them all to 1, and I am able to get a solution so at least the script is running correctly.
function f = pressXmanifold(x)
F = [1,1,1];
rho = 1;
A = 1;
f(1) = x(1)-x(3) - ...
1/2*rho*(x(4)/F(1))^2*...
A*(x(4)/x(6)*F(3)/F(1))^(-2)*...
(1+(F(3)/F(1))^2+3*(F(3)/F(1))^2*((x(4)/x(6))^2-(x(4)/x(6))));
f(2) = x(2)-x(3) - ...
1/2*rho*(x(5)/F(2))^2*...
A*(x(5)/x(6)*F(3)/F(2))^(-2)*...
(1+(F(3)/F(2))^2+3*(F(3)/F(2))^2*((x(5)/x(6))^2-(x(5)/x(6))));
f(3) = x(1) - x(3);
f(4) = x(2) - x(3);
f(5) = x(4) - x(5);
f(6) = x(4)+x(5) - x(6);
end
- For F I would want to be able to input any 1 x 3 array into my function.
- For rho I would want to input a value as well.
- For A, it would have to change with the iterations based on the following criteria (given as an if statement).
if F(3)/F(1) <= 0.35 && x(3)/x(2) <= 1
A = 1;
elseif F(3)/F(1) > 0.35 && x(3)/x(2) <= 0.4
A = 0.9*(1-x(3)/x(2));
elseif F(3)/F(1) > 0.35 && x(3)/x(2) > 0.4
A = 0.55;
end
For now I've only tried dealing with the first two parameters. f = pressXmanifold(x,F,rho) and removing the first two lines in my function.
But when I try to follow the instructions on the MathWorks site
options = optimoptions('fsolve','Display','none','PlotFcn',@optimplotfirstorderopt);
fun = @pressXmanifold;
x0 = [1,1,1,1,1,1];
x = fsolve(fun,x0,F,rho,options)
Unable to perform assignment because dot indexing is
not supported for variables of this type.
Error in createOptionFeedback (line 33)
options.(stopTestOptions{k}) = [];
Error in prepareOptionsForSolver (line 57)
optionFeedback = createOptionFeedback(options);
Error in fsolve (line 157)
[options, optionFeedback] =
prepareOptionsForSolver(options, 'fsolve');
0 Comments
Answers (1)
Walter Roberson
on 25 Jan 2025 at 21:56
x = fsolve(fun,x0,F,rho,options)
This instructs fsolve to process fun as a function handle that expects a single input, and to work with x0 as the initial input.
It then instructs that F be used as the options structure.
After that, undocumented behaviour kicks in. It is undocumented (or perhaps no longer documented since R12.1 or so), but any remaining parameters are passed to the input function, making the function call fun(X, rho, options)
However, F is the wrong datatype to be an options structure, so fsolve fails trying to process F as an options structure.
If what you had wanted had been to pass F and rho to fun then the call you would use would be
x = fsolve(fun,x0,options,F,rho)
However, this would fail because fun = @pressXmanifold and pressXmanifold is a function that expects only a single input.
pressXmanifold internally defines F and rho, so it is not clear why F and rho should be passed in.
The suspicion is that you should be using
F = [1,1,1];
rho = 1;
fun = @(x)pressXmanifold(x, F, rho);
and
x = fsolve(fun,x0,options)
together with
function f = pressXmanifold(x, F, rho)
%F = [1,1,1];
%rho = 1;
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!