Message Boards Message Boards

GROUPS:

Plot a simple diagonal ball throw in water, changing resistance coefficient

Posted 9 months ago
866 Views
|
2 Replies
|
1 Total Likes
|

Hello!

I would like to plot a graph for ball that was thrown from (H1+H2) in the air diagonaly and when it hits the water at hight H1 resistance coefficent changes from ka (resistance coefficent of air) to kw (resistance coefficent of water)

How can I do it? I tried using function WhenEvent like this:

m = 0.5;
g = 9.81;
v0 = 10;
H1 = 10.;
H2 = 5.;
kw = 1; (*coeficient of fluid*)
ka = 0.1; (*coeficient of air*)

s = NDSolve[{
v = Sqrt[x'[t]^2 + y'[t]^2]; 

m x''[t] == -kz v x'[t], 
m y''[t] == -m g - kz v y'[t], 
x'[0] == v0, x[0] == 0, y'[0] == 0, y[0] == H1 + H2, 
WhenEvent[y[t] <= H1, Replace [ka, ka -> kw]]}, 
{x, y}, {t, 0, 10}];

{x[t], y[t]} = {x[t], y[t]} /. s[[1]]

ParametricPlot[{x[t], y[t]}, {t, 0, 6}, AxesLabel -> {"x[mm]", "y[mm]"}, PlotRange -> {{0, 10}, {0, 16}}]

Thank you!

POSTED BY: Leon F
Answer
2 Replies

Leon,

You do not need a WhenEvent because your discontinuity is in the parameters and not the integration variables:

s = NDSolve[{v = Sqrt[x'[t]^2 + y'[t]^2];
    m * x''[t] == -If[y[t] >= H1, ka, kw] * v * x'[t], 
    m * y''[t] == -m * g - If[y[t] >= H1, ka, kw] * v * y'[t], 
    x'[0] == v0, x[0] == 0, y'[0] == 0, y[0] == H1 + H2}, {x, y}, {t, 
    0, 10}];

Also, Delete this line -- it creates a definition that will mess up your results when you try to change parameters and it does nothing to help:

{x[t], y[t]} = {x[t], y[t]} /. s[[1]]

Just plot the results:

Plot[{x[t], y[t]} /. s, {t, 0, 6}]

ParametricPlot[{x[t], y[t]} /. s[[1]], {t, 0, 6}, 
 AxesLabel -> {"x[mm]", "y[mm]"}, PlotRange -> {{0, 10}, {0, 16}}]

If your discontinuity were a "nasty" one that causes chatter or other integration problems, you would consider adding a WhenEvent with a discrete variable. Something along the lines of:

s = NDSolve[{v = Sqrt[x'[t]^2 + y'[t]^2];
    m x''[t] == -(If[kz[t] == 1, ka, kw]) v x'[t], 
    m y''[t] == -m g - (If[kz[t] == 1, ka, kw]) v y'[t], x'[0] == v0, 
    x[0] == 0, y'[0] == 0, y[0] == H1 + H2, kz[0] == 0, 
    WhenEvent[y[t] == H1, kz[t] -> "DiscontinuitySignature"]}, {x, y, 
    kz}, {t, 0, 10}, DiscreteVariables -> {Element[kz, {-1, 0, 1}]}];

but you do not need it here because you pass through the discontinuity and do not "bang around" on top of it.

Also, you should post code using the upper left button so it formats properly, Lastly, the forum moderators will likely chastise you for posting the same issue twice. :)

Regards,

Neil

@Leon F please make sure you know the rules: https://wolfr.am/READ-1ST

The rules explain how to format your code properly. If you do not format code, it may become corrupted and useless to other members. Please EDIT your posts and make sure code blocks start on a new paragraph and look framed and colored like this.

int = Integrate[1/(x^3 - 1), x];
Map[Framed, int, Infinity]

enter image description here

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract