Nsolve not doing anything - wolfram-mathematica

When I try to use Nsolve to solve for two variables it simply returns the input. What does this mean?
NSolve[{(1 - x)/x^2 == 2*1.2/Pi^2*5.5*10^(-10)*0.5^2*0.06/0.02*(2*Pi*T/(0.51*10^6))^(3/2)*Exp[13.6/T],
x*T^(3/2) == (Pi^2/(2*1.2))*67/(2*10^(-15)*5.5*10^(-10)*0.5^2*0.06/0.02*2.7^(3/2))}, {x, T}]

You are dealing with huge approximate decimal numbers so turn everything into exact rational
numbers
Simplify[{(1 - x)/x^2 == 2*(12/10)/Pi^2*(55/10)*10^(-10)*(5/10)^2*(6/100)/(2/100)*
(2*Pi*T/((51/100)*10^6))^(3/2)*Exp[(136/10)/T],
x*T^(3/2) == (Pi^2/(2*(12/10)))*67/(2*10^(-15)*(55/10)*10^(-10)*(5/10)^2*
(6/100)/(2/100)*(27/10)^(3/2))},x>0&&T>0]
returns
{(1 - x)/x^2 == (11*E^(68/(5*T))*Sqrt[3/(34*Pi)]*T^(3/2))/425000000000000000,
24057*T^(3/2)*x == 33500000000000000000000000000*Sqrt[30]*Pi^2}
assume x>0 and T>0 and so from the second equation
x == 33500000000000000000000000000*Sqrt[30]*Pi^2/(24057*T^(3/2))
substitute to eliminate x from the first equation and
Simplify[(1 - (33500000000000000000000000000*Sqrt[30]*Pi^2/(24057*T^(3/2))))/
(33500000000000000000000000000*Sqrt[30]*Pi^2/(24057*T^(3/2)))^2 ==
2*(12/10)/Pi^2*(55/10)*10^(-10)*(5/10)^2*(6/100)/(2/100)*
(2*Pi*T/((51/100)*10^6))^(3/2)*Exp[(136/10)/T],T>0]
returns
33500000000000000000000000000*Sqrt[6]*Pi^2*(210681*Sqrt[5] +
6700000000000*Sqrt[17]*E^(68/(5*T))*Pi^(3/2)) == 5068352817*T^(3/2)
then look at a Plot to estimate the location of a solution in T
Plot[33500000000000000000000000000*Sqrt[6]*Pi^2*(210681*Sqrt[5] +
6700000000000*Sqrt[17]*E^(68/(5*T))*Pi^(3/2)) - 5068352817*T^(3/2),{T,10^22,10^23}]
which shows there is a solution between T==8.4*10^22 and T==8.5*10^22
and thus a solution between x==3.092*10^-9 and x==3.0376*10^-9
Use FindRoot with bounding values to avoid more numerical issues
FindRoot[{(1 - x)/x^2 == 2*(12/10)/Pi^2*(55/10)*10^(-10)*(5/10)^2*(6/100)/(2/100)*
(2*Pi*T/((51/100)*10^6))^(3/2)*Exp[(136/10)/T],
x*T^(3/2) == (Pi^2/(2*(12/10)))*67/(2*10^(-15)*(55/10)*10^(-10)*(5/10)^2*
(6/100)/(2/100)*(27/10)^(3/2))},
{{x,3092*10^-12,30376*10^-13},{T,84*10^21,85*10^21}},WorkingPrecision->32]
which returns
{x -> 3.0625719427168463423734410827584827274789818044761`32.*^-9,
T -> 8.45379297284849685875884058676955508420831`32.*^22}
then
{(1 - x)/x^2==2*(12/10)/Pi^2*(55/10)*10^(-10)*(5/10)^2*(6/100)/(2/100)*
(2*Pi*T/((51/100)*10^6))^(3/2)*Exp[(136/10)/T],
x*T^(3/2)==(Pi^2/(2*(12/10)))*67/(2*10^(-15)*(55/10)*10^(-10)*(5/10)^2*
(6/100)/(2/100)*(27/10)^(3/2))}/.
{x -> 3.0625719427168463423734410827584827274789818044761`32.*^-9,
T -> 8.45379297284849685875884058676955508420831`32.*^22}
returns
{True,True}
Check every step of this to make certain that I have made no mistakes.

Related

Wolfram Mathematica Solve command for a nonlinear system of equations

