0

I'm trying to solve these two equations numerically:

diff1[r3_] := z2 /. NDSolve[
    {zed2[θ2, θ3[r2, r3]] == z2'[r2], z2[lastr2] == lastz2}
  , z2
  , {r2, lastr2, lastr2 + 10 bit}
][[ 1]][[1]];

diff2[r2_] := z3 /. NDSolve[
    {zed3[θ3[r2, r3], θ4] == z3'[r3], z3[lastr3] == lastz3}
  , z3
  , {r3, lastr3, lastr3 + 10 bit}
][[ 1]][[1]];

Where for a given input value of r3 or r2 each returns an intepolation function in the opposite either r2 or r3. I've tried to solve this numerically in the following way:

FindRoot[{diff1[r3][r2] == (r2 - r1)/Tan[θ2] + z1 - t1, 
 diff2[r2][r3] == (t2 + z4) - (r4 - r3)/Tan[θ4]}, {{r2, 1, 
 r1, r4}, {r3, 1, r1, r4}}];

Where all but r2 and r3 are known. If I type say diff1[0.1][2] I get a single value as expected but FindRoot doesn't seem to like this and I can't work out why, is FindRoot not simply substituting a number in numerically or is it something like it's trying to differentiate with respect to a numerical value?

Any help would be appreciated

Kuba
  • 136,707
  • 13
  • 279
  • 740

1 Answers1

1

You need to restrict the functions to only evaluate when given a number, else FindRoot first tries to solve them symbolically. This is super common. Try:

diff1[r3_?NumericQ] := 
  z2 /. NDSolve[{zed2[θ2, θ3[r2, r3]] == z2'[r2], 
       z2[lastr2] == lastz2}, 
      z2, {r2, lastr2, lastr2 + 10 bit}][[1]][[1]];

diff2[r2_?NumericQ] := 
  z3 /. NDSolve[{zed3[θ3[r2, r3], θ4] == z3'[r3], 
       z3[lastr3] == lastz3}, 
      z3, {r3, lastr3, lastr3 + 10 bit}][[1]][[1]];

(I can't check this works as you haven't provided the values for the various constants in your code).

Kuba
  • 136,707
  • 13
  • 279
  • 740
SPPearce
  • 5,653
  • 2
  • 18
  • 40