I needed some help with plotting a velocity vs forcing term diagram for a chaotic oscillator on mathematica.
Basically, I have to solve the following differential equation
x''[t] + b x'[t] - x[t] + x[t]^3 - f Cos[w t] == 0, x'[0] == 0,
x[0] == 0
and plot the velocity of my solution for times in the interval [0,1000] in increments of 2*Pi
for different values of f.
That is, for each f in the interval [0,2] (in increments of .05), I will have approximately 150 velocity points, and I must plot all of these points on one graph.
I though about using a do loop and came up with something like
Remove["Global`*"]
b = .1;
w = 1;
Period = 1;
tstep = 2 Pi/Period;
Do[{Do[{data =
Table[Flatten[
Evaluate[{f,
x'[t] /.
NDSolve[{x''[t] + b x'[t] - x[t] + x[t]^3 - f Cos[w t] == 0,
x'[0] == 0, x[0] == 0}, x[t], {t, 0, 1000},
MaxSteps -> 59999]}]], {t, 0, 1000, tstep}]}, {t, 0, 1000,
1}]}, {f, 0, 2, .1}]
but had no luck.
How can I do this?
Does this do what you want?
The Explanation
First, let me focus on
sol. This is close to your own code (with a change) but refactored for clarity, rather than buried inside the loops.sol :=is equivalent toSetDelayed[sol, ...NDSolveoperation is therefore not performed untilsolis used somewhereThe change I made was to extract this portion from the result of
NDSolve:I do this with
Part:NDSolve[...][[1, 1, 2]]It could also be done with
x[t] /. First @ NDSolve[...]This extracted portion is paired with the current value of
fin a list:{f, NDSolve[ ... }so that later they can be plotted.Now:
builds a table of the changing value of
solas it globally changes the value off. This is where NDSolve is performed.The result is a series of solutions for each value of
fin this form:Finally:
creates a table by evaluating the entire series of results created above for globally changing values of
t, andListPlots it.There are a few things more I would like to say but I am out of time. I will make a further edit in a few hours.