Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Suppose, I have an differential equation like this one:
mu1 u1[x] - u1''[x] - 10 u1[x] == 0
where mu1 is the eigenvalue and u1 is the eigenfuntion. Now, How can i calculate the eigenvalue mu1 numerically??? Can anyone help me out with this problem??
I'm assuming you want to solve something like
u1''[x] + 10 u1[x] == mu1 u1[x]
with boundary conditions
u1[x0] == 0; u1[x1] == 0; u1'[x0] =!= 0
for some x0 < x1. One way to do that is to first solve the differential equation plus the boundary conditions at x0, e.g.
sol = DSolve[{mu1 u1[x] - u1''[x] - 10 u1[x] == 0, u1[x0] == 0, u1'[x0] == 1}, u1, x][[1]]
which gives as output
{u1 -> Function[{x}, -((E^(-Sqrt[-10 + mu1] x - Sqrt[-10 + mu1] x0)
(-E^(2 Sqrt[-10 + mu1] x) + E^(2 Sqrt[-10 + mu1] x0)))/(2 Sqrt[-10 + mu1]))]}
We can then use this solution to find mu1 such that the boundary condition at x1 is satisfied:
sol1 = Solve[{u1[x1] == 0 /. sol[[1]], x1 > x0}, mu1, MaxExtraConditions -> All]
From which we find
{{mu1 -> ConditionalExpression[(10 x0^2 - 20 x0 x1 + 10 x1^2 - 4 \[Pi]^2 C[1]^2)/(
x0^2 - 2 x0 x1 + x1^2),
x0 \[Element] Reals && C[1] \[Element] Integers && C[1] >= 1 && x1 > x0]},
{mu1 -> ConditionalExpression[(-\[Pi]^2 + 10 x0^2 - 20 x0 x1 + 10 x1^2 -
4 \[Pi]^2 C[1] - 4 \[Pi]^2 C[1]^2)/(x0^2 - 2 x0 x1 + x1^2),
x0 \[Element] Reals && C[1] \[Element] Integers && C[1] >= 0 && x1 > x0]}}
Related
I have set of time dependent equations. 4 equations with 4 time dependent variables {r[t], c[t], Uo[t], U1[t]}.
Those 4 variables need to be used for a parametric transformation function
zJouko[o_] := r[t]*Exp[o*I] + (Uo[t]/(Exp[o*I] - c[t])) + U1[t]. o has nothing to do with the time parameter.
I need to plot this parametric function zJouko[o] for few time intervals on the same figure.
I have initial conditions for the 4 variables.
I have tried to use NSolve and then use its results to the plot but unsuccessfully.
Another problem is that when I launch Mathematica NSolve is working for several times and after that return empty solution.
I have tried this code unsuccessfully. I also don't know where to put the time intervals in the code.
some constants:
q2 = 0.5; mu1 = 1; mu2 = 1; tau = 1.0;
NSolve with the 4 equations and initial conditions
setEquation = NSolve[
{Uo[t]/c[t] == U1[t],
q2 == (r[t]/c[t]) + (Uo[t]*c[t]/(1 - ((c[t])^2))) + U1[t],
mu1*Exp[-t/tau] == r[t]*(r[t] - (Uo[t]/((c[t])^2))),
mu2*Exp[-t/tau] ==
Uo[t]*(((Uo[t])/((((c[t])^2) - 1)^2)) - r[t]/((c[t])^2)),
r[0] == 1/100, Uo[0] == -1/2, U1[0] == -5/12, c[0] == 6/5}, {r[t],
c[t], Uo[t], U1[t]}]
the function and the ParametricPlot:
zJouko[o_] := r[t]*Exp[o*I] + (Uo[t]/(Exp[o*I] - c[t])) + U1[t];
ParametricPlot[{Re[zJouko[o]], Im[zJouko[o]]}, {o, 0, 2 Pi}]
In a fresh start of Mathematica I give it
q2 = 1/2; mu1 = 1; mu2 = 1; tau = 1;
setEquation = Reduce[{Uo[t]/c[t] == U1[t],
q2 == (r[t]/c[t]) + (Uo[t]*c[t]/(1 - ((c[t])^2))) + U1[t],
mu1*Exp[-t/tau] == r[t]*(r[t] - (Uo[t]/((c[t])^2))),
mu2*Exp[-t/tau] == Uo[t]*(((Uo[t])/((((c[t])^2) - 1)^2)) - r[t]/((c[t])^2)),
r[0] == 1/100, Uo[0] == -1/2, U1[0] == -5/12, c[0] == 6/5},
{r[t], c[t], Uo[t], U1[t]}]
and it tells me r[t] is either plus or minus Sqrt[16 + E^t]/(4*Sqrt[2]*Sqrt[E^t]) and then given that choice of r[t]
c[t] == 4*r[t]
Uo[t] == r[t] - 16*r[t]^3
U1[t] == (1 - 16*r[t]^2)/4
Test the solution to see if it is correct.
q2 = 1/2; mu1 = 1; mu2 = 1; tau = 1;
r[t_]:=Sqrt[16 + E^t]/(4*Sqrt[2]*Sqrt[E^t]);
c[t_]:= 4*r[t];
Uo[t_]:=r[t] - 16*r[t]^3;
U1[t_]:= (1 - 16*r[t]^2)/4;
Simplify[ {Uo[t]/c[t] == U1[t],
q2 == (r[t]/c[t]) + (Uo[t]*c[t]/(1 - ((c[t])^2))) + U1[t],
mu1*Exp[-t/tau] == r[t]*(r[t] - (Uo[t]/((c[t])^2))),
mu2*Exp[-t/tau] == Uo[t]*(((Uo[t])/((((c[t])^2) - 1)^2)) - r[t]/((c[t])^2))}]
and this returns
{True, True, True, True}
Check your conditions at t==0
{r[0],c[0],Uo[0],U1[0]}//N
and it returns
{0.728869, 2.91548, -5.46651, -1.875}
Plot your function
zJouko[o_]:= r[t]*Exp[o*I] + (Uo[t]/(Exp[o*I] - c[t])) + U1[t];
Plot[Table[{Re[zJouko[o]], Im[zJouko[o]]},{t,0,2}], {o, 0, 2 Pi}]
Please try to check all this carefully and see if you can find that I have made a mistake anywhere.
I have two lines:
y = -1/3x + 4
y = 3x + 85
The intersection is at [24.3, 12.1].
I have a set of coordinates prepared:
points = [[1, 3], [4, 8], [25, 10], ... ]
#y = -1/3x + b
m_regr = -1/3
b_regr = 4
m_perp = 3 #(1 / m_regr * -1)
distances = []
points.each do |pair|
x1 = pair.first
y2 = pair.last
x2 = ((b_perp - b_regr / (m_regr - m_perp))
y2 = ((m_regr * b_perp) / (m_perp * b_regr))/(m_regr - m_perp)
distance = Math.hypot((y2 - y1), (x2 - x1))
distances << distance
end
Is there a gem or some better method for this?
NOTE: THE ABOVE METHOD DOES NOT WORK. See my answer for a solution that works.
What's wrong with using a little math?
If you have:
y = m1 x + b1
y = m2 x + b2
It's a simple system of linear equations.
If you solve them, your intersection is:
x = (b2 - b1)/(m1 - m2)
y = (m1 b2 - m2 b1)/(m1 - m2)
After much suffering and many different tries, I found a simple algebraic method here that not only works but is dramatically simplified.
distance = ((y - mx - b).abs / Math.sqrt(m**2 + 1))
where x and y are the coordinates for the known point.
For Future Googlers:
def solution k, l, m, n, p, q, r, s
intrsc_x1 = m - k
intrsc_y1 = n - l
intrsc_x2 = r - p
intrsc_y2 = s - q
v1 = (-intrsc_y1 * (k - p) + intrsc_x1 * (l - q)) / (-intrsc_x2 * intrsc_y1 + intrsc_x1 * intrsc_y2);
v2 = ( intrsc_x2 * (l - q) - intrsc_y2 * (k - p)) / (-intrsc_x2 * intrsc_y1 + intrsc_x1 * intrsc_y2);
(v1 >= 0 && v1 <= 1 && v2 >= 0 && v2 <= 1) ? true : false
end
The simplest and cleanest way I've found on the internet.
Related to my previous question, just wonder how to solve a system of linear equations with non-negative integral solutions, for example:
c11*x+c12*y+c13*z=d1
c21*x+c22*y+c23*z=d2
Thanks a lot!
Edit
I meant efficiently. For example, I could have used FrobeniusSolve to get two solution lists and try to find the intersection. But sometimes, the individual solution list is probably hugely large. Or try to verify each individual solution returned by one FrobeniusSolve to see whether they satisfy all the remaining equations, but that suffers from the same drawback.
Reduce is able to solve these types of problems.
To answer the specific case in your comment above:
In[1]:= solns = Reduce[x1 + 2 x2 + 5 x3 + 7 x4 == 40 &&
x1 + x2 + 2 x3 + x4 == 20 &&
x1 > 0 && x2 > 0 && x3 > 0 && x4 > 0,
{x1, x2, x3, x4}, Integers]
Out[1]= (x1 == 6 && x2 == 11 && x3 == 1 && x4 == 1) ||
(x1 == 7 && x2 == 8 && x3 == 2 && x4 == 1) ||
(x1 == 8 && x2 == 5 && x3 == 3 && x4 == 1) ||
(x1 == 9 && x2 == 2 && x3 == 4 && x4 == 1) ||
(x1 == 11 && x2 == 5 && x3 == 1 && x4 == 2) ||
(x1 == 12 && x2 == 2 && x3 == 2 && x4 == 2)
Edit:
You can check that this is the same solution you get by solving the two equations separately and taking the intersection of their solutions:
In[2]:= a = Reduce[x1 + 2 x2 + 5 x3 + 7 x4 == 40 &&
x1 > 0 && x2 > 0 && x3 > 0 && x4 > 0,
{x1, x2, x3, x4}, Integers];
b = Reduce[x1 + x2 + 2 x3 + x4 == 20 &&
x1 > 0 && x2 > 0 && x3 > 0 && x4 > 0,
{x1, x2, x3, x4}, Integers];
In[4]:= solns == Intersection[a, b]
Out[4]= True
And you can extract the solutions by, e.g.,
turning the solutions into a list of replacement rules
and applying to the variables:
In[5]:= {x1, x2, x3, x4} /. {ToRules[solns]}
Out[5]= {{6, 11, 1, 1}, {7, 8, 2, 1}, {8, 5, 3, 1},
{9, 2, 4, 1}, {11, 5, 1, 2}, {12, 2, 2, 2}}
I'm working with chaotic attractors, and testing some continuous-> discrete equivalences. I've made a continuous simulation of the Rossler system this way
a = 0.432; b = 2; c = 4;
Rossler = {
x'[t] == -y[t] - z[t],
y'[t] == x[t] + a*y[t],
z'[t] == b + x[t]*z[t]-c*z[t]};
sol = NDSolve[
{Rossler, x[0] == y[0] == z[0] == 0.5},
{x, y, z}, {t,500}, MaxStepSize -> 0.001, MaxSteps -> Infinity]
Now, when trying to evaluate a discrete equivalent system with RSolve, Mma doesn't do anything, not even an error, it just can't solve it.
RosslerDiscreto = {
x[n + 1] == x[n] - const1*(y[n] + z[n]),
y[n + 1] == 1 - a*const2)*y[n] + const2*x[n],
z[n + 1] == (z[n]*(1 - const3) + b*const3)/(1 - const3*x[n])}
I want to know if there is a numerical function for RSolve, analogous as the NDSolve is for DSolve.
I know i can make the computation with some For[] cycles, just want to know if it exists such function.
RecurrenceTable is the numeric analogue to RSolve:
rosslerDiscreto = {
x[n+1] == x[n] - C[1]*(y[n] + z[n]),
y[n+1] == (1 - a*C[2])*y[n] + C[2]*x[n],
z[n+1] == (z[n]*(1 - C[3]) + b*C[3]) / (1 - C[3]*x[n]),
x[0] == y[0] == z[0] == 0.5
} /. {a->0.432, b->2, c->4, C[1]->0.1, C[2]->0.1, C[3]->0.1};
coords = RecurrenceTable[rosslerDiscreto, {x,y,z}, {n,0,1000}];
Graphics3D#Point[coords]
I have a set of inequalities, for example,
2 x1 >=3 x2 && 0<=x1<=1 && 0<=x2<=1
which can be solved with Reduce. Then I want to do an integration for the function f(x1,x2)=1/x1 in the area defined by the inequalities above. In this case, Reduce gives a result
(x1 == 0 && x2 == 0) || (0 < x1 <= 1 && 0 <= x2 <= (2 x1)/3)
Then I can do integration by using
Integrate[Integrate[1/x1, {x2, 0, 2 x1/3}], {x1, 0, 1}]
But these all need my manual intervention. How do I do this in a streamline fashion? Many thanks!
Integrate[1/x1 Boole[2 x1 >= 3 x2 && 0 <= x1 <= 1 &&0 <= x2 <= 1], {x1, -\[Infinity], \[Infinity]}, {x2, -\[Infinity], \[Infinity]}]