Matlab function problems.

Discussion in 'Programmer's Corner' started by farmosh203, Feb 16, 2008.

  1. farmosh203

    Thread Starter Member

    Nov 26, 2006
    20
    0
    I was looking in the Help menu in Matlab and they gave an example using the "fsolve" command. When trying to do the same exact example, I was getting an error. Here is what the example in the Help menu says:

    "First, write an M-file that computes F, the values
    of the equations at x.

    function F = myfun(x)
    F = [2*x(1) - x(2) - exp(-x(1));
    -x(1) + 2*x(2) - exp(-x(2))];

    Next, call an optimization routine.

    x0 = [-5; -5]; % Make a starting guess at the solution
    options=optimset('Display','iter'); % Option to display output
    [x,fval] = fsolve(@myfun,x0,options) % Call optimizer"


    So I made the following into an M file:

    "function F = myfun(x)
    F = [2*x(1) - x(2) - exp(-x(1));
    -x(1) + 2*x(2) - exp(-x(2))];

    x0 = [-5; -5]; % Make a starting guess at the solution
    options=optimset('Display','iter'); % Option to display output
    [x,fval] = fsolve(@myfun,x0,options) % Call optimizer"




    When I try to run this code I get the error:

    ??? Input argument "x" is undefined.

    Error in ==> fun1 at 3
    F = [2*x(1) - x(2) - exp(-x(1));



    Does anyone know what is wrong or how I can fix this problem? Any help would be great, thanks.
     
  2. scubasteve_911

    Senior Member

    Dec 27, 2007
    1,202
    1
    I don't know what to tell you, I ran the code as given and it works perfectly.

    I named the m-file as myfun.m and ran the three given lines in succession. It gave the result as per the matlab help file.

    Steve
     
  3. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    When you call myfun, what are you passing as the input argument x?

    The error message indicates that you are not specifying a workspace variable when calling myfun.

    Dave
     
Loading...