Find root not working when attempting on function defined by module

Please help! I’m attempting to utilize the FindRoot functionality on a function with three arguments that is defined in a module that uses NDSolve. FindRoot is not working! The module is because if input values, it solves it and provides an answer… Any help is GREATLY appreciated! Happy Thanksgiving! Please see code below:

In[30]:= ClearAll
SetAttributes[{vhat, khat, w, c, xin, [Delta], v, j}, Constant]
khat = .162
xin = .028
vhat = .017

Out[30]= ClearAll

Out[32]= 0.162

Out[33]= 0.028

Out[34]= 0.017

In[35]:= Clear[yOUT, yIN, ytest, nIN, nOUT, WronsFUN, [Lambda], w]
WronsFUN[[Lambda], w, xoo_] := Module[{val}, Clear[nIN, nOUT];
nIN = NDSolve[{[Lambda] == -Iwx^(3/2) - vhatx/y[x]y’[x] -
7/2
khat
x^2/yy’[x] - khatx^3/yy’'[x],
vhat
xin^(-1/2)y[xin] + khatxin^(3/2)y’[xin] == y[xin],
y[xin] == 1}, y[x], {x, xin, 1}];
yIN[x_] = First[y[x] /. nIN];
nOUT = NDSolve[{[Lambda] == -I
wx^(3/2) - vhatx/y[x]y’[x] -
7/2
khatx^2/y[x]y’[x] - khatx^3/y[x]y’'[x],
vhat
y[1] + khat
y’[1] == -y[1], y[1] == 1}, y, {x, xin, 1}];
yOUT[x_] = First[y[x] /. nOUT];
val = yIN[xoo]*yOUT’[xoo] - yOUT[xoo]*yIN’[xoo]; val]

In[37]:= FindRoot[WronsFUN[j, 21.67, .038], {j, .1 - .1*I}]

During evaluation of In[37]:= NDSolve::ndnum: Encountered non-numerical value for a derivative at x == 0.028`. >>

During evaluation of In[37]:= ReplaceAll::reps: {NDSolve[{j==(0. -21.67 I) x^(3/2)-(0.017 x (y^[Prime])[x])/y[<<1>>]-(0.567 x^2 (y^[Prime])[x])/y[<<1>>]-(0.162 x^3 (y^[Prime][Prime])[x])/y[<<1>>],0.101594 y[0.028]+0.000759018 (y^[Prime])[0.028]==y[0.028],y[0.028]==1},y[x],{x,0.028,1}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

During evaluation of In[37]:= NDSolve::ndnum: Encountered non-numerical value for a derivative at x == 1.`. >>

