Message Boards Message Boards

1
|
15709 Views
|
17 Replies
|
18 Total Likes
View groups...
Share
Share this post:

Problems with ArcSin[], FindRoot[], invisible characters, new discussions

Posted 11 years ago
Hi,

If I run a code with sin(ArcSin) imbeded in it , the output I am getting is ;
if x=1(say)
sin(ArcSin[1]),instead of a numerical values,i.e. 1.

NOTE: I tried running, sin(ArcSin[1]) independently ; I do get 1.

I am confused, such a silly thingĀ  but its a bug I cant fix in my program.

A help will be appreciated.

Hope to hear back soon

SD
Attachments:
POSTED BY: S D
17 Replies
Posted 11 years ago

When I read your post six days ago it was not clear exactly what the question was, "I have a bunch of numbers and they are wrong, what should I do?"

If I only use N and do not use Re or Abs on any of your numbers to hide what the value might be in your Print it is clear that r1 is real, r2 is complex and not equal to r1, h is real, h1 is complex and not equal to h and Energy is complex. Each of those seems to contradict what you say the results must be. Perhaps, if you are lucky, there is only one small, simple, easy to find and fix error. Hopefully it is not that every calculation has several different and very hard to find errors.

Rather than printing out a list of 30 incorrect results, can you choose a single value for h1, carefully think what the result should be for that value of h1, perhaps plot the function you are using FindRoot on and see if it is finding the wrong root? Or see if there is some other error in what is being given to FindRoot? Or is the equation for Energy perhaps incorrect? The idea is to focus on a small problem and try to discover and correct any error in that. If that works then you can see whether this gives the correct answer for your long list of results.

I don't even know where to begin to try to decide whether there is an error in your Energy equation or in the FindRoot equation.

POSTED BY: Bill Simpson
Posted 11 years ago

Any Suggestion?

POSTED BY: S D
Posted 11 years ago

Bill You are a savior...

I had been trying to check if the answers are correct or not .By simply choosing R1 and R2 similar we should have h=h1 and r1=r2 . But, I am having issue understanding the cause of the problem ... I checked my derivation and the equations used are correct also if I use a different method of simulation answers are what we expect .. but as the programming will get complex I need to figure using the find root and work with it.Hence need your advice :)

Following is the code in NB format ( Also see attached) rhop = 1.781 10^3; rhol = 0.781 10^3; g = 9.80665; T = 0.4/10^2; q = Sqrt[((rhop - rhol) g)/T]; bb = 0.0000001; alpha1 = Pi/6; R1 = 900/10^9; R2 = 900/10^9; l0 = 400/10^9; E1 = 1.781072418 ; alpha2 = Pi/6; For[h1 = 400/10^9, h1 < 1.2 R1 - l0, h1 = h1 + 10/10^9, Psi1 = ArcSin[r1/R1] - alpha1; Psi2 = ArcSin[r2/R2] - alpha2; r1 = Sqrt[(l0 + h1) (2 R1 - l0 - h1)]; a = Sqrt[bb(bb + 2r1)(bb + 2r2)(bb + 2r1 + 2r2)/(4(bb + r1 + r2)^2)]; T1 = Log[(a/r1) + Sqrt[(a^2/r1^2) + 1]]; T2 = Log[(a/r2) + Sqrt[(a^2/r2^2) + 1]]; A = \!( *SubsuperscriptBox[([Sum]), (k = 1), (2)](((1/ k))((((Sinh[k((T1 - T2))]))/((Sinh[ k*((T1 + T2))])))))); B = \!( *SubsuperscriptBox[([Sum]), (k = 1), (2)](((2/ k))((((Exp[(-k)T2Sinh[kT1]]))/((Sinh[ k*((T1 + T2))])))))); (= r1Sin[ Psi1](T1 + 2Log[1 - Exp[-2T1]]) - (r1Sin[Psi1] + r2Sin[Psi2]) Log[E1qa] + (r1Sin[Psi1] - r2Sin[Psi2])*(A + B) - h ; (Plot[k,{h,-1/(510^6),30001/(510^6)}] ])) r2 = Sqrt[(l0 + h) (2 R2 - l0 - h)]; Print["{r1,r2,h1,h, Energy}=", {N[r1], Re[r2], N[h1], Re[Abs[h]], Re[(-((Pi* T) ((2 h1 R1 Cos[alpha1] - r1 h1 Sin[Psi1] + r1^2) + (2 Abs[h] R2 Cos[alpha2] - r2 Abs[h] Sin[Psi2] + r2^2)) - ((rhop - rhol) g) ( (
Pi/2)*(-(h1^2)/2) (2 l0 (2 R1 - l0) - h1^2) + ((4/ 3 ) (R1 - l0) (-h1^2) + r1^2 h1^2) + (Pi/2)* (-h^2/2) (2 l0 (2 R2 - l0) - h^2) + ((4/ 3 ) (R2 - l0) (-h^2) + r2^2 h^2)))/(10^(-14)))]} /. FindRoot[r2*Sin[ Psi2](T2 + 2Log[1 - Exp[-2T2]]) - (r1Sin[Psi1] + r2Sin[Psi2])

   Log[E1*q*a] + ((r1*Sin[Psi1] -  r2*Sin[Psi2])*(A -  B) ) - h == 
 0, {h, 300*10^(-9)}]]](* this find root is to solve for the \

wetting height of the second sphere *)

