First time using stackOverflow. :)
I'm trying to use mathematica to solve some simply polynomial equations (let's say in one variable) with constraints on the variable, e.g |x| < 1.
When I try something like:
Solve[x^2 == 4 && x^2 < 1, x]
I get an error stating that "x > 0 is not a well-formed equation".
The mathematica solve page even suggests this syntax on its second to last example, so I'm quite confused. (If it's relevant, I have version 7.) Any help would be appreciated.
Thanks!
Solve is not supposed to solve inequalities (M7). You can use Reduce to do that:
In[2]:= Reduce[x^2 == 4 && x^2 < 1, x]
Out[2]= False
Here is an example with Solve:
In[4]:= Solve[x^2 == 4 && x^4 == 16, x]
Out[4]= {{x -> -2}, {x -> 2}}
In Mma v 8:
{Solve[x^2 == 4 && x^2 < 1, x],
Solve[x^2 == 4 && (-1 < x < 1), x]}
(*
->{{},{}}
*)
Related
I'm try to use Mathematica to transform a couple of komplex equations, but I didn't get the right syntax.
Solve[{EI*w[x] == Piecewise[{{(x - a)^3, x >= a}, {0, x < a}}]*F*(1/6) +
ca*(x³)*(1/6) + cb*(x^2)*0.5 + cc*x + cd,
EI*w'[x] == Piecewise[{{(x - a)², x >= a}, {0, x < a}}]*F*0.5 + ca*(x^2)*0.5 + cb*x + cc,
EI*w''[x] == Piecewise[{{(x - a)^1, x >= a}, {0, x < a}}]*F + ca*x*0.5 + cb,
EI*w'''[x] == Piecewise[{{(x - a)^0, x >= a}, {0, x < a}}]*F + ca,
w[0] == -EIw'''[0]/ka, w[l] == EIw'''[0]/kb, w'[0] == 0, w'[l] == 0},
{ca, cb, cc, cd}]
Can someone give me a hint how to solve these equations with Mathematica.
Begin by cleaning up some typesetting issues.
We simplify this by looking only at the domain x>=a
Solve[{EI*w[x] == (x-a)^3*F*(1/6)+ca*(x^3)*(1/6)+cb*(x^2)*(1/2)+cc*x+cd,
EI*w'[x] == (x-a)^2*F*(1/2)+ca*(x^2)*(1/2)+cb*x+cc,
EI*w''[x] == (x-a)^1*F+ca*x*(1/2)+cb,
EI*w'''[x] == (x-a)^0*F+ca,
w[0] == -EI*w'''[0]/ka, w[1] == EI*w'''[0]/kb, w'[0] == 0, w'[1] == 0},
w[x], x]
Your second equation is just derivative of first equation and provides no additional information
In[1]:= D[(x-a)^3*F*(1/6)+ca*(x^3)*(1/6)+cb*(x^2)*(1/2)+cc*x+cd, x] ==
(x-a)^2*F*(1/2)+ca*(x^2)*(1/2)+cb*x+cc
Out[1]= True
Your third and fourth equations give information about ca
In[2]:= Simplify[D[(x-a)^1*F+ca*x*(1/2)+cb, x] == (x-a)^0*F+ca]
Out[2]= ca == 0
OR it seems more and more likely that this is the result of a typo in the equations from the original poster. Without more information from the original poster it isn't possible to decide whether this is correct or not. But the person should be able to look at the method used and get the solution needed.
Thus
w[x] == ((x-a)^3*F*(1/6)+cb*(x^2)*(1/2)+cc*x+cd)/EI
In[3]:= w'[x] == D[((x-a)^3*F*(1/6)+cb*(x^2)*(1/2)+cc*x+cd)/EI, x]
Out[3]= w'[x] == (cc+cb x+1/2 F (-a+x)^2)/EI
In[4]:= w'''[x] == D[((x-a)^3*F*(1/6)+cb*(x^2)*(1/2)+cc*x+cd)/EI, {x, 3}]
Out[4]= w'''[x] == F/EI
We already know ca, solve for the remaining three variables using the first three boundary conditions
In[5]:= Simplify[Solve[{((0-a)^3*F*(1/6)+cb*(0^2)*(1/2)+cc*0+cd)/EI == -EI*(F/EI)/ka,
((l-a)^3*F*(1/6)+cb*(l^2)*(1/2)+cc*l+cd)/EI == EI*(F/EI)/kb,
(cc+cb x+1/2 F (-a+0)^2)/EI == 0}, {cb, cc, cd}]]
Out[5]= {{cb -> (F (6 EI (ka+kb)+ka kb (3 a-l) l^2))/(3 ka kb l (l-2 x)),
cc -> -((F (3 a^2 ka kb l (l-2 x)+6 a ka kb l^2 x+
2 (6 EI (ka+kb)-ka kb l^3) x))/(6 ka kb l (l-2 x))),
cd -> 1/6 F (a^3-(6 EI)/ka)}}
Look at the final boundary condition
In[7]:= Simplify[(cc+cb x+1/2 F (-a+l)^2)/EI == 0 /. Out[5]]
Out[7]= {(F l (-2 a+l))/EI == 0}
Check every step of this carefully and verify that no errors were made.
Then go back and see if you can use these same methods for the domain x < a
I need to use NDSolve which in turn uses the solution from another ODE as function in terms of output from another NDSolve.
If I use the exact solution from the first differential equation inside the NDSolve, it's OK. But when I use the same solution in the form of function (which uses InterpolatingFunction) it does not work.
I believe, it's got to do with the structure of NDSolve output. Could anyone please enlighten me on this. Will be of great help!
The code is:
feq = 2 V alpha fip F''[fi] - (V^2 - (V^2 + sigma - 2 fi) (F'[fi])^2 + (F'[fi])^4
Frange[lo_, hi_] :=
Module[{fii, sol},
sol = NDSolve[{(feq == 0 /.fi -> fii), F[0] == 0}, F, {fii, lo, hi}]]
eqpois = fi''[x] == ne[x] - F[fi[x]]/.sol
NDSolve[{eqpois, fi'[0] == 0, fi[0] == 0}, fi, {x,0,1}]
Here in order to find F[phi], I need to solve the 1st diff eq that is feq, which is solved by NDSolve inside the function Frange[lo,hi]. The solution is then used inside the second equation eqpois, which has to be solved using NDSolve again. The problem comes up in the second NDSolve, which does not produce the result. If I use the analytical solution of F[phi] in eqopis, then there is no problem.
Example Problem
I have done a little experiment with this. Let's take an example of coupled ODEs
1st eqn : dg/dx = 2f(g) with initial condition g(0) = 1
The function f(y) is a solution from another ODE, say,
2nd eqn : df/dy = 2y with IC f(0) = 0
The solution of the 2nd ODE is f(y) = y^2 which when put into the the 1st ODE becomes
dg/dx = 2 g^2 and the final solution is g(x) = 1/(1-2x)
The issue:
When I use DSolve, it finds the answer correctly
In[39]:= s = DSolve[{f'[y] == 2 y, f[0] == 0}, f, y]
Out[39]= {{f -> Function[{y}, y^2]}}
In[40]:= ss = DSolve[{g'[x] == 2 (f[g[x]]/.First#s), g[0] == 1}, g, x]
Out[40]= {{g -> Function[{y}, 1/(1 - 2 x)]}}
The problem comes when I use NDSolve
In[41]:= s = NDSolve[{f'[y] == 2 y, f[0] == 0}, f, {y, 1, 5}]
Out[41]= {{f -> InterpolatingFunction[{{1., 5.}}, <>]}}
In[42]:= ss1 = NDSolve[{g'[x] == 2 (Evaluate[f[g[x]]/.First#s1]), g[0] == 1}, g, {x, 1, 2}]
Out[42]= {}
The erros are:
During evaluation of In[41]:= InterpolatingFunction::dmval: Input value {2.01726} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
During evaluation of In[41]:= InterpolatingFunction::dmval: Input value {2.01726} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
During evaluation of In[41]:= InterpolatingFunction::dmval: Input value {2.04914} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
During evaluation of In[41]:= General::stop: Further output of InterpolatingFunction::dmval will be suppressed during this calculation. >>
During evaluation of In[41]:= NDSolve::ndsz: At y == 0.16666654771477857, step size is effectively zero; singularity or stiff system suspected. >>
During evaluation of In[41]:= NDSolve::ndsz: At y == 0.16666654771477857, step size is effectively zero; singularity or stiff system suspected. >>
Any help in this regard will be highly appreciated!
--- Madhurjya
I got your simple example to work with a little mod ..
f0 = First#First#DSolve[{f'[y] == 2 y, f[0] == 0}, f, y]
g0 = g /.
First#First#DSolve[{g'[x] == 2 (f[g[x]] /. f0), g[0] == 1}, g, x]
fn = f /. First#First#NDSolve[{f'[y] == 2 y, f[0] == 0}, f, {y, 0, 10}]
gn = g /.
First#First#
NDSolve[{g'[x] == 2 (fn[g[x]]), g[0] == 1}, g, {x, 0, 9/20}]
GraphicsRow[{
Plot[{g0#x, gn#x}, {x, 0, 9/20},
PlotStyle -> {{Thick, Black}, {Thin, Red, Dashed}}],
Plot[{f#x /. f0, fn#x}, {x, 0, 2},
PlotStyle -> {{Thick, Black}, {Thin, Red, Dashed}}]}]
note we need to ensure the y range in the first NDSolve is sufficient to cover the expected range of g from the second. That is where all those interpolation range errors come from.
Is it possible to work out the probability of 3 or more Head from 4 coin tosses using the Probability or NProbability functions.
This is not a question about the trivial answer to this problem, it is more to get an understanding of how to solve this kind of problem with Mathematica using distributions.
So using 4 random variables from Distribution P
I was hoping something like this would do the trick, but it does not work. I get 0.
P = BernoulliDistribution[0.5];
vars = List[Distributed[a,P],Distributed[b,P],Distributed[c,P],Distributed[c,P]];
NProbability[Count[ {a,b,c,d}, 1] >= 3, vars]
Any ideas would be greatly appreciated.
Not an expert using Mma for statistics here, but this seems to work:
l = TransformedDistribution[
x + y + w + z, {x \[Distributed] BernoulliDistribution[0.5],
y \[Distributed] BernoulliDistribution[0.5],
z \[Distributed] BernoulliDistribution[0.5],
w \[Distributed] BernoulliDistribution[0.5]}];
Table[NProbability[x > i, x \[Distributed] l], {i, -1, 4}]
(*
{1, 0.9375, 0.6875, 0.3125, 0.0625, 0.}
*)
In[10]:= Probability[a + b + c + d >= 3, vars]
Out[10]= 0.3125
Coin flipping is easier described with a BinomialDistribution:
In[12]:= Probability[m >= 3, m \[Distributed] BinomialDistribution[4, 0.5]]
Out[12]= 0.3125
I have a function, let's say for example,
D[x^2*Exp[x^2], {x, 6}] /. x -> 0
And I want to replace 6 by a general integer n,
Or cases like the following:
Limit[Limit[D[D[x /((-1 + x) (1 - y) (-1 + x + x y)), {x, 3}], {y, 5}], {x -> 0}], {y -> 0}]
And I want to replace 3 and 5 by a general integer m and n respectively.
How to solve these two kinds of problems in general in mma?
Many thanks.
Can use SeriesCoefficient, sometimes.
InputForm[n! * SeriesCoefficient[x^2*Exp[x^2], {x,0,n}]]
Out[21]//InputForm=
n!*Piecewise[{{Gamma[n/2]^(-1), Mod[n, 2] == 0 && n >= 2}}, 0]
InputForm[mncoeff = m!*n! *
SeriesCoefficient[x/((-1+x)*(1-y)*(-1+x+x*y)), {x,0,m}, {y,0,n}]]
Out[22]//InputForm=
m!*n!*Piecewise[{{-1 + Binomial[m, 1 + n]*Hypergeometric2F1[1, -1 - n, m - n,
-1], m >= 1 && n > -1}}, 0]
Good luck extracting limits for m, n integer, in this second case.
Daniel Lichtblau
Wolfram Research
No sure if this is what you want, but you may try:
D[x^2*Exp[x^2], {x, n}] /. n -> 4 /. x -> 0
Another way:
f[x0_, n_] := n! SeriesCoefficient[x^2*Exp[x^2], {x, x0, n}]
f[0,4]
24
And of course, in the same line, for your other question:
f[m_, n_] :=
Limit[Limit[
D[D[x/((-1 + x) (1 - y) (-1 + x + x y)), {x, m}], {y, n}], {x ->
0}], {y -> 0}]
These answers don't give you an explicit form for the derivatives, though.
I'm currently doing some normalization along the lines of:
J = Integrate[Psi[x, 0]^2, {x, 0, a}]
sol = Solve[J == 1, A]
A /. sol
For this type of normalization, the negative square root is extraneous. The result of this calculation is:
In[49]:= J = Integrate[Psi[x, 0]^2, {x, 0, a}]
Out[49]= 2 A^2
In[68]:= sol = Solve[J == 1, A]
Out[68]= {{A -> -(1/Sqrt[2])}, {A -> 1/Sqrt[2]}}
Even if I try giving it an Assuming[...] or Simplify[...], it still gives me the same results:
In[69]:= sol = Assuming[A > 0, Solve[J == 1, A]]
Out[69]= {{A -> -(1/Sqrt[2])}, {A -> 1/Sqrt[2]}}
In[70]:= sol = FullSimplify[Solve[J == 1, A], A > 0]
Out[70]= {{A -> -(1/Sqrt[2])}, {A -> 1/Sqrt[2]}}
Can anyone tell me what I'm doing wrong here?
I'm running Mathematica 7 on Windows 7 64-bit.
ToRules does what the box says: converts equations (as in Reduce output) to rules. In your case:
In[1]:= ToRules[Reduce[{x^2==1,x>0},x]]
Out[1]= {x->1}
In[2]:= {ToRules[Reduce[{x^2==1},x]]}
Out[2]= {{x->-1},{x->1}}
For more complex cases, I have often found it useful to just check the value of the symbolic solutions after pluging in typical parameter values. This is not foolproof, of course, but if you know there is one and only one solution then it is a simple and efficient method:
Solve[x^2==someparameter,x]
Select[%,((x/.#)/.{someparameter-> 0.1})>0&]
Out[3]= {{x->-Sqrt[someparameter]},{x->Sqrt[someparameter]}}
Out[4]= {{x->Sqrt[someparameter]}}
Solve doesn't work like this. You might try Reduce, instead, e.g.
In[1]:= Reduce[{x^2 == 1, x > 0}, x]
Out[1]= x == 1
It's then a little tricky to transform this output to replacement rules, at least in the general case, because Reduce might use arbitrary many logical connectives. In this case, we could just hack:
In[2]:= Solve[Reduce[{x^2 == 1, x > 0}, x], x]
Out[2]= {{x->1}}