Mathematica's error by solving an ODE - wolfram-mathematica

I would know what is the problem with this Mathematica's code. Is there anyone that can give me an explanation of the bug, and also that can tell me how to improve the code?
V[x_, y_, z_] := x^2 + y - z;
m = 10;
DSolve[m*x''[t] == -Grad[V[x, y, z], {x, y, z}]*x[t], x[t], t]
Model of a particle in a potential-field. In this model we consider a particle as being a point of mass which describes a trajectory in space which is modeled by a function giving its coordinates in space as a function of time. The potential field is given by a function V : R^3 → R and the trajectory is a solution of the differential equation
Note this model assumes the particle is a point mass, which is certainly known to be false in many cases in which we use this model; for example, as a model of planetary motion.
Actual equation:

First: don't trust Wikipedia. It good for some basic knowledge, but for something specific better use some field-specific sources.
The correct equation is:
And the correct code:
V[x_, y_, z_] := x^2 + y - z;
m = 10;
DSolve[m*{x''[t], y''[t], z''[t]} ==
-(Grad[V[x, y, z], {x, y, z}] /. {x -> x[t], y -> y[t], z -> z[t]})
, {x[t], y[t], z[t]}, t]
Solution:
{{x[t] -> C[1] Cos[t/Sqrt[5]] + C[2] Sin[t/Sqrt[5]],
y[t] -> -(t^2/20) + C[3] + t C[4],
z[t] -> t^2/20 + C[5] + t C[6]}}

Related

Give Matematica Solve function only references to equations instead of whole equations?

How do I supply the Matematica Solve function with only references to equations, instead of whole equations?
I.e. let's say I have multiple equations, like:
eq1 = {x + y == 0}
eq2 = {x y - 2 == 0}
However, when I try to put them in Solve, Matematica throws an error:
Solve[{eq1 && eq2}, {x, y}]
Solve::naqs: {x+y==0}&&{-2+x y==0} is not a quantified system of equations and inequalities.
At the same time, re-writing whole equations works
Solve[x + y == 0 && x y - 1 == 0, {x, y}]
{{x->-I,y->I},{x->I,y->-I}}
but can quickly become cumbersome when the number and complexity of equations increases.

Using Dsolve in mathematica

I was using dsolve to find the solved differential equation for the Schrodinger equations involving magnetic resonance and i am getting an error.
DSolve[{I*a'[t] == .5*(w0*a[t] + w1*Cos[w*t]*b[t]),
I*b'[t] == .5*(w1*Cos[w*t]*a[t] - w0*b[t]),
a[t]^2 + b[t]^2 == 1}, {a, b}, t]
from this i am getting the reply
There are fewer dependent variables than equations, so the system is \
overdetermined.
i see 3 equation and 3 unknowns so i do not really know why it is doing this.
Any help would be appreciated.
Perhaps this will help.
Ref. NMR Transition Rate in a Oscillating B Field
DSolve[
b'[t] == ((I w1)/2) (Exp[I (w + w0) t] + Exp[-I (w - w0) t]) - I w0 b[t],
b[t], t] // Simplify

Mathematica NDSolve