Results:

{r1,r2,h1,h, Energy}={8.9442710^-7,8.5546510^-7,4.10^-7,7.7960510^-7,-4.68544}

{r1,r2,h1,h, Energy}={8.9548910^-7,3.0434210^-7,4.110^-7,5.3391610^-7,-2.78158}

{r1,r2,h1,h, Energy}={8.9643710^-7,3.0482410^-7,4.210^-7,5.3524910^-7,-2.79936}

{r1,r2,h1,h, Energy}={8.9727410^-7,3.0528610^-7,4.310^-7,5.3655110^-7,-2.81688}

{r1,r2,h1,h, Energy}={8.9799810^-7,3.0572710^-7,4.410^-7,5.3782410^-7,-2.83415}

{r1,r2,h1,h, Energy}={8.986110^-7,3.0614710^-7,4.510^-7,5.3906610^-7,-2.85117}

{r1,r2,h1,h, Energy}={8.9911110^-7,3.0654710^-7,4.610^-7,5.4027710^-7,-2.86795}

{r1,r2,h1,h, Energy}={8.99510^-7,3.0692610^-7,4.710^-7,5.4145910^-7,-2.8845}

{r1,r2,h1,h, Energy}={8.9977810^-7,3.0728410^-7,4.810^-7,5.4261110^-7,-2.90083}

{r1,r2,h1,h, Energy}={8.9994410^-7,3.0762110^-7,4.910^-7,5.4373210^-7,-2.91694}

{r1,r2,h1,h, Energy}={9.10^-7,3.0793710^-7,5.10^-7,5.4482410^-7,-2.93284}

{r1,r2,h1,h, Energy}={8.9994410^-7,3.0823110^-7,5.110^-7,5.4588510^-7,-2.94854}

{r1,r2,h1,h, Energy}={8.9977810^-7,3.0790810^-7,5.210^-7,5.4480810^-7,-2.96547}

{r1,r2,h1,h, Energy}={8.99510^-7,3.0756310^-7,5.310^-7,5.43710^-7,-2.98245}

{r1,r2,h1,h, Energy}={8.9911110^-7,3.0719810^-7,5.410^-7,5.4256310^-7,-2.99951}

{r1,r2,h1,h, Energy}={8.986110^-7,3.0681110^-7,5.510^-7,5.4139610^-7,-3.01664}

{r1,r2,h1,h, Energy}={8.9799810^-7,3.0640410^-7,5.610^-7,5.4019810^-7,-3.03385}

{r1,r2,h1,h, Energy}={8.9727410^-7,3.0597710^-7,5.710^-7,5.3897110^-7,-3.05116}

{r1,r2,h1,h, Energy}={8.9643710^-7,3.0552810^-7,5.810^-7,5.3771410^-7,-3.06856}

{r1,r2,h1,h, Energy}={8.9548910^-7,3.050610^-7,5.910^-7,5.3642610^-7,-3.08606}

{r1,r2,h1,h, Energy}={8.9442710^-7,3.0457110^-7,6.10^-7,5.3510910^-7,-3.10366}

{r1,r2,h1,h, Energy}={8.9325210^-7,3.0406210^-7,6.110^-7,5.337610^-7,-3.12138}

{r1,r2,h1,h, Energy}={8.9196410^-7,3.0353210^-7,6.210^-7,5.3238210^-7,-3.13922}