I am trying to solve a nonlinear system of equations by using the Solve (and NSolve) command, but the evaluation get stuck.
For a very similar system, basically the same but with the derivatives of the equations I get no problems. I define the functions I need, write the equations, define the variables, define the solutions through the Solve command, and, once obtained with another system the initial values, I try to solve the system with NSolve.
Defining the functions:
a[x_] := A (1 - ms[x])
b[x_]:=2 ((ArcSinh[nn[x]/ms[x]] ms[x]^3 + nn[x] ms[x] Sqrt[nn[x]^2 + ms[x]^2])/(8 \[Pi]^2) + (ArcSinh[pp[x]/ms[x]] ms[x]^3 + pp[x] ms[x] Sqrt[pp[x]^2 + ms[x]^2])/(8 \[Pi]^2))
where A is a constant. Here I deleted some multiplicative constants to simplify the problem.
Then I have the equations:
eq1[x_]:= B a[x] + C a[x]^2 + D a[x]^3 - F b[x]
eq2[x_]:= pp[x]^3 - nn[x]^3
eq3[x_]:= G - (pp[x]^3 + nn[x]^3)
eq4[x_]:= Sqrt[nn[x]^2 + ms[x]^2] - Sqrt[pp[x]^2 + ms[x]^2] - Sqrt[m + ee[x]^2] + H (pp[x]^3 - nn[x]^3)
where B, C, D, G, m and H are constants. Here too, I deleted some multiplicative constants, to simplify the code for you.
Finally, I define the variables:
Var = {ee[x], pp[x], nn[x], ms[x]}
then solve the system "implicitly":
Sol =
Solve[{eq1[x] == 0, eq2[x] == 0, eq3[x] == 0, eq4[x] == 0}, Var]
(N.B: it is here that the code get stuck!!!! Despite, as I said, with a similar system with derivatives of the equations, everything work fine.)
and make a list of the equations:
eqs =
Table[Var[[i]] == (Var[[i]] /. Sol[[1]]), {i, Length[Var]}];
To conclude, after having obtained the initial conditions, I would try to solve the system:
system0 = Flatten[{eqs, ee[xi] == eei, pp[xi] == ppi, nn[xi] == nni, ms[xi] == msi}];
sol0 = NSolve[system0, {ee, kpp, nn, ms}, {x, xi, xf}, Flatten[{MaxSteps -> 10^4, MaxStepFraction -> 10^-2, WorkingPrecision -> 30, InterpolationOrder -> All}, 1]];
where I previously set xi = 10^-8 and xf = 10.
Trying to be more clear, when I try to evaluate the system through the Solve command, the evaluation continues indefinitely and I cannot understand why, where is the mistake. Despite a similar system with the derivative of the previous equations and NSolve replaced with NDSolve, works without any problem, and the execution of the "equivalent" line (Sol = Solve[{eq1[x] == 0, eq2[x] == 0, eq3[x] == 0, eq4[x] == 0}, Core]) is extremely fast (~1 sec).
Any help to understand where I am wrong is welcome, as well any suggestion to solve numerically this kind of system of equations.
Trying to be more clear, when I try to evaluate the system through the Solve command, the evaluation continues indefinitely and I cannot understand why, where is the mistake. Despite a similar system with the derivative of the previous equations and NSolve replaced with NDSolve, works without any problem, and the execution of the "equivalent" line (Sol = Solve[{eq1[x] == 0, eq2[x] == 0, eq3[x] == 0, eq4[x] == 0}, Core]) is extremely fast (~1 sec).
Any help to understand where I am wrong is welcome, as well any suggestion to solve numerically this kind of system of equations.

MATLAB error using symfun/subsindex