During evaluation of In[37]:= ReplaceAll::reps: {NDSolve[{j==(0. -21.67 I) x^(3/2)-(0.017 x (y^[Prime])[x])/y[<<1>>]-(0.567 x^2 (y^[Prime])[x])/y[<<1>>]-(0.162 x^3 (y^[Prime][Prime])[x])/y[<<1>>],0.017 y[1]+0.162 (y^[Prime])[1]==-y[1],y[1]==1},y[x],{x,0.028,1}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

Out[37]= {j → 0.1 - 0.1 I}

In[38]:= WronsFUN[7.07812 - 1.98627*I, 21.67, .038]

Out[38]= 1.6058 - 0.555075 I

You really should reformat so that input is clearly demarcated and easy to cut and paste. Also you might do better by making the function a “black box” that only evalautes on explicit numeric input. This can be done like so.

WronsFUN[\[Lambda]_?NumberQ, w_?NumberQ, xoo_?NumberQ] := ...

Don’t forget to first Clear that function so as to remove the existing definition.

Here is the code cleaner. Sorry about that:

ClearAll
    SetAttributes[{vhat, khat, w, c, xin, \[Delta], v, j}, Constant]
    khat = .162
    xin = .028
    vhat = .017

Clear[yOUT, yIN, ytest, nIN, nOUT, WronsFUN, \[Lambda], w]
WronsFUN[\[Lambda]_, w_, xoo_] := Module[{val}, Clear[nIN, nOUT];
  nIN = NDSolve[{\[Lambda] == -I*w*x^(3/2) - vhat*x/y[x]*y'[x] - 
       7/2*khat*x^2/y[x]*y'[x] - khat*x^3/y[x]*y''[x], 
     vhat*xin^(-1/2)*y[xin] + khat*xin^(3/2)*y'[xin] == y[xin], 
     y[xin] == 1}, y[x], {x, xin, 1}];
  yIN[x_] = First[y[x] /. nIN];
  nOUT = NDSolve[{\[Lambda] == -I*w*x^(3/2) - vhat*x/y[x]*y'[x] - 
       7/2*khat*x^2/y[x]*y'[x] - khat*x^3/y[x]*y''[x], 
     vhat*y[1] + khat*y'[1] == -y[1], y[1] == 1}, y[x], {x, xin, 1}];
  yOUT[x_] = First[y[x] /. nOUT];
  val = yIN[xoo]*yOUT'[xoo] - yOUT[xoo]*yIN'[xoo]; val]

FindRoot[WronsFUN[j, 21.67, .038], {j, .1 - .1*I}]

WronsFUN[7.07812 - 1.98627*I, 21.67, .038]

The problem happens before you ever get to FindRoot

In[1]:= khat = .162; xin = .028; vhat = .017; {\[Lambda], w, xoo} = {j, 21.67, .038};
  NDSolve[{\[Lambda] == -I*w*x^(3/2) - vhat*x/y[x]*y'[x] - 7/2*khat*x^2/y[x]*y'[x] - khat*x^3/y[x]*y''[x], 
     vhat*xin^(-1/2)*y[xin] + khat*xin^(3/2)*y'[xin] == y[xin], y[xin] == 1}, y[x], {x, xin, 1}]

During evaluation of In[1]:= NDSolve::ndnum: Encountered non-numerical value for a derivative at x == 0.028`. >>

Out[4]= NDSolve[{j == (0. - 21.67 I) x^(3/2) - (0.017 x Derivative[1][y][x])/y[x] - (0.567 x^2 Derivative[1][y][x])/y[x] - (
    0.162 x^3 (y^\[Prime]\[Prime])[x])/y[x], 0.101594 y[0.028] + 0.000759018 Derivative[1][y][0.028] == y[0.028],
   y[0.028] == 1}, y[x], {x, 0.028, 1}]

So NDSolve is failing with the very first point. Study that until you can resolve the problem.

The NumberQ WORKED!!! THANK YOU SOOO MUCH!!! I WAS BANGING MY HEAD ON THIS FOR 2 DAYS!!!

I saw the last response after recoding to get something workable. I thought I would show it so that there is a complete solution in the responses.

khat = .162;
xin = .028;
vhat = .017;

WronsFUN[\[Lambda]_?NumberQ, w_?NumberQ, xoo_?NumberQ] := Module[
  {val, nIN, nOUT},
  nIN = NDSolve[{\[Lambda] == -I*w*x^(3/2) - vhat*x/y[x]*y'[x] - 
       7/2*khat*x^2/y[x]*y'[x] - khat*x^3/y[x]*y''[x], 
     vhat*xin^(-1/2)*y[xin] + khat*xin^(3/2)*y'[xin] == y[xin], 
     y[xin] == 1}, y[x], {x, xin, 1}];
  yIN[x_] = First[y[x] /. nIN];
  nOUT = NDSolve[{\[Lambda] == -I*w*x^(3/2) - vhat*x/y[x]*y'[x] - 
       7/2*khat*x^2/y[x]*y'[x] - khat*x^3/y[x]*y''[x], 
     vhat*y[1] + khat*y'[1] == -y[1], y[1] == 1}, y[x], {x, xin, 1}];
  yOUT[x_] = First[y[x] /. nOUT];
  val = yIN[xoo]*yOUT'[xoo] - yOUT[xoo]*yIN'[xoo]; val]

With those definitions the following now seems to work as expected. Or at least not complain about working conditions, salary, or the like.

FindRoot[WronsFUN[j, 21.67, .038], {j, .1 - .1*I}]