I have a question about NDSolve function in Mathematica.
I have an oscillator defined by these two equations:
x' = v
v' = -x - u*v^3
where u is some constant.
How to create an NDSolve that resolves this? I tried following code (it has to depend on time) but it doesnt work:
eq1 = x'[t] == v;
eq2 = v' == -x[t] - u*v^3;
eq3 = x[0] == 2;
(initial displacement is 2m).
s = NDSolve[{eq1, eq2, eq3}, x, {t, 0, 30}]
Thank you very much...
You need to observe that the first equation once differentiated with respect to t can be used to substitute for v[t]. But then the second equation becomes a ODE of second order and requires to be supplied with another extra initial condition. We will give
v[0]==x'[0]==some number
Then after solving this ODE for x you can recover v[t]==x'[t]
I give you the solution in term of a Manipulate so that geometrically the situation becomes clear to you.
(* First equation *)
v[t] = x'[t];
(*
Differentiate this equation once and substitute
for v[t] in the second equation
*)
Manipulate[
With[{u = Constant, der = derval},
res = NDSolve[{x''[t] == -x[t] - u*x'[t]^3, x[0.] == 2,x'[0.] == der},
x, {t, 0., 30.}] // First;
Plot[Evaluate[{x[t], v[t]} /. res], {t, 0, 30}, PlotRange -> All,
Frame -> True,Axes -> None, ImageSize -> 600]
],
{{Constant, 0.,TraditionalForm#(u)}, 0.,3, .1},
{{derval, -3., TraditionalForm#(v[0] == x'[0])}, -3, 3, .1}
]
Hope this helps you but next time before you ask you need to brush up the theory first as you can see the question you asked concerns very basic and elementary Mathematics not Mathematica programming. Good luck!!
You need to specify a numeric value for your u as well as an initial condition for v[t] :
u=1.0;
solution=NDSolve[{x'[t]==v[t], v'[t]==-x[t]-u v[t]^3,x[0]==2,v[0]==-1},{x,v},{t,0,1}]
Plot[{solution[[1,1,2]][t],solution[[1,2,2]][t]},{t,0,1}]

Mathematica: Tangent of Two Curves

I asked this question yesterday but not sure if I made clear what I was looking for. Say I have two curves defined as f[x_]:=... and g[x_]:=... as shown below. I want to use Mathematica to determine the abscissa intersection of the tangent to both curves and store value for each curve separately. Perhaps this is really a trivial task, but I do appreciate the help. I am an intermediate with Mathematica but this is one I haven't been able to find a solution to elsewhere.
f[x_] := x^2
g[x_] := (x - 2)^2 + 3
sol = Solve[(f[x1] - g[x2])/(x1 - x2) == f'[x1] == g'[x2], {x1, x2}, Reals]
(* ==> {{x1 -> 3/4, x2 -> 11/4}} *)
eqns = FlattenAt[{f[x], g[x], f'[x1] x + g[x2] - f'[x1] x2 /. sol}, 3];
Plot[eqns, {x, -2, 4}, Frame -> True, Axes -> None]
Please note that there will be many functions f and g for which you won't find a solution in this way. In that case you will have to resort to numerical problem solving methods.
You just need so solve a system of simultaneous equations:
The common tangent line is y = a x + b.
The common slope is a = f'(x1) = g'(x2)
The common points are a x0 + b = f(x0) and a x1 + b = g(x1).
Depending on the nature of the functions f and g this may have no, one, or many solutions.

find minimum of a function defined by integration in Mathematica

I need to find the minimum of a function f(t) = int g(t,x) dx over [0,1]. What I did in mathematica is as follows:
f[t_] = NIntegrate[g[t,x],{x,-1,1}]
FindMinimum[f[t],{t,t0}]
However mathematica halts at the first try, because NIntegrate does not work with the symbolic t. It needs a specific value to evaluate. Although Plot[f[t],{t,0,1}] works perferctly, FindMinimum stops at the initial point.
I cannot replace NIntegrate by Integrate, because the function g is a bit complicated and if you type Integrate, mathematica just keep running...
Any way to get around it? Thanks!
Try this:
In[58]:= g[t_, x_] := t^3 - t + x^2
In[59]:= f[t_?NumericQ] := NIntegrate[g[t, x], {x, -1, 1}]
In[60]:= FindMinimum[f[t], {t, 1}]
Out[60]= {-0.103134, {t -> 0.57735}}
In[61]:= Plot[f[t], {t, 0, 1}]
Two relevant changes I made to your code:
Define f with := instead of with =. This effectively gives a definition for f "later", when the user of f has supplied the values of the arguments. See SetDelayed.
Define f with t_?NumericQ instead of t_. This says, t can be anything numeric (Pi, 7, 0, etc). But not anything non-numeric (t, x, "foo", etc).
An ounce of analysis...
You can get an exact answer and completely avoid the heavy lifting of the numerical integration, as long as Mathematica can do symbolic integration of g[t,x] w.r.t x and then symbolic differentiation w.r.t. t. A less trivial example with a more complicated g[t,x] including polynomial products in x and t:
g[t_, x_] := t^2 + (7*t*x - (x^3)/13)^2;
xMax = 1; xMin = -1; f[t_?NumericQ] := NIntegrate[g[t, x], {x, xMin, xMax}];
tMin = 0; tMax = 1;Plot[f[t], {t, tMin, tMax}];
tNumericAtMin = t /. FindMinimum[f[t], {t, tMax}][[2]];
dig[t_, x_] := D[Integrate[g[t, x], x], t];
Print["Differentiated integral is ", dig[t, x]];
digAtXMax = dig[t, x] /. x -> xMax; digAtXMin = dig[t, x] /. x -> xMin;
tSymbolicAtMin = Resolve[digAtXMax - digAtXMin == 0 && tMin ≤ t ≤ tMax, {t}];
Print["Exact: ", tSymbolicAtMin[[2]]];
Print["Numeric: ", tNumericAtMin];
Print["Difference: ", tSymbolicAtMin [[2]] - tNumericAtMin // N];
with the result:
⁃Graphics⁃
Differentiated integral is 2 t x + 98 t x^3 / 3 - 14 x^5 / 65
Exact: 21/3380
Numeric: 0.00621302
Difference: -3.01143 x 10^-9
Minimum of the function can be only at zero-points of it's derivate, so why to integrate in the first place?
You can use FindRoot or Solve to find roots of g
Then you can verify that points are really local minimums by checking derivates of g (it should be positive at that point).
Then you can NIntegrate to find minimum value of f - only one numerical integration!

Resources