Reduce function syntax in Mathematica - wolfram-mathematica

I would like to know whats wrong with my code. I am trying to solve system of non-linear equations (initially in wolfram but the command was too long) in Mathematica:
Reduce[Pi*(h^2 + 2*R*(R - r))/sqrt (h^2 + (R - r)^2) - 2*x*Pi/3*h*R -
x*Pi/3*h*r == 0 &&
Pi*(h^2 + 2*r*(r - R))/sqrt (h^2 + (R - r)^2) + 2*Pi*r -
x*Pi/3*h*R - 2*x*Pi/3*h*r == 0 &&
Pi*h*(r + R)/sqrt (h^2 + (R - r)^2) - x*Pi/3*R^2 - x*Pi/3*R*r -
x*Pi/3*r^2 == 0 && -Pi/3*h*(R^2 + R*r + r^2) + 1 == 0, {R, r, h,
x}];
Do you know how to retype it and solve these equations? I tried to type it according to documentation, but I evidently made some mistake...
These are the original equations (in LaTeX, I dont know if they will show correctly here:
\begin{equation*}
\frac{\partial}{\partial R} L(R, r, h, \lambda) = \frac{\pi(h^2 + 2R(R-r))}{\sqrt{h^2 + (R - r)^2}} - 2\lambda \frac{\pi}{3}hR - \lambda \frac{\pi}{3}hr= 0
\end{equation*}
\begin{equation*}
\frac{\partial}{\partial r} L(R, r, h, \lambda) = \frac{\pi(h^2 + 2r(r-R))}{\sqrt{h^2 + (R - r)^2}} + 2\pi r - \lambda \frac{\pi}{3}hR - 2\lambda \frac{\pi}{3}hr= 0
\end{equation*}
\begin{equation*}
\frac{\partial}{\partial h} L(R, r, h, \lambda) = \frac{\pi h(r + R)}{\sqrt{h^2 + (R - r)^2}} - \lambda \frac{\pi}{3}R^2 - \lambda \frac{\pi}{3}Rr - \lambda \frac{\pi}{3}r^2= 0
\end{equation*}
\begin{equation*}
\frac{\partial}{\partial \lambda} L(R, r, h, \lambda) = - \frac{\pi}{3} h (R^2 + Rr + r^2) + 1 = 0
\end{equation*}
edit:
I corrected pi to PI, now it started evaluating so maybe it was the mistake...It just takes a very long time...

You have to learn at least the basics. Go to Help->Documentation Center and click on the book in the search bar. There is everything explained from the very start.
As already pointed out in the comments, all functions and built-in symbols start with a capital letter. Therefore your call should be
Reduce[Pi*(h^2 + 2*R*(R - r))/Sqrt[h^2 + (R - r)^2] - 2*x*Pi/3*h*R -
x*Pi/3*h*r == 0 &&
Pi*(h^2 + 2*r*(r - R))/Sqrt[h^2 + (R - r)^2] + 2*Pi*r -
x*Pi/3*h*R - 2*x*Pi/3*h*r == 0 &&
Pi*h*(r + R)/Sqrt[h^2 + (R - r)^2] - x*Pi/3*R^2 - x*Pi/3*R*r -
x*Pi/3*r^2 == 0 && -Pi/3*h*(R^2 + R*r + r^2) + 1 == 0, {R, r, h,
x}]

Related

Solving Large System of Non-Linear Equations Using Mathematica

So I'm trying to create a graph of mu as a function of a variable A, where mu is part of a solution to the following system of 17 non-linear equations (with lambda, A, and r as parameters). However, I can't seem to find a way for Mathematica to solve this. Does anyone know how to solve a large system of non-linear equations? Have been researching methods for almost a month now but still no luck.
Also, to graph mu versus A, do I need Mathematica to solve this, or is there another method?
Solve[
mu*(yB - xB) = (1 - mu)*(xS - yS) &&
0 == -lambda*xB*yS - lambda*xB*(A - xS) + mu*(A*(1 - A - yB) - (1 - A)*xB) &&
0 == -lambda*yB*xS + lambda*(1 - A - yB)*yS + mu*((1 - A)*(A - xB) - A*yB) &&
0 == -lambda*xS*yB + lambda*(A - xS)*xB + (1 - mu)*(A*(1 - A - yS) - (1 - A)*xS) &&
0 == -lambda*yS*xB - lambda*yS*(1 - A - yB) + (1 - mu)*((1 - A)*(A - xS) - A*yS) &&
r*VBhn == lambda/mu*yS*(VBho - VBhn - PSB) + lambda/mu*(A - xS)*(VBho - VBhn - PACh) + (1 - A)*(VBln - VBhn) &&
r*VBlo == lambda/mu*xS*(VBln - VBlo + PBS) + A*(VBho - VBlo) + (1 - delta) &&
r*VBln == lambda/mu*yS*(VBlo - VBln - PACl) + A*(VBhn - VBln) &&
r*VBho == (1 - A)*(VBlo - VBho) + 1 &&
r*VShn == lambda/(1 - mu)*yB*(VSho - VShn - PBS) + (1 - A)*(VSln - VShn) &&
r*VSlo == lambda/(1 - mu)*xB*(VSln - VSlo + PSB) + lambda/(1 - mu)*(1 - A - yB)*(VSln - VSlo + PACl) + A*(VSho - VSlo) + (1 - delta) &&
r*VSln == A*(VShn - VSln) &&
r*VSho == lambda/(1 - mu)*xB*(VShn - VSho + PACh) + (1 - A)*(VSlo - VSho) + 1 &&
VBho - VBhn - PSB == VSln - VSlo + PSB &&
VBln - VBlo + PBS == VSho - VShn - PBS &&
VBho - VBhn - PACh == VShn - VSho + PACh &&
VBlo - VBln - PACl == VSln - VSlo + PACl,
{mu, xS, xB, yS, yB, VBhn, VBlo, VBln, VBho, VShn, VSlo, VSln, VSho, PSB, PBS, PACh, PACl}
]

Mathematica Code with Module and If statement

Can I simply ask the logical flow of the below Mathematica code? What are the variables arg and abs doing? I have been searching for answers online and used ToMatlab but still cannot get the answer. Thank you.
Code:
PositiveCubicRoot[p_, q_, r_] :=
Module[{po3 = p/3, a, b, det, abs, arg},
b = ( po3^3 - po3 q/2 + r/2);
a = (-po3^2 + q/3);
det = a^3 + b^2;
If[det >= 0,
det = Power[Sqrt[det] - b, 1/3];
-po3 - a/det + det
,
(* evaluate real part, imaginary parts cancel anyway *)
abs = Sqrt[-a^3];
arg = ArcCos[-b/abs];
abs = Power[abs, 1/3];
abs = (abs - a/abs);
arg = -po3 + abs*Cos[arg/3]
]
]
abs and arg are being reused multiple times in the algorithm.
In a case where det > 0 the steps are
po3 = p/3;
b = (po3^3 - po3 q/2 + r/2);
a = (-po3^2 + q/3);
abs1 = Sqrt[-a^3];
arg1 = ArcCos[-b/abs1];
abs2 = Power[abs1, 1/3];
abs3 = (abs2 - a/abs2);
arg2 = -po3 + abs3*Cos[arg1/3]
abs3 can be identified as A in this answer: Using trig identity to a solve cubic equation
That is the most salient point of this answer.
Evaluating symbolically and numerically may provide some other insights.
Using demo inputs
{p, q, r} = {-2.52111798, -71.424692, -129.51520};
Copyable version of trig identity notes - NB a, b, p & q are used differently in this post
Plot[x^3 - 2.52111798 x^2 - 71.424692 x - 129.51520, {x, 0, 15}]
a = 1;
b = -2.52111798;
c = -71.424692;
d = -129.51520;
p = (3 a c - b^2)/3 a^2;
q = (2 b^3 - 9 a b c + 27 a^2 d)/27 a^3;
A = 2 Sqrt[-p/3]
A == abs3
-(b/3) + A Cos[1/3 ArcCos[
-((b/3)^3 - (b/3) c/2 + d/2)/Sqrt[-(-(b^2/9) + c/3)^3]]]
Edit
There is also a solution shown here
TRIGONOMETRIC SOLUTION TO THE CUBIC EQUATION, by Alvaro H. Salas
Clear[a, b, c]
1/3 (-a + 2 Sqrt[a^2 - 3 b] Cos[1/3 ArcCos[
(-2 a^3 + 9 a b - 27 c)/(2 (a^2 - 3 b)^(3/2))]]) /.
{a -> -2.52111798, b -> -71.424692, c -> -129.51520}
10.499

Using conditions to find imaginary and real part

I have used Solve to find the solution of an equation in Mathematica (The reason I am posting here is that no one could answer my question in mathematica stack.)The solution is called s and it is a function of two variables called v and ro. I want to find imaginary and real part of s and I want to use the information that v and ro are real and they are in the below interval:
$ 0.02 < ro < 1 ,
40
The code I used is as below:
ClearAll["Global`*"]
d = 1; l = 100; k = 0.001; kk = 0.001;ke = 0.0014;dd = 0.5 ; dr = 0.06; dc = 1000; p = Sqrt[8 (ro l /2 - 1)]/l^2;
m = (4 dr + ke^2 (d + dd)/2) (-k^2 + kk^2) (1 - l ro/2) (d - dd)/4 -
I v p k l (4 dr + ke^2 (d + dd)/2)/4 - v^2 ke^2/4 + I v k dr l p/4;
xr = 0.06/n;
tr = d/n;
dp = (x (v I kk/2 (4 dr + ke^2 (d + dd)/2) - I v kk ke^2 (d - dd)/8 - dr l p k kk (d - dd)/4) + y ((xr I kk (ro - 1/l) (4 dr + ke^2 (d + dd)/2)) - I v kk tr ke^2 (1/l - ro/2) + I dr xr 4 kk (1/l - ro/2)))/m;
a = -I v k dp/4 - I xr y kk p/2 + l ke^2 dp p (d + dd)/8 + (-d + dd)/4 k kk x + dr l p dp;
aa = -v I kk dp/4 + xr I y k p/2 - tr y ke^2 (1/l - ro/2) - (d - dd) x kk^2/4 + ke^2 x (d - dd)/8;
ca = CoefficientArrays[{x (s + ke^2 (d + dd)/2) +
dp (v I kk - l (d - dd) k p kk/2) + y (tr ro ke^2) - (d -
dd) ((-kk^2 + k^2) aa - 2 k kk a)/(4 dr + ke^2 (d + dd)/2) == 0, y (s + dc ke^2) + n x == 0}, {x, y}];
mat = Normal[ca];
matt = Last#mat;
sha = Solve[Det[matt] == 0, s];
shaa = Assuming[v < 100 && v > 40 && ro < 1 && ro > 0.03,Simplify[%]];
reals = Re[shaa];
ims = Im[shaa];
Solve[reals == 0, ro]
but it gives no answer. Could anyone help? I really appreciate any solution to this problem.
I run your code down to this point
mat = Normal[ca]
and look at the result.
There are lots of very tiny floating point coefficients, so small that I suspect most of them are just floating point noise now. Mathematica thinks 0.1 is only known to 1 significant digit of precision and your mat result is perhaps nothing more than zero correct digits now.
I continue down to this point
sha = Solve[Det[matt] == 0, s]
If you look at the value of sha you will see it is s->stuff and I don't think that is at all what you think it is. Mathematica returns "rules" from Solve, not just expressions.
If I change that line to
sha = s/.Solve[Det[matt] == 0, s]
then I am guessing that is closer to what you are imagining you want.
I continue to
shaa = Assuming[40<v<100 && .03<ro<1, Simplify[sha]];
reals = Re[shaa]
And I instead use, because you are assuming v and ro to be Real and because ComplexExpand has often been very helpful in getting Re to provide desired results,
reals=Re[ComplexExpand[shaa]]
and I click on Show ALL to see the full expanded value of that. That is about 32 large screens full of your expression.
In that are hundreds of
Arg[-1. + 50. ro]
and if I understand your intention I believe all those simplify to 0. If that is correct then
reals=reals/.Arg[-1. + 50. ro]->0
reduces the size of reals down to about 20 large screen fulls.
But there are still hundreds of examples of Sqrt[(-1.+50. ro)^2] and ((-1.+50. ro)^2)^(1/4) making up your reals. Unfortunately I'm expecting your enormous expression is too large and will take too long for Simplify with assumptions to be able to be practically effective.
Perhaps additional replacements to coax it into dramatically simplifying your reals without making any mistakes about Real versus Complex, but you have to be extremely careful with such things because it is very common for users to make mistakes when dealing with complex numbers and roots and powers and functions and end up with an incorrect result, might get your problem down to the point where it might be feasible for
Solve[reals == 0, ro]
to give you a meaningful answer.
This should give you some ideas of what you need to think carefully about and work on.

Finding the intersection of two lines

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.

RSolve not solving discrete Rossler system

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]

Resources