Changing Objective Function in fsolve
Show older comments
Hello!
This seems like a simple issue, but I've been unable to spot the solution...
If I have a function with two outputs, let's say something like:
[A,B]=myfun(x,y,z)
I have a situation where I need to be able to solve for either A or B under different circumstances. I know that fsolve automatically takes the objective function as the first entry in the output vector (in this case, A). So I currently have two functions (identical script, except for the order out outputs):
[A,B]=myfun(x,y,z), and [B,A]=myfun(x,y,z)
It would be a lot easier if I could tell my solver which output entry to solve for - is there a way of achieving this?
Many thanks,
Miles
Answers (1)
Star Strider
on 23 Aug 2017
One approach:
y = shell_fcn(args,opt)
[B,A] = myfun(args(1),args(2),args(3));
switch opt
case 1
y = A;
case 2
y = B;
end
end
Then something like this ...
opt = 1;
x1 = fsolve(@(args) shell_fcn(args, opt), x0);
opt = 2;
x2 = fsolve(@(args) shell_fcn(args, opt), x0);
NOTE — This is UNTESTED CODE. You will probably have to experiment with it.
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!