Message Boards Message Boards

Find root not working when attempting on function defined by module

POSTED BY: david baughman
5 Replies

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}]
POSTED BY: Daniel Lichtblau

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

POSTED BY: david baughman
Posted 10 years ago

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.

POSTED BY: Bill Simpson
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]
POSTED BY: david baughman

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.

POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract