tags:

views:

48

answers:

1

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
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
@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

related questions