0
$\begingroup$

In the following code, I have tried to plot basically a distribution of points that I have assigned to be called Acoords and Bcoords. What I want is for the number of points to be a function of time. That is the sol1 and sol2 here (solutions to the differential equation, A[t] and B[t]). If I specify a particular t, the plot is exactly how I want it. However, when I try to add it to the manipulate function to plot the time dependence, it remain stagnant. I have tried to use the Evaluate function, but to no avail. My question is how I can implement time dependence properly so that the diagram evolves as I change the time using the slide bar? Any help would be greatly appreciated, and the code it pasted below!

A0 = 8; (*number of soldiers in force one*)

B0 = 6; (*number of soldiers in force two*)
\[Alpha] = 
  10.1; (*firepower of force force one*)
\[Beta] = 
  18; (*firepower of force force two*)

xbound = 20;(*size of battlefield*)
ybound = 20;(*size of battlefield*)

sol = DSolve[{A'[t] == -\[Beta]*B[t], B'[t] == -\[Alpha]*A [t], 
    A[0] == A0, B[0] == B0}, {A[t], B[t]}, t];

sol1 = A[t] /. sol[[1, 1]];
sol2 = B[t] /. sol[[1, 2]];

xcoordA = {};
ycoordA = {};
xcoordB = {};
ycoordB = {};

t = 0.005;

numbersoldiersA = Round[sol1];
numbersoldiersB = Round[sol2];
For[i = 0, i < numbersoldiersA, i++, 
  xcoordA = Append[xcoordA, RandomInteger[xbound]]];
For[j = 0, j < numbersoldiersA, j++, 
  ycoordA = Append[ycoordA, RandomInteger[xbound]]];
For[k = 0, k < numbersoldiersB, k++, 
  xcoordB = Append[xcoordB, RandomInteger[xbound]]];
For[l = 0, l < numbersoldiersB, l++, 
  ycoordB = Append[ycoordB, RandomInteger[xbound]]];

Acoords = Table[{xcoordA[[i]], ycoordA[[i]]}, {i, numbersoldiersA}];
Bcoords = Table[{xcoordB[[i]], ycoordB[[i]]}, {i, numbersoldiersB}];

listplot = 
  ListPlot[{Acoords, Bcoords}, PlotStyle -> PointSize[Large], 
   PlotRange -> 21, 
   LabelStyle -> {FontSize -> 12, FontFamily -> "Times", , Black, 
     Bold}, FrameLabel -> {\.00 \.00 "INSERT", "INSERT"}, 
   PlotLabel -> "INSERT", PlotStyle -> {Pink, Blue}, Frame -> True, 
   PlotLegends -> {"INSERT", "INSERT"}];

Show[listplot]

Manipulate[
 ListPlot[{Evaluate[Acoords], Evaluate[Bcoords]}, 
  PlotStyle -> PointSize[Large], PlotRange -> 21, 
  LabelStyle -> {FontSize -> 12, FontFamily -> "Times", , Black, 
    Bold}, FrameLabel -> {\.00 \.00 "INSERT", "INSERT"}, 
  PlotLabel -> "INSERT", PlotStyle -> {Pink, Blue}, Frame -> True, 
  PlotLegends -> {"INSERT", "INSERT"}], {t, 0, 1, 
  Appearance -> "Labeled"}]

These are currently the plots that I see:

enter image description here

$\endgroup$
16
  • $\begingroup$ The code you show does not run. Everything above Manipulate gives errors. I did not run your manipulate code. Only the code above it. !Mathematica graphics Please make sure the code has no errors before using it in Manipulate $\endgroup$
    – Nasser
    Commented Feb 17, 2023 at 12:35
  • $\begingroup$ you have to plug in a t value, and then it doesnt give an error i believe. but this is exactly what i dont want to have to do. if you put t=0.005 in the parameter section, everything runs fine for me @Nasser are you saying it doesnt for you ? $\endgroup$ Commented Feb 17, 2023 at 12:48
  • $\begingroup$ you have to plug in a t value but I am just running the code you gave. $\endgroup$
    – Nasser
    Commented Feb 17, 2023 at 13:05
  • $\begingroup$ Look at Acoords, Bcoords, they do not have a form that can be used in a iterator statement $\endgroup$ Commented Feb 17, 2023 at 13:07
  • $\begingroup$ @DanielHuber could you elaborate on what you mean by this? $\endgroup$ Commented Feb 17, 2023 at 13:20

