1
$\begingroup$

Bug introduced in 7.0, persisting through 13.2.


So i am trying to plot the magnetic vector field surrounding a "flat conducting cable" (don't know the proper english expression).

This is the "code" (there are some constants in the analytic formulas, which I have excluded for the sake of clarity):

 VectorPlot[{Piecewise[{{ArcTan[y/(x + 3)] - (Pi - ArcTan[y/(x - 3)]), 

 Abs[x] < 3}},

 ArcTan[y/(x + 3)] - ArcTan[y/(x - 3)]],

 Log[Sqrt[(y^2 + (x + 3)^2)]/Sqrt[(y^2 + (x - 3)^2)]]},

 {x, -10, 10}, {y, -10, 10}]

The error message I get is the following:

Part::partw: Part 1 of {} does not exist. >>

I am not sure whether Piecewise can be used with VectorPlot at all since I haven't managed to find a syntactic, so that is probably where the problem lies. (I have managed to plot the separate functions, basically I am experiencing problems implementing Piecewise into the code.)

Thank you very much!

$\endgroup$
1

1 Answer 1

2
$\begingroup$

A vector plot needs two components:

 VectorPlot[
   Piecewise[
  {
   {{ArcTan[y/(x + 3)] - (Pi - ArcTan[y/(x - 3)]), y}, 
    Abs[x] < 3},
   {{ArcTan[y/(x + 3)] - ArcTan[y/(x - 3)], y}, 
     Log[Sqrt[(y^2 + (x + 3)^2)]/Sqrt[(y^2 + (x - 3)^2)]]}
   }
            ],
   {x, -10, 10}, {y, -10, 10}
           ]

enter image description here

Of course, you will need to include the proper formula for the $x$ and the $y$ components of your vector in the ranges you state.

$\endgroup$
4
  • $\begingroup$ Thank you for the answer, but it has left me even more confused as I don't see how it fits with the syntax for the Piecewise or VectorPlot functions at all ... First let me ask you why did you put the y at the end of the first two formulas and nothing at the end of the last one? (actually the first two were defining x in ranges bordered by 3 and -3 and the third one was for y, but that doesn't really matter) $\endgroup$
    – HudPesjan
    Commented Apr 30, 2015 at 13:00
  • $\begingroup$ @HudPesjan A VectorPlot is (of course) a plot of vectors, which here must have two components. Your original posting had just a scalar (one component). My answer included two components (placed within {}), and I arbitrarily introduced a second component whose value was $y$. As I stated, you must write both components of your vector in your function. The conditional statement, however, can be one-dimensional, as indeed all of the above are. $\endgroup$ Commented Apr 30, 2015 at 14:56
  • $\begingroup$ Your answer makes perfect sense, but I am not sure I understand what is going on in the code. My questions would be firstly: Have you included both, the x and y components inside the Piecewise function and wether it is neccesesary to do so? And secondly: If I have understood correctly, what you have written inside the Piecewise function is: { {component (for x?), y}, condition (for x?)}, { {component (for y?), y}, condition (for y?)}, It kinda doesn't look like the suggested Piecewise syntax, so I don't see what you really did there. $\endgroup$
    – HudPesjan
    Commented May 3, 2015 at 9:39
  • $\begingroup$ @HudPesjan It is indeed necessary to include a list of two elements as your function—the $x$ and the $y$ components of your vector field. I just put in a simple $y$... you must determine the formula for the $x$ component and $y$ component of your vector. The condition which determines which vector function to use must evaluate to True or False, and thus can be a function of one variable, two variables, or whatever. $\endgroup$ Commented May 3, 2015 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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