Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB numerical evaluation doubt

  1. Feb 19, 2008 #1
    Hi! I need help from someone that knows something about numerical methods in MATLAB. Specifically, fsolve.

    So, I have an equation L = K(x) - F(x,y). Given the values of L and y, I need to determine the value of x numerically.

    So, here's what I've done:

    G(x) = L- K(x) + F(x,y)
    in the function m-file
    function [G] = g(x,y,L)

    I then need to evaluate G(x) numerically to find the values of x at which G(x) becomes 0. Thats my understanding of numerical determination.

    So I use the fsolve command in Matlab this way
    [x, fval] = fsolve(@g, x, options);​

    But the problem is that I need to pass multiple values of y, L and some initial estimate of x (that apparently fsolve requires) to find different values of x for different values of y. Matlab doesnt accept this. i.e. ideally I'd like to do something like

    for y=0:0.1:10

    [x, fval] = fsolve(@g(x,y,L), x, options);​


    where I can pass multiple values of y and L (array or for-loop) to find the values of x.

    Can I do this? or do I need to declare Global variables and access y and L in function g that way?

    Also, if I've no idea of what x looks like, how do I estimate an initial value?

    Would appreciate your contributions to resolving this.

  2. jcsd
  3. Feb 19, 2008 #2


    User Avatar
    Science Advisor
    Gold Member

    There are various aways to do this.
    The simplest way is to use an anonymous function (look in the help-files, as far as I remember fsolve is used to illustrate how they are used). Or, alternatively, use nested functions (where you have more than one function per file, and access to all the variables). Personally I prefer the former.

    About finding a first guess: Try plotting the function for different values for L and y; the inital guess only needs to be in the "neighborhood" in order for fsolve to be efficient.
  4. Feb 20, 2008 #3
    Hey thanks,

    I created and used the anonymous function and it worked out fine.

    for L=0:15
    g = @(x) L-K(x)+F(x,y)
    [x] = fsolve(g, x, options)

    It worked fine except when Matlab threw some warnings at some of the iterations:
    Warning, unable to determine if -4503599627370496*pi*(1+2*_Z13)/(-4503599627370496*pi+1746276232035673*i) is between 0 and 1; try to use assumptions or set option _EnvAllSolutions to true

    Setting that option to true didn't help.

    Is this an error with the way I've used fsolve? I'm using the int() command for integration in one of the functions and using syms too. The symbolic variables can sometimes cause fsolve to throw errors like these, but I've tried resolving it using double(int(...)). It worked better than the first time (meaning more iterations went through), but sort of just collapsed later.

    Any suggestions?
    Last edited: Feb 20, 2008
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook