# Piecewise function within a vectorplot

I'm trying to do a vector plot with some code that includes piecewise functions and I keep getting the following error: Part::partw: Part 1 of {} does not exist. Here is the code:

 OPT[r1_, kk_, q1_, p1_, a1_, d_] := kk/4 ((a1/(p1 q1 kk) + 1 - d/r1 ) + Sqrt[((a1/(p1 q1 kk) + 1 - d/r1 )^2) + ((8 a1 d)/( p1 q1 kk r1)) ])

Sanctionvec[r1_, kk_, q1_, g_, p1_, a1_, l0_, x0_, T_, d_, n_, B_, Pi1_] :=
VectorPlot[{g (d (p1 - a1/(q1 x2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n])/(q1 x2))
+ (r1 x2)/n (1 - x2/kk) (1/x2 (n - 1) (p1 - a1/(q1 x2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n])/(q1 x2))
- a1 /(q1 x2^2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d,n])/(q1 x2^2)) + Pi1 B H1vec[r1, kk, q1, p1, a1, d, n]
- (p1 - a1/(q1 x2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n])/(q1 x2) ) (r1 (1 - (2 x2)/kk))) l,
r1 x2 (1 - x2/kk) - q1 n l x2}, {l, 0, 1.1 Max[Evaluate[r1/(n q1)]]}, {x2, 0.001, kk},
VectorStyle -> Gray, VectorScale -> {Small, 0.1, Automatic},  VectorPoints -> 10]

Hvec[r1_, kk_, q1_, p1_, a1_, d_, n_, x2_] :=  Piecewise[{{q1 x2, (r1 OPT[r1, kk, q1, p1, a1, d])/
n (1 - OPT[r1, kk, q1, p1, a1, d]/kk) < (r1 x2)/n (1 - x2/kk)}}, 0]

H1vec[r1_, kk_, q1_, p1_, a1_, d_, n_, x2_] := Piecewise[{{r1/n (1 - x2/kk),
(r1 OPT[r1, kk, q1, p1, a1, d])/n (1 - OPT[r1, kk, q1, p1, a1,d]/kk) < (r1 x2)/n (1 - x2/kk)}}, 0]


(also d is a delta, B beta and Pi1 pi - changed them to make it look a bit neater). If it helps at all, the code is for an adapted version of the modified Golden rule arising from the Gordon-Schaefer harvest function.

Thank you!

Edit: Have now tried adding x2 as an argument to Hvec and H1vec but unfortunately still getting the same error. code is now:

Sanctionvec[r1_, kk_, q1_, g_, p1_, a1_, l0_, x0_, T_, d_, n_, B_, Pi1_] :=
VectorPlot[{g (d (p1 - a1/(q1 x2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n,x2])/(q1 x2))
+ (r1 x2)/n (1 - x2/kk) (1/x2 (n - 1) (p1 - a1/(q1 x2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n,x2])/(q1 x2))
- a1 /(q1 x2^2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d,n,x2])/(q1 x2^2)) + Pi1 B H1vec[r1, kk, q1, p1, a1, d, n,x2]
- (p1 - a1/(q1 x2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n,x2])/(q1 x2) ) (r1 (1 - (2 x2)/kk))) l,
r1 x2 (1 - x2/kk) - q1 n l x2}, {l, 0, 1.1 Max[Evaluate[r1/(n q1)]]}, {x2, 0.001, kk},
VectorStyle -> Gray, VectorScale -> {Small, 0.1, Automatic},  VectorPoints -> 10]

• Could you include the erroneous code for the vector plot? Commented Apr 5, 2021 at 14:37
• @DanielHuber sorry, do you mean the values I called the function with? Thats Sanctionvec[2.4,54231,0.0000545,0.05,23.37,19.5,100,54231,15,0.03,10,1]. It just produces a blank graph highlighted red with the part1 of {} does not exist error Commented Apr 5, 2021 at 15:08
• You declare a function of 13 variables and call the function with only 12 variables. Commented Apr 5, 2021 at 15:15
• Woops, sorry realised I typed it up wrong - I actually called it with Sanctionvec[2.4,54231,0.0000545,0.05,23.37,19.5,100,54231,15,0.03,100,10,1] - I have all the numbers saved as named variables so I don't have to remember them Commented Apr 5, 2021 at 15:20
• The function OPT is not declared. Commented Apr 5, 2021 at 15:34

OPT[r1_, kk_, q1_, p1_, a1_, d_] :=
kk/4 ((a1/(p1 q1 kk) + 1 - d/r1) +
Sqrt[((a1/(p1 q1 kk) + 1 - d/r1)^2) + ((8 a1 d)/(p1 q1 kk r1))])
Hvec[r1_, kk_, q1_, p1_, a1_, d_, n_, x2_] :=
Piecewise[{{q1 x2, (r1 OPT[r1, kk, q1, p1, a1, d])/
n (1 - OPT[r1, kk, q1, p1, a1, d]/kk) < (r1 x2)/
n (1 - x2/kk)}}, 0]

