5
$\begingroup$

Here is my code:

rawdata = http://pastebin.com/fgeZrSCp (* I hope pastebin is OK *)

f[x_] := Interpolation[rawdata, InterpolationOrder->1][(x + 1)*10]
  (* just creating a function with the data *)
d = 0.05;
T = 0.8;
wn = 2 Pi/T;
wd = wn*Sqrt[1 - d^2];

sol = 
  NDSolve[{xx''[t] + 2 d wn*xx'[t] + wn^2 xx[t] == -f[t], xx[0] == 0, xx'[0] == 0}, xx, 
    {t, 0, 65}, MaxSteps -> 500000];
Plot[Evaluate[xx[t] /. sol], {t, 0, 65}, PlotRange -> All]

Plot of the solution

My goal is to get the maximum value of Abs[xx[t]]. How can I do this in an efficient way?

Note that d and T can change. In this particular example the maximum should be around 0.052.

Context: Structural Dynamics, I'm trying to find a "response spectrum".

$\endgroup$
1
  • $\begingroup$ Well, you can't use NDSolve if you do not have a numerical value for d and T. So, once these are set, and you get your solution xx[t] from NDSolve, then you can use many of Mathematica maximum related functions, may be NMaxValue migth do it. "gives the maximum value of f with respect to x." reference.wolfram.com/mathematica/ref/NMaxValue.html $\endgroup$
    – Nasser
    Nov 3, 2013 at 5:21

1 Answer 1

6
$\begingroup$
(*just creating a function with the data*)
d = 0.05;
T = 0.8;
wn = 2 Pi/T;
wd = wn*Sqrt[1 - d^2]; 
f[x_] := Interpolation[rawdata, InterpolationOrder -> 1][(x + 1)*10] ; 
sol = NDSolve[{xx''[t] + 2 d wn*xx'[t] + wn^2 xx[t] == -f[t], xx[0] == 0, 
                xx'[0] == 0}, xx, {t, 0, 65}, MaxSteps -> 500000];
Show[Plot[Abs[xx[t]] /. sol, {t, 0, 65}, PlotRange -> All], 
 Graphics[{Red, PointSize[Large], 
           Point@{t /. #[[2]], #[[1]]} &@ NMaximize[{Abs[xx[t]] /. sol[[1]], 1 < t < 65}, t]}]]

Mathematica graphics

NMaximize always attempts to find a global maximum of f subject to the constraints given.

If f and cons are linear, NMaximize can always find global maxima, over both real and integer values.

Otherwise, NMaximize may sometimes find only a local maximum.

$\endgroup$
3
  • $\begingroup$ Thank you so much! Now I have to iterate through T to plot a curve of the maxima found and it's taking a year. Would it be possible to make my code run faster? (Should I open a new question for that?) $\endgroup$
    – Ivan
    Nov 3, 2013 at 15:59
  • 1
    $\begingroup$ @IvánVladimirGonzálezBustos sol = ParametricNDSolve[{xx''[t] + 2 d 2 Pi /T xx'[t] + (2 Pi/T)^2 xx[t] == -f[t], xx[0] == 0, xx'[0] == 0}, xx, {t, 0, 65}, {T}, MaxSteps -> 500000]; ss = Table[ NMaxValue[{Abs[xx[T][t]] /. sol[[1]], 1 < t < 65}, t], {T, .55, 3, .125}] ListPlot[ss, Joined -> True] $\endgroup$ Nov 3, 2013 at 16:24
  • 1
    $\begingroup$ @IvánVladimirGonzálezBustos For T < .5 the maxima found is unreliable $\endgroup$ Nov 3, 2013 at 16:25

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.