Solve system of equations with NSolve?

I need help with NSolve.. below I explain the maths of what I am doing so the question becomes clear..

I have two points, P1 and P2, defined in Euler angles relative to the origin of the spherical coordinates frame:

P1[alpha_,beta_,gamma_]:={-0.5207928328826381` Cos[alpha] Sin[beta] - 
   0.7566899220728581` (Cos[beta] Cos[alpha] Cos[gamma] - Sin[alpha] Sin[gamma]), -0.5207928328826381` Sin[beta] Sin[alpha] - 0.7566899220728581` (Cos[beta] Cos[gamma] Sin[alpha] +Cos[alpha] Sin[gamma]), -0.5207928328826381`Cos[beta] + 0.7566899220728581` Cos[gamma] Sin[beta]};

P2[alpha_,beta_,gamma_]:={-0.5207928328826381` Cos[alpha] Sin[beta] + 
       0.7566899220728581` (Cos[beta] Cos[alpha] Cos[gamma] - Sin[alpha] Sin[gamma]), -0.5207928328826381` Sin[beta] Sin[alpha] + 0.7566899220728581` (Cos[beta] Cos[gamma] Sin[alpha] +Cos[alpha] Sin[gamma]), -0.5207928328826381`Cos[beta] - 0.7566899220728581` Cos[gamma] Sin[beta]};

I want to shift these two points to a general position, so I define the spherical coordinates of a centre as

R[r_,theta_,phi_]:=r {Sin[theta] Cos[phi], Sin[theta] Sin[phi], Cos[theta]};

However I change the Euler angles, I want to keep the radial distance of the R fixed at 0.0949009, in other words, I always have

  R[0.0949009,theta,phi]

and at the same time I want the radial distances of the two points, P1 and P2 equal 0.753964 at all values of Euler angles, so I solve for theta and phi values that satisfy this condition

functoSolve[alpha_, beta_, gamma_] := Module[{a1, a2}, a1 = Chop[NSolve[{(P1[alpha, beta, gamma]+ R[0.0949009,theta,phi]).(P1[alpha, beta, gamma]+ R[0.0949009,theta,phi]) ==0.753964, (P2[alpha, beta, gamma]+ R[0.0949009,theta,phi]).(P2[alpha, beta, gamma]+ R[0.0949009,theta,phi]) ==0.753964}, {theta,phi}], 10^-7]; a2 = Union[a1, SameTest -> (Abs[ (theta /. #1) - (theta /. #2)] < 10^-6 && Abs[(phi. #1) - (phi /. #2)] < 10^-6 &)]; Chop[P[0.0949009,theta,phi] /. a2[[1]], 10^-7]]

where a2 picks the first solution since NSolve returns all possible solution. So the functoSolve is the function I need to use many times to find theta and gamma for any {alpha,beta,gamma} combination. It works for when I put {alpha, beta, gamma}={0,0,0}. However, for any other combination, such as {10 Degree, 4 Degree, 0},NSolve fails to find the solution and returns the error messages

NSolve::ifun: Inverse functions are being used by NSolve, so some solutions may not be found; use Reduce for complete solution information. >>

NSolve::svars: Equations may not give solutions for all "solve" variables. >>

I cannot understand why Mathematica complains (!!) the maths is OK and the problem is simple enough. I do not need a high precision solution but I need a solution.

I really need help fix this problem.

Thanks,

eft

It’s better to use CodeSample (the first icon in the row) as copying as text can cause problems. For example, there seems to be a missing ] at near the end of your code. In any case, you’ve got one equation and two variables, which probably explains why NSolve doesn’t do what you expect.

Yes, indeed some parenthesis are missing. If corrected/added (hopefully as intended in the OP it appears to work.

NSolve[(-0.38148299350912984 + 
      0.09490089588967025 Cos[x])^2 + (-0.8335932784285498 + 
      0.09490089588967025 Cos[y] Sin[x])^2 + (-0.05829052041804752 + 
      0.09490089588967025 Sin[x] Sin[y])^2 == 0.753964, {x, y}]

The solution is somewhat long and does not seem to contribute to this. Actually, I suppose that the thread will be closed because the question is only caused by a simple typo.

Anyway, here’s a representation of the solutions:

ContourPlot[(-0.38148299350912984 + 
      0.09490089588967025 Cos[x])^2 + (-0.8335932784285498 + 
      0.09490089588967025 Cos[y] Sin[x])^2 + (-0.05829052041804752 + 
      0.09490089588967025 Sin[x] Sin[y])^2 == 0.753964, {x, -10, 10}, {y, -15, 15}, PlotPoints -> 50]

Cheers,

Marco

PS: Note that Mathematica suggests to use Reduce instead of NSolve. You should consider to follow that advice.

well, I have to apologise for the ill expressed question. I chopped half the equation while editing.. I have been trying to add the full equations using CodeSample with no successful so far..Would you kindly explain how I should use it?

eft

Hi,

this here is a good starting point. The moderation team explain how to post and format everything correctly:

Cheers,

Marco

I have edited the question and added details of the problem. I really hope to get help come over it.

eft

The documentation on DSolve says that “NSolve deals primarily with linear and polynomial equations.” You may try FindRoot if your problem is mainly numerical.