H1vec[r1_, kk_, q1_, p1_, a1_, d_, n_, x2_] :=
Piecewise[{{r1/
n (1 - x2/kk), (r1 OPT[r1, kk, q1, p1, a1, d])/
n (1 - OPT[r1, kk, q1, p1, a1, d]/kk) < (r1 x2)/
n (1 - x2/kk)}}, 0]

Sanctionvec[r1_, kk_, q1_, g_, p1_, a1_, l0_, x0_, T_, d_, n_, B_,
Pi1_] := VectorPlot[
t = {g (d (p1 -
a1/(q1 x2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n,
x2])/(q1 x2)) + (r1 x2)/
n (1 - x2/
kk) (1/x2 (n - 1) (p1 -
a1/(q1 x2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n,
x2])/(q1 x2)) -
a1/(q1 x2^2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n,
x2])/(q1 x2^2)) +
Pi1 B H1vec[r1, kk, q1, p1, a1, d, n,
x2] - (p1 -
a1/(q1 x2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n,
x2])/(q1 x2)) (r1 (1 - (2 x2)/kk))) l,
r1 x2 (1 - x2/kk) - q1 n l x2}, {l, 0,
1.1 Max[Evaluate[r1/(n q1)]]}, {x2, 0.001, kk}]

Sanctionvec[2.4, 54231, 0.0000545, 0.05, 23.37, 19.5, 100, 54231, 15, \
0.03, 100, 10, 1]


• Thank you - it's no longer throwing up the error code! I've run into another problem as the VectorPlot produced is not at all what it's supposed to look like - I've added a reply as I can't seem to add an image in a comment Commented Apr 6, 2021 at 12:24

Thank you! It's now no longer throwing up errors, but the vector plot that it produces is very strange:

OPT[r1_, kk_, q1_, p1_, a1_, d_] := kk/4 ((a1/(p1 q1 kk) + 1 - d/r1) +
Sqrt[((a1/(p1 q1 kk) + 1 - d/r1)^2) + ((8 a1 d)/(p1 q1 kk r1))])
Hvec[r1_, kk_, q1_, p1_, a1_, d_, n_, x2_] := Piecewise[{{q1 x2, (r1 OPT[r1, kk, q1, p1, a1, d])/
n (1 - OPT[r1, kk, q1, p1, a1, d]/kk) < (r1 x2)/
n (1 - x2/kk)}}, 0]

H1vec[r1_, kk_, q1_, p1_, a1_, d_, n_, x2_] :=  Piecewise[{{r1/
n (1 - x2/kk), (r1 OPT[r1, kk, q1, p1, a1, d])/
n (1 - OPT[r1, kk, q1, p1, a1, d]/kk) < (r1 x2)/
n (1 - x2/kk)}}, 0]

Sanctionvec[r1_, kk_, q1_, g_, p1_, a1_, l0_, x0_, T_, d_, n_, B_,Pi1_] := VectorPlot[t = {g (d (p1 -
a1/(q1 x2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n,
x2])/(q1 x2)) + (r1 x2)/
n (1 - x2/
kk) (1/x2 (n - 1) (p1 -
a1/(q1 x2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n,
x2])/(q1 x2)) -
a1/(q1 x2^2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n,
x2])/(q1 x2^2)) +
Pi1 B H1vec[r1, kk, q1, p1, a1, d, n,
x2] - (p1 -
a1/(q1 x2) - (Pi1 B Hvec[r1, kk, q1, p1, a1, d, n,
x2])/(q1 x2)) (r1 (1 - (2 x2)/kk))) l,
r1 x2 (1 - x2/kk) - q1 n l x2}, {l, 0, 1.1 Max[Evaluate[r1/(n q1)]]}, {x2, 0.001, kk},
VectorStyle -> Gray, VectorScale -> {Small, 0.1, Automatic}, VectorPoints -> 10]

Sanctionvec[2.4, 54231, 0.0000545, 0.05, 23.37, 19.5, 100, 54231, 15, 0.03, 100, 10, 1]


I've tried changing the plot range but no matter what values I put it still produces this:

Edit: when VectorScale code is removed I get this

• It is caused by the superfluous VectorScale. B.t.w. this is superseeded in version 12.1 (see the manual) Commented Apr 6, 2021 at 12:32
• @DanielHuber if I remove the VectorScale it becomes even weirder! I also am using version 12.0, though I don't know if that's enough to make a difference Commented Apr 6, 2021 at 13:52
• I have 12.1. Is my code running at your place? Commented Apr 6, 2021 at 14:16
• It runs, it just doesn't look right! I've got some over vectorplot functions that don't have piecewise functions in them and they work fine with the VectorScale-> {Small, 0.1, Automatid}, so I'm not sure what's made it so unhappy! Commented Apr 6, 2021 at 15:11
• I think I've figured out the issue, though haven't quite worked out how to fix it. Basically the system has a singularity at low levels of l and x2 and I need to fiddle with the scaling to get that massive arrow to disappear Commented Apr 7, 2021 at 10:51