{r1,r2,h1,h, Energy}={8.9056210^-7,3.0298310^-7,6.310^-7,5.3097310^-7,-3.15719}

{r1,r2,h1,h, Energy}={8.8904410^-7,3.0241410^-7,6.410^-7,5.2953310^-7,-3.17528}

{r1,r2,h1,h, Energy}={8.8741210^-7,3.0182510^-7,6.510^-7,5.2806210^-7,-3.1935}

{r1,r2,h1,h, Energy}={8.8566410^-7,3.0121610^-7,6.610^-7,5.265610^-7,-3.21187}

{r1,r2,h1,h, Energy}={8.8379910^-7,3.0058710^-7,6.710^-7,5.2502710^-7,-3.23038}

I expect r1=r2 , h1=h ...

h is height so it is an real and absolute value , which we find using findrooot ..

This h is used to calculate r2 , but When I tried to replace h in r2 with Re[Abs[h]], the output becomes 0.

Another problem is different h ,when h should be equal to h1 is making me guess there is something going on with the summations of A and B or in findroot that is causing the error!

You had been a great help , I really mean it .. any suggestion is appreciated!

-SD

Attachments:
POSTED BY: S D
Posted 11 years ago

I'm nearly lost now and just guessing what you are doing.

Every notebook you attach has more invisible ToolTip hidden inside than the last one and this time there were over a dozen. I don't know what you are doing to get that, you almost certainly don't know what you are doing to get that. Usually the first thing I do when I get any notebook from you is to use Notepad to carefully delete all those and the stuff that goes with them and then see if the notebook works or not. Often that fixes at least half your problems.

It looks like you have changed some of your variable names to alpha1 and alpha2 but left some as \[Alpha]1 and \[Alpha]2 which may or may not show up as Greek characters after the select-and-copy-and-pasteToForum-and... Since the warning message in orange that starts with FindRoot::nlnum has some alpha's showing in that, I suspect this is one place to start.

It is usually a good idea to put variable "declarations" that don't depend on other variables before declarations that then depend on those. Mathematica is hiding a vast amount of stuff behind the curtain and one of those things is (mostly) invisibly hiding that it keeps the last value assigned to any variable.

With your newly introduced For loop in this version I'm having a very hard time untangling whether the order you have assigned values to variables might be part of your problem.

Is there any way your problem might be made lots simpler? Or is this as simple as it can get?

EDIT

I have taken one more whack at your code, or at least the first section of it. I have manually edited out the ToolTip using Notepad. I have guessed: you wanted your Greek alpha1 and alpha2 to be english alpha1 and alpha2, you wanted your r subscript 1 to be r1. you wanted your k=longexpression==0; to be k=longexpression; I have reordered your lines so that if x=expression depends on y= expression then y=expression is before x=expression, that includes your h1 For iterator.. I think I've done something else, but I can't spot what it might be now.

Here is the result

