tags:

views:

37

answers:

1

I want to make a slight change to a MATLAB function at each step in a for loop. My function is too complicated to write as anonymous. Is there any way to change an m-file function at each step?

Additional Info: My function is an equations with 8 inputs and infinitely many solutions. I want set 7 of the inputs and then use fsolve to find the 8th. vary some of these 7 fixed inputs in a for loop so that I can create a graph of the solutions to this equation.

+3  A: 

Let's make an example with two inputs, of which you want to change one. Since you claim your function is really complicated, let's write it into a file called complicated.m, which we save on the Matlab path.

function out = complicated(v1,v2,x)

out = v1*x-v2*x.^2;

Say we want to change v1 and v2 at every iteration in the loop and find a root of the polynomial and plot it

figure,hold on
for v1 = 1:5
   for v2 = 1:5
      %# define the function
      myFun = @(x)complicated(v1,v2,x);
      %# find the roots
      fzero(myFun,1)
      %# plot the function
      plot(-5:0.1:5,myFun(-5:0.1:5))
   end
end
Jonas

related questions