Tag Info

Hot answers tagged

12

You have to create your own mesh and you have to convert your u and v to mesh interpolations. (In the example in the documentation, NDSolveValue does this itself in constructing uif, vif.) Example: Needs["NDSolve`FEM`"] mesh = ToElementMesh[FullRegion[2], {{0, 5}, {0, 1}}]; u = Function[{x, y}, x (y - 0.5)/25]; v = Function[{x, y}, -x^2/50]; uif = ...


9

The problems that I can see are that NDSolve has to rewrite the equation system and determine which solver to use at every step. NDSolve has to interpolate the solution over all values, but you only need the last value. You evaluate θ'[2π], but you only ask NDSolve for θ, not its derivative. So at each step you are actually taking the derivative of the ...


7

You need the magic of "Pseudospectral": n = 35; g = 9.81; a = 1350; L = 3500; T = 30; h0 = 4; v0 = Sqrt[2 g h0]; R = 0.003; sol = NDSolve[ {D[h[x, t], x] - R v[x, t] Abs[v[x, t]] == D[v[x, t], t]/g, D[v[x, t], x] == g D[h[x, t], t]/a^2, v[x, 0] == v0, v[0, t] == v0 Exp[-(t^2/0.4)], h[L, t] == h0, h[x, 0] ...


6

Method 1, using Laplace transform eq = y'[t] + Integrate[y[x], {x, 0, t}] == Exp[-t]; eq = LaplaceTransform[eq, t, s]; eq /. LaplaceTransform[y[t], t, s] -> U0 sol = Solve[%, U0] Simplify@InverseLaplaceTransform[U0 /. sol, s, t] % /. y[0] -> 0 Method 2, convert to second order ODE You can, but you are missing a second initial condition. This is ...


5

The short answer is there is no solution. The problem alluded to in the tutorial is that the derivative expression -Sqrt[y[x]^3] (or equivalently y[x]^(3/2)) is discontinuous in a complex neighborhood of y[0] == -2. The discontinuity arises from the branch-cut choice in Mathematica. As for NDSolve, it fails to complete a solution because it tries to ...


5

A list of styles is the syntax for giving a series of styles to be applied cyclically to each of a series of plot elements. For example: VectorPlot[{{y, -x}, {x, y}}, {x, -3, 3}, {y, -3, 3}, VectorStyle -> {Gray, Arrowheads[0]}] Observe that one field gets Gray while the other gets Arrowheads[0]. The correct syntax should be to use a sub-list (as ...


5

Perhaps it's overshooting, but I like this way: RC = 4 Vin = 2 s = NDSolve[{It'[t] + (1/RC) It[t] == q[t], It[0] == 0, q[0] == 0, WhenEvent[t == 10, q[t] -> 1]}, It, {t, 0, 30}, DiscreteVariables -> q] Plot[Evaluate[It[t] /. s], {t, 0, 30}, PlotRange -> All]


5

With the option MaxStepSize -> 1., it seems to work. (A little bit magic) sol = NDSolve[{ D[h[x, t], x] - R*v[x, t]*Abs[v[x, t]] == 1/g D[v[x, t], t], D[v[x, t], x] == g/a^2*D[h[x, t], t], v[x, 0] == v0, v[0, t] == v0 Exp[-t^2/0.4], h[L, t] == h0, h[x, 0] == h0}, {h, v}, ...


4

Example using vector notation: s = NDSolve[{m'[t] == Cross[m[t], {0, 0, 1}], m[0] == {0, 1, 0}}, m, {t, 0, 1}] Plot[Evaluate[m[t] /. s], {t, 0, 1}] s = NDSolve[{m'[t] == Cross[m[t], {Cos@t, Sin@t, 1}], m[0] == {1, 1, 0}}, m, {t, 0, 10}]; ParametricPlot3D[m[t] /. s, {t, 0, 10}]


3

What you get as result are InterpolatingFunction objects. These can be used like pure functions. Therefore, you can transform the result of NDSolve so that you can call it like a function: sol = NDSolve[{ca'[t] == -0.1*ca[t]*cb[t],cb'[t] == -0.1*2*ca[t]*cb[t], ca[0] == 10, cb[0] == 12}, {ca, cb}, {t, 0, 5}]; f = With[{exp = Through[({ca, cb} /. ...


3

Just add this one line u[t_, x_] := {u1[t, x], u2[t, x]}; This tells M all u's in the code are vectors: eq = {D[u[t, x], t] == D[u[t, x], {x, 2}], u[0, x] == {1, 0}, u[t, 0] == {Cos[t], Sin[t]}, u[t, 5] == {1, 0}}; NDSolve[eq, u[t, x], {t, 0, 10}, {x, 0, 5}] Update: For plotting, might be easier to use {u1,u2} instead of {u1[t,x],u2[t,x]}. Hence ...


3

Change u to u[t] in the call to NDSolve m = {{2, 0}, {0, 1}}; c = {{3, -.5}, {-.5, .5}}; k = {{4, -1}, {-1, 1}}; initu = u[0] == {{0}, {.1}}; initv = u'[0] == {{1}, {0}}; u[t_] := {{u1[t]}, {u2[t]}}; sol = NDSolve[{m.u''[t] + c.u'[t] + k.u[t] == {{Sin[2 t]}, {Sin[2 t]}}, initu, initv}, u[t], {t, 0, 100}] Now M sees that u[t] is {{u1[t]}, {u2[t]}} ...


3

In an earlier comment, I observed that NDSolve found the equation to be stiff, even when the calculation was begun a short distance d from StartY. Because the third root of Fnum is 0.000797339, d must be quite small, say, 0.0001. However, even with this choice of d, NDSolve promptly stops because the equation is stiff. It turns out, however, that NDSolve ...


2

Since you haven't provided some code I can run, I will use the example of the documentation. As you can read in the details section of the documentation of WhenEvent, what you want can probably be implemented using something like the following: Reap@ NDSolve[{(2 - f[x]) f'[x] == f[x], f[0] == 1, WhenEvent[Abs[f'[x]] > 10^6, Sow[x]; ...


2

I think I have solved this ODE (I didn't verify the solution). The problem with DSolve is Integrate was not terminating for this inhomogeneous equation. So what I did was solve the homogeneous equation, then applied variation of parameters described here: homode = -16c*m^2Cos[x]k[x] - c(Sin[3x] - 7Sin[x])k'[x] + 4c*Cos[x]Sin[x]^2k''[x] == 0; homsol = ...


2

eq = { x'[t] Sin[y[ t]] + x[t] y'[t] a == 1, y'[t] Cos[x[t]] - y[t] x'[t] b == 1} sol = ParametricNDSolve[{eq, x[0] == 1, y[0] == 1}, {x, y}, {t, 0, 1}, {a, b}] Manipulate[ ParametricPlot[{x[a, b][t], y[a, b][t]} /. sol, {t, 0, 1}, AspectRatio -> 1], {a, 0.5, 1}, {b, 0.5, 1}]


2

Set the style for vector fields: vf = VectorPlot[{1, p*Exp[-p]}, {t, 0, 5}, {p, 0, 3}, VectorScale -> {0.05, Automatic, None}, VectorPoints -> 20, VectorStyle -> {{Red, Arrowheads[0]}}, AspectRatio -> Automatic]


2

The step function comes on the right-hand side: i[t] /. NDSolve[{i'[t]/c + r i[t] == UnitStep[t], i[0] == 0} /. {c -> 1, r -> 4}, i[t], {t, 0, 5}]; The plot shows the current as the capacitor is being charged: Plot[%, {t, 0, 5}, PlotRange -> All] You can change the input to something else, for example a SquareWave input shows the capacitor ...


2

The problem with NDSolve methods is that (as far as I know) it always check boundary points. And this a problem, as you noted. I would recommend you to look at this wonderful approach. I adapted it to your problem below v = 10; eq = (W[y]*W'[y] + W[y]*v + (0.05 - 1.66667 y + (270.27 y)/(1 + 270.27 y) - (660. y)/(1 + 1666.67 y))); ...


2

Plot[D[variables /. sol, t] /. t -> u, {u, 0, 10}, Evaluated -> True]


2

You have to suppress symbolic evaluations by replacing your definition of avgR with, e.g. avgR[gn_, gp_, sn_, sp_, K_, a_?NumericQ, b_, n0_, p0_, tg_, ts_] := Log[(tot[tg + ts]/tot[0]) /. sol[gn, gp, sn, sp, K, a, b, n0, p0, tg, ts]] In NMaximize the 1/10 has to be removed NMaximize[{avgR[gnex, gpex, snex, spex, Kex, a, bex, n0ex, p0ex, tgex, tsex], a ...


1

Bug fixed in 10.0.2 Clear[y, x]; DSolve[D[y[x], x] - y[x]^2 + y[x]*Sin[x] - Cos[x] == 0, y[x], x, GeneratedParameters -> C]


1

Actually, the program as written works fine, but you forgot to define n. I tried it for n=3 and obtained credible results: n = 3; f[x_?VectorQ] := n^2*ListConvolve[{1, -2, 1}, x, {2, 2}, {1, 0}]; ans = X /. NDSolve[{X'[t] == f[X[t]], X[0] == (Range[n - 1]/n)^10}, X, {t, 0, 0.25}][[1]] and, for instance, ans[.2] gives {0.252832,0.584738}. Best wishes ...


1

In V10, DSolve gives the answer: {sol} = DSolve[ {ca'[t] == -1/10*ca[t]*cb[t], cb'[t] == -1/10*2*ca[t]*cb[t], ca[0] == 10, cb[0] == 12}, {ca, cb}, t] Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information. >> (* {{cb -> Function[{t}, 24/(-3 + 5 E^(4 t/5))], ...


1

For a numerical solution, we can use FindRoot to find the a for given b, c, d, and y[0] == yinit such that the solution to the IVP y[0] == yinit, y'[0] == -b*c, satisfies the boundary condition y'[-d] == 0. SeedRandom[0]; (* random data *) b0 = RandomReal[1, 3]; c0 = RandomReal[1, 3]; d0 = RandomReal[1, 3]; y0 = RandomReal[1, 3]; {solp} = ...


1

David Etler is right, it's a matter of evaluation order rather than inverse functions. The problem can be reproduced with the following simple example: Clear[y, t, sol, Derivative] y[t_] := y[t] /. First[sol] y[0] $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >> Wait, you clear sol, too! Though you reproduced the warning, this may be a ...


1

I looked at the documentation and it said "the inverse function that is used to represent the solution gives only one of the solutions to [the] equation." I believe what that means is in order to solve the equation Mathematica solved for another variable in terms of $y$ and then took the inverse to find $y$, but that inverse wasn't one-to-one, so some ...


1

By restriction I had in mind clipping: something like this. x1[t_] := Evaluate[Sin[Clip[phi1[t] /. sol, {-Pi/4, Pi/4}]]] y1[t_] := Evaluate[-Cos[Clip[phi1[t] /. sol, {-Pi/4, Pi/4}]]] x2[t_] := Evaluate[Sin[Clip[phi1[t] /. sol, {-Pi/4, Pi/4}]] + Sin[Clip[phi2[t] /. sol, {-Pi/4, Pi/4}]]] y2[t_] := ...


1

The problem is that c does not appear in y[t] /. First[sol] explicitly. I guess all you need to do is remove the colon in your assignment for y[t,c]. There is no need for a delayed evaluation, because sol gives a closed-form solution for any c. As to your question about removing the "asymptotes", you could have a look at Piecewise[], e.g. f[x_] = 1/x; ...


1

Just start integrating at $t_0$ instead of $0$. If you are hell-bent on getting the integrator to do nothing from $0$ to $t_0$, then use the UnitStep[] function.



Only top voted, non community-wiki answers of a minimum length are eligible