# Help in Mathematica

• Mathematica
Hello, I want somebody help me in Mathematica
My code that I wrote most of it is right ,the two first function are correct ( templet & Bisection)but the problem I faced when I wrote the function (firstresult) when I wrote in it the for loop and the if conditions the errors start to appear so please somebody help me in this problem
my programme is attached
and thank you for the help

#### Attachments

• rewrite my system.nb
35.6 KB · Views: 334

1: Get rid of NumberForm inside your Bisection Module. That does not do what you want, whatever that is, and is most of the reason your code is blowing up. Maybe you want to replace that with w=c but I can't even guess.

2: In your Finalresult Module you have For[n=0, b[..]<b[..],n=n/2,RestOfYourForLoop]. If you start with n=0 and you keep dividing n by 2 I don't know how much closer you want to get to zero, but it isn't changing.

After I get rid of the NumberForm and run your code it prints
Q a w
and then grinds away for a long time repeatedly calling Bisection and finally halts without any more output.

You have been fighting this page of code for months. Perhaps someone with some Mathematica experience could write what you really need in a few hours.

Mr Bill thank you for your help and spending your time to solve my problem,but I really need somebody's help in the Mathematica espacially that it is the first time I use it and My Doctors at my university do not have a lot of experience using it. So I really need your help and do not take the following to offense, but if it pleases you to pay for your service I do not mind at all. 90% of my program is finished and Iam facing problem in the rest .
I do not face any problem in the two first functions(Tmp & bisection)both of them work but my problem is now in consructing a new function that finds the local minimum of the function Bisection, I used FindMinimum[Bisection[w,w+100000,z,100],{z,3.7}] but it does not work, so I try to consruct a function that finds the minimum I called it Finalresult,but unfortunately when I worte the for loop and the if condition in it ,it also doesnot work and I do not Know what is the problem.(remark:n=0.2 not 0 this was a mistake)
I will explain to you my aim, if you see the file which is called Bisection,you will see the data,for example:
when Q=0, a=3.7 ,w=93438.975763697 ,
Q=0, a=3.9 ,w=92932.036100504
Q=0, a=4.1 ,w= 93086.1120294133
since the value of w decreased then it increased so there is a local minimum point which will be a=3.95 and w=92912 and that is my aim how I can find this result. And thank you very much

#### Attachments

• Bisection (2).nb
40.7 KB · Views: 298
• rewrite my system.nb
35.8 KB · Views: 309
• Untitled-15.nb
44 KB · Views: 312
Code:
In[1]:= Off[FindRoot::lstol];(*This is dangerous, turning off FindRoot accuracy warnings*)
tmp[a_, z_, w_?NumericQ] := Module[{solution},
solution = NDSolve[{y1'[t] == y5[t], y2'[t] == y6[t], y3'[t] == y7[t], y4'[t] == y8[t],
y5'[t] == y9[t], y6'[t] == y10[t], y7'[t] == y11[t], y8'[t] == y12[t],
y9'[t] == y13[t], y10'[t] == y14[t], y11'[t] == y15[t], y12'[t] == y16[t],
y13'[t] == -z^4 y1[t] + 2 z^2 y9[t] + z^2 w (-t + t^2) y17[t] + a y9[t],
y14'[t] == -z^4 y2[t] + 2 z^2 y10[t] + z^2 w (-t + t^2) y18[t] + a y10[t],
y15'[t] == -z^4 y3[t] + 2 z^2 y11[t] + z^2 w (-t + t^2) y19[t] + a y11[t],
y16'[t] == -z^4 y4[t] + 2 z^2 y12[t] + z^2 w (-t + t^2) y20[t] + a y12[t],
y17'[t] == y21[t], y18'[t] == y22[t], y19'[t] == y23[t], y20'[t] == y24[t],
y21'[t] == (-1 + 2 t) y1[t] + z^2 y17[t] - a y29[t],
y22'[t] == (-1 + 2 t) y2[t] + z^2 y18[t] - a y30[t],
y23'[t] == (-1 + 2 t) y3[t] + z^2 y19[t] - a y31[t],
y24'[t] == (-1 + 2 t) y4[t] + z^2 y20[t] - a y32[t],
y25'[t] == y29[t], y26'[t] == y30[t], y27'[t] == y31[t], y28'[t] == y32[t],
y29'[t] == -y21[t] + z^2 y25[t], y30'[t] == -y22[t] + z^2 y26[t],
y31'[t] == -y23[t] + z^2 y27[t], y32'[t] == -y24[t] + z^2 y28[t],
y1[0] == y2[0] == y3[0] == y4[0] == y5[0] == y6[0] == y7[0] == y8[0] ==
y10[0] == y11[0] == y12[0] == y13[0] == y15[0] == y16[0] == y17[0] ==
y18[0] == y19[0] == y20[0] == y21[0] == y22[0] == y24[0] == y25[0] ==
y26[0] == y27[0] == y28[0] == y29[0] == y30[0] == y31[0] == 0,
y9[0] == y14[0] == y23[0] == y32[0] == 1},
{y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12, y13, y14, y15, y16, y17, y18,
y19, y20, y21, y22, y23, y24, y25, y26, y27, y28, y29, y30, y31, y32}, {t, 0, 1}
];
Det[First[
{{y1[1], y2[1], y3[1], y4[1]},
{y5[1], y6[1], y7[1], y8[1]},
{y17[1], y18[1], y19[1], y20[1]},
{y25[1], y26[1], y27[1], y28[1]}} /. solution]]
];

