1
$\begingroup$

I am developing quite an involved bit of code (for me) that is not finding solutions with Solve when it should. The relevant part of the code is

ky = 0.6;
width = 10.1;
(*...monolith of code that generates s1, s2 and s3...*)
c1 = (s1 == s2) /. x -> 0;
c2 = D[s1 == s2, x] /. x -> 0;
c3 = (s2 == s3) /. x -> width;
c4 = D[s2 == s3, x] /. x -> width;
Solve[c1 && c2 && c3 && c4]
(*{}*)

where s1, s2 and s3 are lists with length 2 generated in the rest of the code (omitted here for simplicity/compactness), an example of which will be provided below. I expect solutions to exist for ky between -1 and 1 but the code failed to find solutions for some values such as ky=0.6.

I therefore attempted to split the Solve process up into a couple of steps and changed the last line in the code to:

{c3, c4} = {c3, c4} /. Solve[c1 && c2] // Flatten;
Solve[c3 && c4]
(*{{r1 -> -0.504095 - 0.823378 I,....and other solutions...}}*)

Hurrah it works. However this code still fails for other values such as ky=0.7 when there should be solutions. How can I improve this code so it always finds solutions when it should? For testing I provide the output from the rest of the code for ky=0.7 (sorry it's messy)

{s1, s2, s3}=
{{(-0.299481 + 0.640556 I) E^(
    I (1.1 x + 0.7 y)) - (0.299481 + 0.640556 I) E^(
    I (-1.1 x + 0.7 y)) r1 + 
   0.941558 E^(I ((0. - 1.47986 I) x + 0.7 y))
     rIm1, (0.707107 + 0. I) E^(
    I (1.1 x + 0.7 y)) + (0.707107 + 0. I) E^(I (-1.1 x + 0.7 y))
     r1 + 0.33685 E^(I ((0. - 1.47986 I) x + 0.7 y))
     rIm1}, {(0.497117 + 0.502866 I) b1 E^(I (-1.67631 x + 0.7 y)) - 
   0.904655 b4 E^(I ((0. - 1.94679 I) x + 0.7 y)) - 
   0.426145 b3 E^(
    I ((0. + 1.94679 I) x + 0.7 y)) + (0.497117 - 0.502866 I) b2 E^(
    I (1.67631 x + 0.7 y)), (0.707107 + 0. I) b1 E^(
    I (-1.67631 x + 0.7 y)) + 
   0.426145 b4 E^(I ((0. - 1.94679 I) x + 0.7 y)) + 
   0.904655 b3 E^(
    I ((0. + 1.94679 I) x + 0.7 y)) + (0.707107 + 0. I) b2 E^(
    I (1.67631 x + 0.7 y))}, {(-0.299481 + 0.640556 I) E^(
    I (1.1 x + 0.7 y)) t1 + 
   0.33685 E^(I ((0. + 1.47986 I) x + 0.7 y))
     tIm1, (0.707107 + 0. I) E^(I (1.1 x + 0.7 y)) t1 + 
   0.941558 E^(I ((0. + 1.47986 I) x + 0.7 y)) tIm1}}

I hope this is an acceptable question, I post with caution (see here).

$\endgroup$
  • 1
    $\begingroup$ For starter, try to shift the initial condition, like ky=0.70001. $\endgroup$ – Sumit Jun 27 '16 at 13:43
  • $\begingroup$ Maybe I should specify it now seems to fail for the entire range 0.7 to 1 $\endgroup$ – Tom Jun 27 '16 at 13:46
  • $\begingroup$ The above code solves perfectly fine for me, with those values of {s1,s2,s3} $\endgroup$ – Feyre Jun 27 '16 at 13:52
  • $\begingroup$ @Feyre Eugh is this another case where it's because I'm still on 9.0.0 and my supervisor is too cheap to go to 10 and my IT department not getting round to at least upgrading to 9.0.1 cries $\endgroup$ – Tom Jun 27 '16 at 13:56
  • $\begingroup$ Possibly? If it helps, I get these solutions for [c1&&c2&&c3&&c4] and [c3&&c4] respectively {{b1 -> 0.551683 - 0.197769 I, b2 -> 0.163159 - 0.453896 I, b3 -> 0.29314 - 0.549955 I, b4 -> -1.81831*10^-10 + 9.53055*10^-10 I, r1 -> -0.336636 - 0.847225 I, rIm1 -> 0.895327 - 1.06646 I, t1 -> -0.245692 + 0.329425 I, tIm1 -> -1.91242*10^6 + 1.17446*10^6 I}} {{b1 -> 0.551683 - 0.197769 I, b2 -> 0.163159 - 0.453897 I, t1 -> -0.245692 + 0.329425 I, tIm1 -> -1.91242*10^6 + 1.17446*10^6 I}} $\endgroup$ – Feyre Jun 27 '16 at 14:01
1
$\begingroup$

In M10, you will get the answer given by Feyre.

Solve[c1 && c2 && c3 && c4]

{{b1 -> 0.551683 - 0.197769 I, b2 -> 0.163159 - 0.453896 I, b3 -> 0.29314 - 0.549955 I, b4 -> -1.81831*10^-10 + 9.53055*10^-10 I, r1 -> -0.336636 - 0.847225 I, rIm1 -> 0.895327 - 1.06646 I, t1 -> -0.245692 + 0.329425 I, tIm1 -> -1.91242*10^6 + 1.17446*10^6 I}}

In M9 you have to specify which variable you want to solve for. For example

Solve[c1 && c2 && c3 && c4, {b1, b2, b3, b4, r1, rIm1, t1, tIm1}];
Simplify[%, Assumptions -> y > 0]

{{b2 -> (-0.511444 - 0.225877 I) + (1.21486 + 0.0221892 I) b1, b3 -> (0.536368 - 0.877726 I) - (0.579409 - 0.386421 I) b1, b4 -> (-9.68142*10^-10 + 5.66735*10^-10 I) + (1.04054*10^-9 + 1.07328*10^-9 I) b1, r1 -> (-0.834692 - 0.882983 I) + (0.779397 + 0.344217 I) b1, rIm1 -> (0.0198637 - 0.977868 I) + (1.4572 + 0.361793 I) b1, t1 -> (0.455882 - 0.585599 I) - (1.65376 - 1.06576 I) b1, tIm1 -> (674473. + 2.78309*10^6 I) - (3.22886*10^6 + 4.07336*10^6 I) b1}}

You are not getting independent solutions because you have 9 variables and 8 (4 complex) equations. This is mentioned in the error message as well

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

If you freeze one variable (say y=0)

Solve[Block[{y = 0}, c1 && c2 && c3 && c4], {b1, b2, b3, b4, r1, rIm1, t1, tIm1}]

{{b1 -> 0.551683 - 0.197769 I, b2 -> 0.163159 - 0.453896 I, b3 -> 0.29314 - 0.549955 I, b4 -> -1.81831*10^-10 + 9.53055*10^-10 I, r1 -> -0.336636 - 0.847225 I, rIm1 -> 0.895327 - 1.06646 I, t1 -> -0.245692 + 0.329425 I, tIm1 -> -1.91242*10^6 + 1.17446*10^6 I}}

You get the good old answer back. This is also true for M10. I think for some reason M10 is choosing y=0, but can't say that for sure.

| improve this answer | |
$\endgroup$
  • $\begingroup$ I believe the equations always have the same factor of y in the exponent so you can cancel and it reduces to 8 variables, but I guess I need to be more explicit with Mathematica $\endgroup$ – Tom Jun 27 '16 at 14:35
  • $\begingroup$ This appears to be spitting out much more reasonable answers so thank you! However in an ideal world I would prefer not to tell Solve which ones to solve for and just to obtain all solutions because the variables change depending on the rest of the code. Is there an easy way to do that? Something like Solve[{c1,c2,c3,c4},All] (which doesn't work) $\endgroup$ – Tom Jun 27 '16 at 14:45
  • $\begingroup$ In a real world you can have unique solution for maximum n variables if you have n equations. That's how it works ;) $\endgroup$ – Sumit Jun 27 '16 at 14:50
  • $\begingroup$ So your suggestions improved things, but it's still not behaving. For example using your improvements it should find a solution for ky=0.9, width=17.1 and the rest below, but I do not. Is there any further refinements that can be made? $\endgroup$ – Tom Jun 28 '16 at 11:13
  • $\begingroup$ s1={(-0.0332756 + 0.706323 I) E^( I (0.943398 x + 0.9 y)) - (0.0332756 + 0.706323 I) E^( I (-0.943398 x + 0.9 y)) r1 + 0.964095 E^(I ((0. - 1.5843 I) x + 0.9 y)) rIm1, (0.707107 + 0. I) E^( I (0.943398 x + 0.9 y)) + (0.707107 + 0. I) E^( I (-0.943398 x + 0.9 y)) r1 + 0.265559 E^(I ((0. - 1.5843 I) x + 0.9 y)) rIm1} $\endgroup$ – Tom Jun 28 '16 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.