3

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.

2
  • Can you provide any more detail about the type of change you'd like to make? Commented Sep 9, 2010 at 1:14
  • 1
    You do not need using an anonimous function for this kind of problem! Use just a wrapper function wich parametrizes your 8-Input function Commented Sep 9, 2010 at 12:17

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.