In[1]:= rhop = 1.781 10^3;
rhol = 0.781 10^3;
g = 9.80665;
T = 0.4/10^2;
q = Sqrt[((rhop - rhol) g)/T];
bb = 0.0000001;
alpha1 = Pi/6;
R1 = 900/10^9;
R2 = 600/10^9;
l0 = 400/10^9;
alpha2 = 0;
E1 = 1.781072418 ;
For[h1 = 400/10^9, h1 < 1.2 R1 - l0, h1 = h1 + 10/10^9,
 r1 = Sqrt[(l0 + h1) (2 R1 - l0 - h1)];
 Psi1 = ArcSin[r1/R1] - alpha1;
 r2 = Sqrt[(l0 + h) (2 R2 - l0 - h)];
 Psi2 = ArcSin[r2/R2] - alpha2;
 a = Sqrt[bb*(bb + 2*r1)*(bb + 2*r2)*(bb + 2*r1 + 2*r2)/(4*(bb + r1 + r2)^2)];
 T1 = Log[(a/r1) + Sqrt[(a^2/r1^2) + 1]];
 T2 = Log[(a/r2) + Sqrt[(a^2/r2^2) + 1]];
 k = r1*Sin[Psi1]*(T1+2*Log[1-Exp[-2*T1]])-(r1*Sin[Psi1]+r2*Sin[Psi2])*Log[E1*q*a]+(r1*Sin[Psi1]-r2*Sin[Psi2])*(A+B)-h;
 A = \!\(\*SubsuperscriptBox[\(\[Sum]\),\(k=1\),\(2\)]\(\((1/k)\)*\((\((Sinh[k*\((T1-T2)\)])\)/\((Sinh[k*\((T1+T2)\)])\))\)\)\);
 B = \!\(\*SubsuperscriptBox[\(\[Sum]\),\(k=1\),\(2\)]\(\((2/k)\)*\((\((Exp[\(-k\)*T2*Sinh[k*T1]])\)/\((Sinh[k*\((T1+T2)\)])\))\)\)\);
 Print[{((-(Pi*T) ((2 h1 R1 Cos[alpha1] - r1 h1 Sin[Psi1] + r1^2) + (2 h*R2 Cos[alpha2] - r2 *h *Sin[Psi2] + 
       r2^2)) - ((rhop - rhol) g) (1/2 *Pi*(-h1^2) (2 l0 (2 R1 - l0) - h1^2) + (4/3 (R1 - l0) (-h1^2) + r1^2 h1^2) + 
       1/2*Pi* (-h^2) (2 l0 (2 R2 - l0) - h^2) + (4/3 (R2 - l0) (-h^2) + r2^2 h^2)))/(10^(-14)))} /. 
   FindRoot[r1*Sin[Psi1]*(T1 + 2*Log[1 - Exp[-2*T1]]) - (r1*Sin[Psi1] + r2*Sin[Psi2])*
        Log[E1*q*a] + (r1*Sin[Psi1] -  r2*Sin[Psi2])*(A +  B) - h1 == 0, {h, 600/10^9}]
  ]
 ]

FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances. >>

{-2.54714-3.82639*10^-9 I}

FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances. >>

{-2.55259-3.87041*10^-9 I}

FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances. >>

General::stop: Further output of FindRoot::lstol will be suppressed during this calculation. >>

{-2.55755-3.91336*10^-9 I}
{-2.56204-3.95522*10^-9 I}
{-2.56606-3.99598*10^-9 I}
{-2.56962-4.03562*10^-9 I}
{-2.57272-4.07412*10^-9 I}
{-2.57538-4.11148*10^-9 I}
{-2.57761-4.14767*10^-9 I}
{-2.5794-4.18269*10^-9 I}
{-2.58078-4.21651*10^-9 I}
{-2.58471-4.15521*10^-9 I}
{-2.58849-4.09302*10^-9 I}
{-2.59213-4.02997*10^-9 I}
{-2.59562-3.96607*10^-9 I}
{-2.59899-3.90136*10^-9 I}
{-2.60223-3.83587*10^-9 I}
{-2.60535-3.7696*10^-9 I}
{-2.60834-3.7026*10^-9 I}
{-2.61123-7.26977*10^-9 I}
{-2.614-7.13296*10^-9 I}
{-2.61667-6.99483*10^-9 I}
{-2.61923-6.85542*10^-9 I}
{-2.6217-6.7148*10^-9 I}
{-2.62408-6.57301*10^-9 I}
{-2.62637-6.43012*10^-9 I}
{-2.62857-6.28617*10^-9 I}
{-2.63069-6.14123*10^-9 I}

As a check, if I scrape-n-paste everything from the rhop= down to the closing ] then it appears to run. Please see if you can reproduce exactly this behavior on your end with a fresh start of Mathematica in an empty notebook.

The warnings appear to happen every loop, but it stops complaining after three times. It looks like the closer FindRoot gets to a solution the slower the curve approaches zero so it gives up before it gets an answer accurate enough to make it happy. That kind of error/warning message doesn't make me tend to think this is a terrible programming error. It appears that every solution has a tiny but nonzero complex part. That may really be in your equation or it might just be roundoff errors. If you don't care about the imaginary part you could wrap Re[] around the whole thing you are giving to Print and see if the imaginary part goes away. If you do that then don't forget, just in case the complex part is much bigger sometime.

Look carefully at how I've ordered all your variable assignments and to guess what I'm thinking when I did that. Sometimes you can change the order and it can still work, but sometimes this can bite you when you least expect it.

Is there any way you can check to see whether these answers are correct?

Can you then try doing the same thing to your next part of your original notebook and see if you can get it to work?

POSTED BY: Bill Simpson
Posted 11 years ago

Bill ,

Thanks a lot for your help , i am a beginner , so i am getting stuck a lot ... If you dont mind I am having this problem , but i am not sure how to fix it ..attached

