I want to solve an equation for a different value in each cycle of a for loop. I usually include the value that I am solving for in the m-file function but I can't change the m file at each cycle in the for loop. Is there a way to solve the use fsolve but for a value !=0.
+2
A:
You can use an anonymous function. So if your paramterised function is:
function y = f(x, c)
...
end
then you can iterate over different parameters thus:
for c = 0:10
fsolve(@(x)f(x,c), x0);
end
(Untested)
Oli Charlesworth
2010-09-12 11:15:31
This is essentially correct, but if the goal is to solve for various right hand sides in fsolve, then the loop might look effectively like this: for i = 1:numel(rhs),result(i) = fsolve(@(x)f(x) - rhs(i), x0);end
woodchips
2010-09-12 12:28:34
@woodchhips: Yes, I suppose so! I had assumed that one could incorporate the RHS constant into the function itself, but your method is better.
Oli Charlesworth
2010-09-12 16:34:45