Print["Q", "   ", "a", "       ", "w"];

For[ a = 0, a <= 30, a = a + 10,
For[z = 3.7, z <= 5, z = z + 0.2,
For[w = 1000, w <= 2000000, w = w + 100000,
If[tmp[a, z, w] > 0 && tmp[a, z, w + 100000] < 0 ,
Print[a,"   ",z,"   ",NumberForm[v/.FindRoot[tmp[a,z,v]==0,{v,w+50000,w,w+100000}],16]];
]]]]

Q   a       w
0   3.7   93438.9803800478
0   3.9   92932.0414438437
0   4.1   93086.1116093325
0   4.3   93830.3955393992
0   4.5   95112.0235274916
0   4.7   96891.6976268166
0   4.9   99140.5585931292
10   3.7   110356.5628367369
10   3.9   108616.4756344198
10   4.1   107721.5534510192
10   4.3   107567.2986911633
10   4.5   108074.499965424
10   4.7   109183.0371712484
10   4.9   110847.3985368835
20   3.7   128292.9949966425
20   3.9   125184.9890825853
20   4.1   123127.9918062165
20   4.3   121979.6379694277
20   4.5   121631.1326512834
20   4.7   121998.9691454404
20   4.9   123018.9686603545
30   3.7   147215.7116923535
30   3.9   142610.6557686414
30   4.1   139283.0661366297
30   4.3   137048.7686149517
30   4.5   135766.3139890595
30   4.7   135326.4015415545
30   4.9   135644.2668804859

I suspect this could be further simplified and made more numerically accurate and dependable and made to do what you really want to accomplish, but I don't have the information needed to be able to do that.

If you take out that
Code:
Off[FindRoot::lstol];
then you can identify which values of your parameters result in warning/error messages. With those values you can Plot your tmp function over your range of w and see why it is not converging. Perhaps that Plot will show you this is a serious problem that you must correct. Perhaps your function is just nearly "flat" for some parameters and the result of FindRoot will be good enough for what you need.