Attachments:
POSTED BY: S D
Posted 11 years ago

I removed several ToolTip which I think were causing evaluation problems and I assigned a value to the variable "a."

There is still at least one underflow problem in the code which I have not tracked down and eliminated.

FindRoot is going to return a Rule for the result and from your code I am assuming you may not understand how to use this.

This How To Use Rule Solutions may help you understand those.

Attachments:
POSTED BY: Bill Simpson
Posted 11 years ago

I am having trouble with findroot too , if anyone can help me out !, see attached

Attachments:
POSTED BY: S D
Posted 11 years ago

Hey all, I am stuck with using a findroot in a for loop , i have attached a simple example , please help ! SD

Attachments:
POSTED BY: S D
Posted 11 years ago

Try this

a = 5;
b = 6;
For[c = 1, c < 10, c = c + 1, 
 Print[Plot[a*x^2 + b*x + c, {x, -1, -.2}, AxesOrigin -> {0, 0}]];
 Print[Expand[Reduce[a*x^2 + b*x + c == 0, x]]];
 Print["{x,c,x+c}=", {x, c, x + c} /. FindRoot[a*x^2 + b*x + c == 0, {x, 0}]]
]
POSTED BY: Bill Simpson
Posted 11 years ago
h22 solve.nb has TagBoxes and ToolTipBoxes and Subscripts buried in there.

Try this and see if I haven't damaged too much.
Attachments:
POSTED BY: Bill Simpson
Posted 11 years ago
Please Ignore the subject line, I am having trouble creating a new discusiion. Anyways, I have attached a file , I am having trouble finding a solution using NSolveĀ  Please help me find a way to find a solution .. thanks
Attachments:
POSTED BY: S D
Posted 11 years ago
I'm not sure what is causing it, but you are getting things like this
, InterpretationBox[Cell["", "KeyEvent"],
TextCell["", "KeyEvent"]]
in your notebooks. That is what is keeping Mathematica from seeing each of your cells as a simple numeric expression to turn into a single number.

I opened up your notebook using Notepad, looked for those, carefully deleted them, saved the notebook as Simplify2.nb (with type "all files" so that Notepad would not append a .txt onto the end of the file name. When editing that I remove the comma immediately before InterpretationBox and InterpretationBox and then CAREFULLY count opening and closing brackets to make certain I have all the arguments that are being given to it.

The edited simplify2.nb is attached.

I assume there is some combination of keystrokes and mouse clicks that is putting these in there when you are expecting something else. Perhaps someone better than I can explain can do this. But once they are in there they are completely invisible to the average user unless you know what to look for using Notepad or something similar.
Attachments:
POSTED BY: Bill Simpson
Posted 11 years ago
Also IĀ  cant create a new discussion , the group tab is not active ; anything i should know?
POSTED BY: S D
Posted 11 years ago
Hi ,
I cant get a whole number , despite using N[] or Simplify[], see attached
Attachments:
POSTED BY: S D
Posted 11 years ago
There are two invisible items in the notebook.
,InterpretationBox[Cell["", "KeyEvent"], TextCell["", "KeyEvent"]]
I CAREFULLY manually removed those with a text editor.

The infinite sums don't appear to have simple solutions. Numerical approximations of finite sums have underflow problems but might help you.
Attachments:
POSTED BY: Bill Simpson
Posted 11 years ago
Hey , I am having trouble with the sum. Attached is the file.
Thanks !

SD
Attachments:
POSTED BY: S D
Posted 11 years ago
In your notebook, the "ArcSin" commands are actually "Button" functions which refers to the hyperlink "paclet:ref/ArcSin". This means if you click the button it will open the "ArcSin" help file. If you remove these button forms and write cleanly "ArcSin", you should get your output.
In[1]:= (*An example of my output*)-0.13634512116579703` \
(1.5999999999999997`*^-13 +
    9.027136667704869`*^-12 Sin[ArcSin[0.5`]] -
    2.256784166926217`*^-12 Sin[ArcSin[0.5`]]^2) -
40636.410967554446` (-3.2150029430991243`*^-11 Sin[ArcSin[0.5`]]^2 -
    2.3945580341426048`*^-17 Sin[ArcSin[0.5`]]^3)
Out[1]= 3.26615*10^-7
POSTED BY: Girish Arabale
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