1 Answer 1

1
$\begingroup$

First you need to nest the calculations inside the Manipulate in order to update the results when you change time. And second, you should not use the same symbol t as a variable in DSolve and as a numerical value. In the following I use tt instead of t inside Manipulate and then a replacement rule to give numerical values to sol1 and sol2. The code below works, by which I mean that the ListPlot changes when you slide the bar. Whether it does what you want it to do is another issue.

ClearAll["Global`*"]
A0 = 8; (*number of soldiers in force one*)

B0 = 6; (*number of soldiers in force two*)
\[Alpha] = 10.1; (*firepower of force force one*)
\[Beta] = 18; (*firepower of force force two*)

xbound = 20;(*size of battlefield*)
ybound = 20;(*size of battlefield*)

sol = DSolve[{A'[t] == -\[Beta]*B[t], B'[t] == -\[Alpha]*A[t], 
    A[0] == A0, B[0] == B0}, {A[t], B[t]}, t];

sol1 = A[t] /. sol[[1, 1]];
sol2 = B[t] /. sol[[1, 2]];

xcoordA = {};
ycoordA = {};
xcoordB = {};
ycoordB = {};

Manipulate[
 numbersoldiersA = Round[sol1 /. t -> tt];
 numbersoldiersB = Round[sol2 /. t -> tt];
 For[i = 0, i < numbersoldiersA, i++, 
  xcoordA = Append[xcoordA, RandomInteger[xbound]]];
 For[j = 0, j < numbersoldiersA, j++, 
  ycoordA = Append[ycoordA, RandomInteger[xbound]]];
 For[k = 0, k < numbersoldiersB, k++, 
  xcoordB = Append[xcoordB, RandomInteger[xbound]]];
 For[l = 0, l < numbersoldiersB, l++, 
  ycoordB = Append[ycoordB, RandomInteger[xbound]]];
 
 Acoords = Table[{xcoordA[[i]], ycoordA[[i]]}, {i, numbersoldiersA}];
 Bcoords = Table[{xcoordB[[i]], ycoordB[[i]]}, {i, numbersoldiersB}];
 
 listplot = 
  ListPlot[{Acoords, Bcoords}, PlotStyle -> PointSize[Large], 
   PlotRange -> 21, 
   LabelStyle -> {FontSize -> 12, FontFamily -> "Times", , Black, 
     Bold}, FrameLabel -> {\.00 \.00 "INSERT", "INSERT"}, 
   PlotLabel -> "INSERT", PlotStyle -> {Pink, Blue}, Frame -> True, 
   PlotLegends -> {"INSERT", "INSERT"}]
 ,
 {tt, 0, 1, Appearance -> "Labeled"}]
$\endgroup$
2
  • $\begingroup$ thank you very much, this does work perfectly. however, i am still confused about the replacement variable t->tt. I dont fully understand why it can't interpret t as the dynamic variable as well? could you elaborate on that? $\endgroup$ Commented Feb 18, 2023 at 5:26
  • $\begingroup$ In DSolve t is a dummy variable that tells DSolve which is the independent variable in the differential equation. The first time you run the code everything is fine but once you give t a value your definition of sol1 and sol2 breaks down. If you set t=0.1, for example, sol becomes DSolve[{Derivative[1][A][0.1] == -18 B[0.1], Derivative[1][B][0.1] == -10.1 A[0.1], A[0] == 8, B[0] == 6}, {A[0.1], B[0.1]}, 0.1] and you get the error message '0.1 cannot be used as a variable'. $\endgroup$
    – Themis
    Commented Feb 18, 2023 at 11:42

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.