If you change
Code:
tmp[a, z, w] > 0 && tmp[a, z, w + 100000] < 0
to
Code:
Sign[tmp[a, z, w]!=Sign[tmp[a, z, w + 100000]
you do not seem, at least with the current values of parameters you are using, to find any additional roots. That seems questionable to me, but I don't know enough about your problem to know whether this is correct or not.

Last edited:
Hello,Mr Bill,thank you for simplified my code,now I have still something to accomplich which is using FindMinimum function,I do not Know how to use it to find the minimum value of w (the result of the FindRoot function)and the value of a.As before I gave an example:
we have
Q a w
0 3.7 93439
0 3.9 92932
0 4.1 93086
Now When I use findMinimum function it should gives me the minimal value which is a= 3.95 when w= 92912
but when I used it I got some errors that I could not solve
So I need your help,and thank you again.(my trail is attached)

#### Attachments

• system with findroot - Copy.nb
21.1 KB · Views: 306
All of your determinants are monotonically decreasing with w and go from being positive to negative with a single root where the determinant is zero. Your bisection and my FindRoot are finding the value of w where the determinant is zero, within the precision of the approximate floating point numbers. Any tiny nonzero remainder is nothing more than floating point error.

For different values of a and z there is a different value of w where tmp[a,z,w] is zero. Finding the minimum value of that w where the tmp is zero may or may not make any sense.

The following shows this. Look at the graph which shows the plot of tmp for each of your solutions.

After studying and understanding this if you can tell me what you really need to accomplish then I'll make the last changes.

Code:
In[1]:= Off[FindRoot::lstol];(*This is dangerous, turning off FindRoot accuracy warnings*)
tmp[a_, z_, w_?NumericQ] := Module[{solution},
solution = NDSolve[{y1'[t] == y5[t], y2'[t] == y6[t], y3'[t] == y7[t], y4'[t] == y8[t],
y5'[t] == y9[t], y6'[t] == y10[t], y7'[t] == y11[t], y8'[t] == y12[t],
y9'[t] == y13[t], y10'[t] == y14[t], y11'[t] == y15[t], y12'[t] == y16[t],
y13'[t] == -z^4 y1[t] + 2 z^2 y9[t] + z^2 w (-t + t^2) y17[t] + a y9[t],
y14'[t] == -z^4 y2[t] + 2 z^2 y10[t] + z^2 w (-t + t^2) y18[t] +a y10[t],
y15'[t] == -z^4 y3[t] + 2 z^2 y11[t] + z^2 w (-t + t^2) y19[t] +a y11[t],
y16'[t] == -z^4 y4[t] + 2 z^2 y12[t] + z^2 w (-t + t^2) y20[t] +a y12[t],
y17'[t] == y21[t], y18'[t] == y22[t], y19'[t] == y23[t], y20'[t] == y24[t],
y21'[t] == (-1 + 2 t) y1[t] + z^2 y17[t] - a y29[t],
y22'[t] == (-1 + 2 t) y2[t] + z^2 y18[t] - a y30[t],
y23'[t] == (-1 + 2 t) y3[t] + z^2 y19[t] - a y31[t],
y24'[t] == (-1 + 2 t) y4[t] + z^2 y20[t] - a y32[t],
y25'[t] == y29[t], y26'[t] == y30[t], y27'[t] == y31[t], y28'[t] == y32[t],
y29'[t] == -y21[t] + z^2 y25[t], y30'[t] == -y22[t] + z^2 y26[t],
y31'[t] == -y23[t] + z^2 y27[t], y32'[t] == -y24[t] + z^2 y28[t],
y1[0] == y2[0] == y3[0] == y4[0] == y5[0] == y6[0] == y7[0] == y8[0] ==
y10[0] == y11[0] == y12[0] == y13[0] == y15[0] == y16[0] == y17[0] ==
y18[0] == y19[0] == y20[0] == y21[0] == y22[0] == y24[0] == y25[0] ==
y26[0] == y27[0] == y28[0] == y29[0] == y30[0] == y31[0] == 0,
y9[0] == y14[0] == y23[0] == y32[0] == 1}, {y1, y2, y3, y4, y5,
y6, y7, y8, y9, y10, y11, y12, y13, y14, y15, y16, y17, y18,
y19, y20, y21, y22, y23, y24, y25, y26, y27, y28, y29, y30, y31,
y32}, {t, 0, 1}
];
Det[First[
{{y1[1], y2[1], y3[1], y4[1]},
{y5[1], y6[1], y7[1], y8[1]},
{y17[1], y18[1], y19[1], y20[1]},
{y25[1], y26[1], y27[1], y28[1]}} /. solution]]
];

Print["Q     a           w          tmp[a,z,root=w]"];

Show[Reap[For[ a = 0, a <= 30, a = a + 10,
For[z = 3.7, z <= 5, z = z + 0.2,
For[w = 1000, w <= 2000000, w = w + 100000,
If[tmp[a, z, w] > 0 && tmp[a, z, w + 100000] < 0 ,
Sow[Plot[tmp[a,z,v], {v,w,w+100000}, PlotRange->{{90000,150000},{-20,20}}]];
root = v /. FindRoot[tmp[a, z, v] == 0, {v, w + 50000, w, w + 100000}];
Print[a, "   ", z, "   ", NumberForm[root, 16], "  ", tmp[a, z, root]];
]]]]][[2, 1]], ImageSize->720]

During evaluation of In[1]:= Q     a           w          tmp[a,z,root=w]
During evaluation of In[1]:= 0   3.7   93438.9803800478  -8.25097*10^-13
During evaluation of In[1]:= 0   3.9   92932.0414438437  -2.50023*10^-12
During evaluation of In[1]:= 0   4.1   93086.1116093325  -5.79413*10^-12
During evaluation of In[1]:= 0   4.3   93830.3955393992  -2.13278*10^-12
During evaluation of In[1]:= 0   4.5   95112.0235274916  -3.35412*10^-13
During evaluation of In[1]:= 0   4.7   96891.6976268166  -2.72704*10^-11
During evaluation of In[1]:= 0   4.9   99140.5585931292  -2.63948*10^-11
During evaluation of In[1]:= 10   3.7   110356.5628367369  -3.05622*10^-13
During evaluation of In[1]:= 10   3.9   108616.4756344198  6.13706*10^-12
During evaluation of In[1]:= 10   4.1   107721.5534510192  -3.32333*10^-11
During evaluation of In[1]:= 10   4.3   107567.2986911633  3.00603*10^-13
During evaluation of In[1]:= 10   4.5   108074.499965424  -1.07373*10^-10
During evaluation of In[1]:= 10   4.7   109183.0371712484  7.43001*10^-11
During evaluation of In[1]:= 10   4.9   110847.3985368835  2.33465*10^-11
During evaluation of In[1]:= 20   3.7   128292.9949966425  2.73018*10^-11
During evaluation of In[1]:= 20   3.9   125184.9890825853  1.27769*10^-12
During evaluation of In[1]:= 20   4.1   123127.9918062165  2.96876*10^-11
During evaluation of In[1]:= 20   4.3   121979.6379694277  -6.72119*10^-11
During evaluation of In[1]:= 20   4.5   121631.1326512834  1.21746*10^-10
During evaluation of In[1]:= 20   4.7   121998.9691454404  -3.86911*10^-10
During evaluation of In[1]:= 20   4.9   123018.9686603545  -4.40429*10^-10
During evaluation of In[1]:= 30   3.7   147215.7116923535  1.91461*10^-10
During evaluation of In[1]:= 30   3.9   142610.6557686414  2.64546*10^-10
During evaluation of In[1]:= 30   4.1   139283.0661366297  -1.75087*10^-10
During evaluation of In[1]:= 30   4.3   137048.7686149517  2.58199*10^-10
During evaluation of In[1]:= 30   4.5   135766.3139890595  6.92004*10^-9
During evaluation of In[1]:= 30   4.7   135326.4015415545  -5.24718*10^-10
During evaluation of In[1]:= 30   4.9   135644.2668804859  8.24087*10^-9

Out[4]= ...PlotSnipped...

Last edited:
Hello Mr Bill you did not understand me
I want to find the smallest value of w which makes Temp is zero for a certain value of z
When see the results of w it first it decreases then increases so for sure there is a minimum value of w that makes Temp=0and that what Iam looking for
Thank you

As you see the results when Q=0, a=3.7&w=93439, a=3.9&w=92933, a=4.1&w=93086 w decreases then it increases so the smallest value of w that makes Temp=0 is 92912 when z=3.95 and that what I want to get

I am not yet finished thinking.

This works for a==0, but for other values of a the convergence is slow.
Code:
In[2]:= a = 0;
result = NMinimize[{w,tmp[a,z,w]==0,3.7<=z<=5, 80000<=w<=150000}, {z,w}];
Print["tmp[",a,",",First[z/.Rest[result]],",",First[w/.Rest[result]],"]==0"]

Out[3]= tmp[0,3.95008,92911.7]==0

I believe in your last message your Q=0 should be a=0 and your a=3.7 should be z=3.7.
Or perhaps you should not have used a and instead used Q for your variable name.
Your labels do not seem to correctly match your variable names. This is not good.
Having correct labels and variable names helps you understand what to do and when an error has been made.

Last edited:
This is a terrible way of doing this.

Code:
In[2]:= For[a = 0, a <= 30, a = a + 10,
lo = 3.7; hi = 5.;
Do[
w1=w/.FindRoot[tmp[a, lo, w]==0, {w, (80000+150000)/2, 80000, 150000}];
w2=w/.FindRoot[tmp[a, lo+(hi-lo)*.25, w]==0, {w, (80000+150000)/2, 80000, 150000}];
w3=w/.FindRoot[tmp[a, lo+(hi-lo)*.5, w]==0, {w, (80000+150000)/2, 80000, 150000}];
w4=w/.FindRoot[tmp[a, lo+(hi-lo)*.75, w]==0, {w, (80000+150000)/2, 80000, 150000}];
w5=w/.FindRoot[tmp[a, hi, w]==0, {w, (80000 + 150000)/2, 80000, 150000}];
Which[
w1 > w2 && w2 < w3, hi = lo + (hi - lo)*.5,
w2 > w3 && w3 < w4, lo = lo + (hi - lo)*.25; hi = lo + (hi - lo)*.75,
w3 > w4 && w4 < w5, lo = lo + (hi - lo)*.5,
True, Print["Bisection Failed!"]
]
, {10}];
Print["tmp[", a, ",", lo + (hi - lo)*.5, ",", w3, "]==0"]
]

Out[2]=
tmp[0,3.95023,92911.7]==0
tmp[10,4.24409,107541.]==0
tmp[20,4.49323,121631.]==0
tmp[30,4.71309,135325.]==0

It is straightforward to optimize that to eliminate the redundant recalculations in each iteration, but that doesn't resolve the real problems.

There should be a much much better way of doing this. This can fail in many different ways. It depends on the function being concave up, on having a solution within the range, converging at an appropriate rate, etc. There must be a much better way of using some available function that can do this. Unfortunately at the moment I just can't see it. Perhaps someone else can see the obvious way of doing this. Perhaps I'll think of something. There should be a simple clean way to get NMinimize to do all of this quickly.

Last edited:
Hello Mr Bill,thank you very much for spending your time in solving my problem ,I know this method you used is not the best one but at least it works.Now my quation is if I want to find the minimum for more than 30 what changes I will have to do ? just change the interval a<=100 instead of a<=30 or there is something more.
Also I hope that if you Know somebody who has experience in mathematica to solve this problem in better way to ask him
and thank you for every thing

if I want to find the minimum for more than 30 what changes I will have to do?

You must be certain that for each value of 'a' there is a single minima for 'w', you must know the correct range for z and you must know the correct range of values for 'w' so that the FindMinimum will find a solution for 'w' in that range. None of the code I wrote checks to make certain this is true.

I changed the problem so that 'z' did not step by 0.2, but by 0.05, and looked at the plot to see that all the curves did cross cross the axis, but I did not look at changing the values for 'a'.

You might also improve the code so that it does not just do 10 iterations, but that it iterates until a sufficiently good answer has been found, but will not iterate forever if there is an error.

Mr Bill ,I want to ask you about the code you wrote to me in the last time,I tried to find the
minimum zeros for more than 30 but the code gives me that the bisection failed ,also at a=30 there is no minimum values because the the values are decreasing ,so we have to put in our mind that for some points like at a=30 & 70& 90 there are not any minimum zeros .I hope that you give me the changes that I should do in the code to get the correct answer.And thank you again very much for your help and Iam grateful for your cooperation.

#### Attachments

• try NM 100.nb
41.1 KB · Views: 319
You carefully described how there would be a minimum for w between z=3.7 and z=5. You wrote code depending on that being true. I helped you write code depending on that being true. I explained how this was a very bad way of doing this and that it would fail if the conditions were not met. Then you changed the problem so the conditions are not met and the program fails. First it fails because there is no minimum between 3.7 and 5. Then it fails even more because of something to do with the root. And finally it appears to fail completely.

Note: Mathematica will only print the same error message 3 times and then it prints a warning that it is not going to print that error message any more. This does not mean there are no more such errors, only that it is hiding them from you.

Run this and study this carefully.

For[a = 0, a <= 200, a = a + 10,
lo = 3.7; hi = 5.;
Do[
w1 = w/.FindRoot[tmp[a, lo, w] == 0, {w, (80000 + 300000)/2, 80000, 300000}];
w2 = w/.FindRoot[tmp[a, lo + (hi - lo)*.25, w] == 0, {w, (80000 + 300000)/2, 80000, 300000}];
w3 = w/.FindRoot[tmp[a, lo + (hi - lo)*.5, w] == 0, {w, (80000 + 300000)/2, 80000, 300000}];
w4 = w/.FindRoot[tmp[a, lo + (hi - lo)*.75, w] == 0, {w, (80000 + 300000)/2, 80000, 300000}];
w5 = w/.FindRoot[tmp[a, hi, w] == 0, {w, (80000 + 300000)/2, 80000, 300000}];
Which[
w1 > w2 && w2 < w3, hi = lo + (hi - lo)*.5,
w2 > w3 && w3 < w4, lo = lo + (hi - lo)*.25; hi = lo + (hi - lo)*.75,
w3 > w4 && w4 < w5, lo = lo + (hi - lo)*.5,
w1 < w2 && w2 < w3, hi = lo + (hi - lo)*.5; Print["1<2<3"],
w3 > w4 && w4 > w5, lo = lo + (hi - lo)*.5; Print["3>4>5"],
True, Print["Err", {w1, w2, w3, w4, w5}]
], {10}];
Print["tmp[", a, ",", (lo + hi)/2., ",", w3, "]==", tmp[a, (lo + hi)/2., w3]];
Print[Show[Table[Plot[tmp[a, zi, w], {w, 80000, 300000}], {zi, 3.7, 5., .05}]]];
]

Notice how the solution begins to fail. Notice how I suspect an upper limit of 5 for z may be inappropriate. Notice how when roots approach 300000 the code begins to fail even more. And notice the final breakdown for a>120 and it begins claiming the best z is (3.7+5)/2 and not even trying to find a minimum.

I suspect this may be more than bad Mathematica code, this may be bad mathematics and a bad approach to solving the problem. It is certainly giving you incorrect results.

Can you perhaps stop thinking about Mathematica and go back to rethinking the fundamental mathematics and what a good stable dependable correct mathematical solution to your problem would be? And after you have that only then begin to think about how you might want to calculate that solution?

I fear that my fumbling and patching your code again and again is only helping you get to an incorrect answer.

Help me Mathematica

Hello Mr Bill ,thank very much for your help and I want to tell you that I found another way to find the minimum values but also I faced a problem in the range of FindRoot function ,I got the minimum values for Q= 0,10,20,30,40,50 but after 50 the program stops and gives me some errors.May you help me in this method.
And thank you again very much

#### Attachments

• mim - Copy - Copy - Copy.nb
31 KB · Views: 301
I attempted to add some diagnostic display information to your

Code:
...
Finalresult[] := Module[{},
n = 0.2;
z0 = 3.7;
z1 = z0 + n;

Print["tmp[", {a, {z0, z1}, {w + 50000, w + 100000}}, "]"];
Print[Show[ Table[Plot[tmp[a, zi, v], {v, w+50000, w+100000}], {zi, z0, z1, .05}]]];
...

to try to show your code is failing. That tries to demonstrate the range of values you are using in your tmp function.

As you can see from the plots that makes, for a==0 there are nice roots for FindRoot to discover, but perhaps I am being fooled into thinking I understand your code and it is not doing anything like I think it is. I am surprised that your code does not fail for a==10 or a==20 or a==30 because there do not seem to be roots in the range of values you are using. Then for a==40 or a==50 there are nice roots to be found. But for a==60 and beyond there are no roots to be found, FindRoot looks at the range of values you have told it that it must search within, it finds that at one edge of the range the slope of the function says it must search outside the range, it prints a warning message and fails.

Different problem.

It appears you have experience with other programming languages where {} are used to group code statements. Example

Code:
If[g < r, {
z0 = z1;
r = g;}]

and

Code:
For[n = 0.2, n > 0.01, {
z1 = z0 + n;
g = v /. FindRoot[tmp[a, z1, v] == 0, {v, w + 50000, w, w + 100000}];
If[g > r, {
z0 = z0 - n;
n = n/2;
z1 = z0 + n;
r = v /.
FindRoot[tmp[a, z0, v] == 0, {v, w + 50000, w, w + 100000}];}];
If[g < r, {
z0 = z1;
r = g;}]}]

Mathematica does not use {} to group code statements. The Mathematica language does many things differently from other languages. People who have used other languages believe they must use {} to group code statements. I don't know what to tell you to read to understand how Mathematica is different. Mathematica is not Java. Mathematica is not c. Mathematica is much much more different from these languages than you or even I can imagine.

My recommendation: Modify the Print and Plot statement I showed above to make it perfectly clear exactly what ranges of values tmp and FindRoot are allowed to use. Keep modifying this until you can see exactly why the error messages are being displayed and what you need to do to fix this.