0
$\begingroup$

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]
$\endgroup$
9
  • $\begingroup$ Could you include the erroneous code for the vector plot? $\endgroup$ – Daniel Huber Apr 5 at 14:37
  • $\begingroup$ @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 $\endgroup$ – Ducky007 Apr 5 at 15:08
  • $\begingroup$ You declare a function of 13 variables and call the function with only 12 variables. $\endgroup$ – Daniel Huber Apr 5 at 15:15
  • $\begingroup$ 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 $\endgroup$ – Ducky007 Apr 5 at 15:20
  • $\begingroup$ The function OPT is not declared. $\endgroup$ – Daniel Huber Apr 5 at 15:34
2
$\begingroup$

Here is your corrected 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))])
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]

enter image description here

$\endgroup$
1
  • $\begingroup$ 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 $\endgroup$ – Ducky007 Apr 6 at 12:24
0
$\begingroup$

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:

VectorPlot diagram

Edit: when VectorScale code is removed I get this enter image description here

$\endgroup$
7
  • $\begingroup$ It is caused by the superfluous VectorScale. B.t.w. this is superseeded in version 12.1 (see the manual) $\endgroup$ – Daniel Huber Apr 6 at 12:32
  • $\begingroup$ @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 $\endgroup$ – Ducky007 Apr 6 at 13:52
  • $\begingroup$ I have 12.1. Is my code running at your place? $\endgroup$ – Daniel Huber Apr 6 at 14:16
  • $\begingroup$ 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! $\endgroup$ – Ducky007 Apr 6 at 15:11
  • $\begingroup$ 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 $\endgroup$ – Ducky007 Apr 7 at 10:51

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.