I'm having trouble using dsolve with symbolic functions. I'm receiving an error stating:
"Error using symfun/subsindex (line 121)
Indexing values must be positive integers, logicals or symbolic variables.
Error in VK3 (line 9)
[F(n), G(n), H(n)] = dsolve(diff(F) == F2, diff(G) == G2,..."
Here's my code as it stands. This may seem stupid to some, but I have relatively little experience with Matlab. If anyone could tell me where I'm going wrong, I'd be grateful.
syms F(n) G(n) H(n) F2(n) G2(n)
c = 1.004e-6;
m = input('Angular Velocity = ');
z = 0:1:20;
r = input('Radial Distance = ');
n = z*sqrt(m/c);
[F(n), G(n), H(n)] = dsolve(diff(F) == F2, diff(G) == G2,...
diff(F2) == F^2 - G^2 + F2*H,...
diff(G2) == 2*F + G2*H,...
diff(H) == -2*F,...
F(0) == 0, H(0) == 0, G(0) == 1, F(20) == 0, G(20) == 0);
U = m*r*F(n);
V = m*r*G(n);
W = sqrt(m/v)*H(n);
subplot(3,1,1)
plot(U,n), xlabel('U'), ylabel('z'),...
title('Radial Velocity Component')
subplot(3,1,2)
plot(V,n), xlabel('V'), ylabel('z'),...
title('Azimuthal Velocity Component')
subplot(3,1,3)
plot(W,n), xlabel('W'), ylabel('z'),...
title('Axial Velocity Component')
As the error message states, the issue is with the line calling dsolve. As the documentation indicates, this function either returns either
Symbolic array that contains solutions of an equation. The size of a symbolic array corresponds to the number of the solutions.
Structure array that contains solutions of a system of equations. The number of fields in the structure array corresponds to the number of independent variables in a system.
Variables to which the solver assigns the solutions of a system of equations. The number of output variables or symbolic arrays must equal the number of independent variables in a system. The toolbox sorts independent variables alphabetically, and then assigns the solutions for these variables to output variables or symbolic arrays.
In other words, it does not return symbolic functions (symfun). Thus, Matlab sees F(n) as array indexing rather than a symbolic function. I recommend using the structure array form:
S = dsolve(diff(F) == F2,
diff(G) == G2,...
diff(F2) == F^2 - G^2 + F2*H,...
diff(G2) == 2*F + G2*H,...
diff(H) == -2*F,...
F(0) == 0, H(0) == 0, G(0) == 1, F(20) == 0, G(20) == 0);
However, your system may not have an analytic solution (do you have reason to believe that it does?) because you'll get a warning:
Warning: Explicit solution could not be found.
and the output S will be empty. You could try applying assumptions. (Mathematica 10 doesn't fare any better, for what it's worth.)

Mathematica Order of an Equation

Is there a way to obtain the order of an ODE in mathematica.
For example, if i have y''+5y i want mathematica return 2 (beacuse it's a 2nd order equation). So, is it possible what i'm asking?
here is a way to extract the value automatically:
ode = y'' + y' + y == 0 ;
Max[Cases[ ode , Derivative[n_][y] :> n , Infinity]]
2
note this just finds the largest derivative in the expression, it doesn't verify if the expression is actually an ode..

is Valid Golomb Ruler

I want to write a function which can test whether a list of numbers is a valid Golomb ruler or not. I know how to do it in O(n^2) time (using nested for loops) but I am looking for a simple and more optimized way of doing it. I am trying to do it in python and my function takes a list of integers as argument.
A Golomb ruler is defined as a set :
Iff
(Wikipedia)
If this is 3SUM-hard, then no one knows how to do much better than quadratic. I would be amazed if this weren't 3SUM-hard, but the hypothetical reduction looks as though it would be rather technical.
it's my code in python which checks if the list of integers forms a rule of Golomb or not:
def is_golomb_ruler(ruler: list[int]) -> bool:
marks_count = len(ruler)
# check if first element is not 0
if ruler[0] != 0:
return False
# check if contains negative values
for item in ruler:
if item < 0:
return False
# check differences table
tab_diff = []
for i in range(marks_count - 1):
d = ruler[i + 1] - ruler[i]
if tab_diff.__contains__(d):
return False
tab_diff.append(d)
index = marks_count - 2
diff_len = 2
for i in range(index, 0, -1):
for j in range(i):
d = 0
for k in range(diff_len):
d += tab_diff[k + j]
if tab_diff.__contains__(d):
return False
tab_diff.append(d)
diff_len += 1
# case of two identical marks
if tab_diff.__contains__(0):
return False
return True
you find my repo of the complete Golomb problem solved by the metaheuristic method (Simulated annealing and genetic algorithms) here :
GolombProblem_Metaheuristic_Project

How to obtain values from a NMaximize in a For loop

I am using NMaximize to obtain values from an NDSolve function:
Flatten[NDSolve[{x''[t] == (F Cos[\[CapitalOmega] t] -
c x'[t] - (k + \[Delta]kb) x[t] + \[Delta]kb y[t])/m,
y''[t] == (-c y'[t] - (k + \[Delta]kb) y[t] + \[Delta]kb x[t])/m,
x'[0] == 0, y'[0] == 0, x[0] == 0, y[0] == 0}, {x[t], y[t]}, {t, 0, 10}]];
NMaximize[{Evaluate[y[t] /. s], 8 < t < 9}, t]
This is the case of a set of coupled, second order, ordinary differential equations (they were derived by a constant rotational speed gyroscope).
I need to obtain the maximum of the response function after the transient solution has faded and no longer influences the result.
I am trying to use a For loop to obtain the different maximums achieved for a range of "CapitalOmega", say 80 to 130 in steps of 1/2.
Currently I am getting the result in a form:
{a, {t -> b}}
How could This be placed on a list for all the values of "a" obtained from the For loop? This so they can be plotted using
ListLinePlot[]
If for each value of CapitalOmega you are getting some {a,{t->b}} from your NDSolve and you just want the list of 'a' values then
Table[First[NDSolve[...],{CapitalOmega,80,130,1/2}]
should do it. The First will extract the 'a' each time and using Table instead of For will put them in a list for you. If my example isn't exactly what your actual code is then you should still be able to use this idea to accomplish what you want.
Note: When I try to paste just your NDSolve[...] into Mathematica I get
NDSolve::ndnum: Encountered non-numerical value for a derivative at t==0.`.
which may be a real problem or may just be because of how you cut and pasted your posting.

Resources