1
$\begingroup$

I'm new to Mathematica working on a project for school. I'm trying to show the deflection of a cord over time by breaking it up into 14 points and plotting the x,y coordinates of each point over time. I apologize in advance, I'm unsure how to format this post neatly....

ks=100;
kd=10;
gy=-10
d=1;
n=14;
dx=d/n;
Subscript[l, r] = dx;
tmax = 14;
Subscript[x, 14][t_] := 1;
Subscript[y, 14][t_] := 0;
Subscript[x, 1][t_] := 0;
Subscript[y, 1][t_] := 0;

xtable =
  Table[
   {Subscript[x, i]''[t] == -kd*(Subscript[x, i]'[t] - Subscript[x, i + 1]'[t]) - 
   ks*(Subscript[x, i][t] - Subscript[x, i + 1][t])*(1 - Subscript[l, r]/((Subscript[x, i][t]
   - Subscript[x, i + 1][t])^2 + (Subscript[y, i][t] - 
   Subscript[y, i + 1][t])^2)^0.5)- kd*(Subscript[x, i]'[t] - Subscript[x, i - 1]'[t]) 
   -ks*(Subscript[x, i][t] - Subscript[x, i - 1][t])*(1 - Subscript[l, 
   r]/((Subscript[x, i][t] - Subscript[x, i - 1][t])^2 + (Subscript[y, i][t] - 
   Subscript[y, i - 1][t])^2)^0.5), Subscript[x, i][0] == Subscript[l, r] (i - 1), 
   Subscript[x, i]'[0] == 0}, {i, 2, (n - 1)}];

ytable =
  Table[
   {Subscript[y, i]''[t] == 
     gy - kd*(Subscript[y, i]'[t] - Subscript[y, i + 1]'[t]) - 
     ks*(Subscript[y, i][t] - Subscript[y, i + 1][t])*(1 - Subscript[l, 
     r]/((Subscript[x, i][t] - Subscript[x, i + 1][t])^2 + (Subscript[y, i] 
     [t] - Subscript[y, i + 1][t])^2)^0.5)
     - kd*(Subscript[y, i]'[t] - Subscript[y, i - 1]'[t]) -
     ks*(Subscript[y, i][t] - Subscript[y, i - 1][t])*(1 - 
     Subscript[l, r]/((Subscript[x, i][t] - Subscript[x, i - 1][t])^2 + 
     (Subscript[y, i][t] - Subscript[y, i - 1][t])^2)^0.5), 
     Subscript[y, i][0] == 0, Subscript[y, i]'[0] == 0}, {i, 2, (n - 1)}];

(*Combines x,y tables to form ODE to be solved*)
ODE = Join[xtable, ytable];
(*Solves ODE for position over time*)
PSolve = NDSolve[{ODE}, Table[{Subscript[x, i][t], Subscript[y, i][t]},
{i, 2, n - 1}], {t,0, tmax}];
(*Solves ODE for velocity over time*)
VSolve = NDSolve[{ODE}, 
Table[{Subscript[x, i]'[t], Subscript[y, i]'[t]}, {i, 2, n - 1}], {t, 0, tmax}]; 

(*Position for all ten time steps*)
Positions = 
  Table[Evaluate[Table[{Subscript[x, i][t], Subscript[y, i][t]}, {i, 1, n}] 
  /. PSolve], {t, 0, tmax}];

Show[ListLinePlot[Positions[[1]]], ListLinePlot[Positions[[2]]], 
 ListLinePlot[Positions[[3]]], ListLinePlot[Positions[[4]]], 
 ListLinePlot[Positions[[5]]], ListLinePlot[Positions[[6]]], 
 ListLinePlot[Positions[[7]]], ListLinePlot[Positions[[8]]], 
 ListLinePlot[Positions[[9]]], ListLinePlot[Positions[[10]]], 
 ListLinePlot[Positions[[11]]], ListLinePlot[Positions[[12]]], 
 ListLinePlot[Positions[[13]]], ListLinePlot[Positions[[14]]], 
 ListLinePlot[Positions[[tmax]]], ImageSize -> Large, AxesLabel -> {Xpos, Ypos}]

I get the following errors:

Show::gcomb: Could not combine the graphics objects in Show...

RGBColor called with 1 argument; 3 or 4 arguments are expected.

Coordinate Skeleton[14] should be a pair of numbers, or a Scaled or Offset form.

I would like to get a line for every second combined on one chart. Could anyone please help fix and/or clean up my code? Much appreciated.

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Chris K Nov 14 '18 at 3:05
1
$\begingroup$

A few things:

  1. You don't need to solve for position and velocity separately, one NDSolve is enough.

  2. You should omit the [t] in the list of unknowns in NDSolve.

  3. I'd also Flatten the list of unknowns.

  4. You can use Plot on the results from NDSolve. There's no need for a Table.

Together:

PSolve = NDSolve[ODE, Flatten@Table[{Subscript[x, i], Subscript[y, i]}, {i, 2, n - 1}], {t, 0, tmax}][[1]]

Plot[Evaluate[Table[{Subscript[x, i][t], Subscript[y, i][t]}, {i, 2, n - 1}] /. PSolve], {t, 0, tmax}]
Plot[Evaluate[Table[{Subscript[x, i]'[t], Subscript[y, i]'[t]}, {i, 2, n - 1}] /. PSolve], {t, 0, tmax}]

Mathematica graphics Mathematica graphics

If you want to see all the points in the cord at a given time, you could try something like:

ListLinePlot[Transpose[
  Table[{Subscript[x, i][tmax], Subscript[y, i][tmax]} /. PSolve, {i, 1, n}]],
  PlotRange -> All]

Mathematica graphics

P.S. Your post's formatting looks great.

$\endgroup$

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.