Maple solve system of equation showing "RootOf" - solver

I'm trying to solve the following system:
gx:=2*x*exp(x^2+y^2)-4*y
gy:=2*y*exp(x^2+y^2)-4*x
sys:={gx=0,gy=0}:
solve(sys,{x,y})
It then displays the following output:
{x = 0, y = 0}, {x = RootOf(2*_Z^2-ln(2)), y = RootOf(2*_Z^2-ln(2))}, {x = -RootOf(2*_Z^2-ln(2)-I*Pi), y = RootOf(2*_Z^2-ln(2)-I*Pi)}
The first "root" (0,0) is correct, however how do i remove that root of and whatever Z that is? Is it possible to get the correct answer out of it?

This is a great scenario for the function allvalues.
From the help page:
compute all possible values of expressions involving RootOfs
gx:=2*x*exp(x^2+y^2)-4*y;
gy:=2*y*exp(x^2+y^2)-4*x;
sys:={gx=0,gy=0}:
sol := solve(sys,{x,y}):
seq(allvalues(sol[i]), i= 1..numelems([sol])):
print~([%])[];
Notice, however, that you are not getting all solutions this way. There are infinitely many solutions to the problem; to get all solutions, use the optional argument allsolutions = true in the solve command:
sol2 := solve(sys,{x,y},allsolutions = true):
seq(allvalues(sol2[i]), i= 1..numelems([sol2])):
print~([%])[];
If you run this, you will see a new variable _Z1 that has a trailing tilde (~) - this tilde means there are assumptions on the variable. To see these assumptions use
about(_Z1);
Originally _Z1, renamed _Z1~:
is assumed to be: integer
This means that the above solutions work for any integer _Z1. Those are all your solutions and written in the expected way.

You can use fsolve to final numerical solutions,
restart;
gx:=2*x*exp(x^2+y^2)-4*y;
gy:=2*y*exp(x^2+y^2)-4*x;
sys:={gx=0,gy=0}:
fsolve(sys,{x,y})
{x = .5887050113, y = .5887050113}
sys:={gx=0.,gy=0.}:
solve(sys,{x,y})
{x = 0., y = 0.}, {x = .5887050112, y = .5887050112}, {x = -.5887050112, y = -.5887050112}, {x = -.9887236333-.7943556085*I, y = .9887236333+.7943556085*I}, {x = .9887236333+.7943556085*I, y = -.9887236333-.7943556085*I}

Related

Loop over fsolve Scilab

Just as a silly example, say that I wish to solve for the following nonlinear equation x^2 - F(c)=0, where c can take different values between zero and one and F is a standard normal CDF. If I wish to solve for one particular value of c, I would use the following code:
c = linspace(0,1,100);
L = length(c);
x0 = c;
function Y = eq(x)
Y = x^2 - cdfnor("PQ",x-c(1),0,1)
endfunction
xres = fsolve(x0(1),eq);
My question is: Is there a way to solve for the equation for each value of c (and not only c(1))? Specifically, if I can use a loop over fsolve? If so, how?
Just modify your script like this:
c = linspace(0,1,100);
L = length(c);
x0 = c;
function Y = eq(x)
Y = x^2 - cdfnor("PQ",x-c,zeros(c),ones(c))
endfunction
xres = fsolve(x0,eq);

Avoid Numpy Index For loop

Is there any way to avoid using a second for loop for an operation like this?
for x in range(Size_1):
for y in range(Size_2):
k[x,y] = np.sqrt(x+y) - y
Or is there a better way to optimize this? Right now it is incredibly slow for large sizes.
Here's a vectorized solution with broadcasting -
X,Y = np.ogrid[:Size_1,:Size_2]
k_out = np.sqrt(X+Y) - Y
Supplementing Divakar's solution: If Y and X are not new ranges but some preexisting vectors of numbers, use np.ix_:
Y, X = np.array([[1.3, 3.5, 2], [2.0, -1, 1]])
Y, X = np.ix_(Y, X) # does the same as Y = Y[:, None]; X = X[None, :]
out = np.sqrt(Y+X) - X

Mathematica Solve function gives incorrect solution, why?

Please look into attached file. I’m using Mathematica Solve function to solve some simple equations from physics. One of the equations is an If function which defines function value when a condition is met. Solve finds almost correct solution which itself is a ConditionalExpression. For independent variable θ = 90° the answer given by Solve is in error. It seems that Solve forgets the case when Cos equals 0. Why? Thanks.
Regards/Mikael
Specifying theta as a real solves the problem.
w = 1500;
mus = 0.4;
fv = f Cos[theta Degree];
fh = f Sin[theta Degree];
fn = fv + w;
ff = If[mus fn >= 0, mus fn, 0];
frul = Quiet#Solve[fh == ff, f, Reals];
f /. frul /. theta -> 90.
{600.}
f /. frul /. theta -> 90
{Undefined}
Same again, with radians.
w = 1500;
mus = 0.4;
fv = f Cos[theta];
fh = f Sin[theta];
fn = fv + w;
ff = If[mus fn >= 0, mus fn, 0];
frul = Quiet#Solve[fh == ff, f, Reals];
f /. frul /. theta -> N[Pi/2]
{600.}
f /. frul /. theta -> Pi/2
{Undefined}
Many thanks Chris.
Yes, giving it real numbers yields correct answers. This is because Cos[90.0°] is 6.123233995736766E-17 whereas Cos[90°] is 0. The solution is the same but we are fooling it with finite machine precision.
If I ask me, I would say that this is a bug in equation solver in Mathematica. The solution produced by Solve[] should test for Cos[] >= 0. Now it tests for Cos[] > 0 which is not true for Cos[90°].

mathematica, picking our linear terms from an equation

so I'm trying to pick out the linear terms in an expression - for example if I say
eqn = dy/dt + y == y^2
, dy/dt+y is linear and y^2 is non linear.
I know in this case I can just use
eqn[[1]] to pull out the lhs and have that be my linear terms, but is there some way I can use a string pattern or something to get the linear parts of any entered equation?
This is some quick solution I came up with. You may need to change this for your purposes, but maybe it is a possibility. First the terms are converted into a string. This is then searched for "+" or "-" and separated. The individual terms are tested for linearity.
terms = y + y^2 - Sqrt[y];
string = ToString[terms, InputForm];
split = StringSplit[string, {"+", "-"}];
Do[
(*Take term i and convert the string to an expression*)
exp = ToExpression[split[[i]]];
Print[exp];
(*Test for Additivity: f(x+y)= f(x)+f(y)*)
pexp = exp /. {y -> y + c};(*f(x+y)*)
cexp = exp /. {y -> c};(*f(y)*)
test1 = pexp - (exp + cexp) // Simplify;(*f(x+y)-(f(x)+f(y))*)
(*Test for Homogeneity: f(a*x) = a*f(x)*)
aexp = exp /. {y -> a*y};(*f(a*x)*)
test2 = a*exp - aexp // Simplify;(*a*f(x)-f(a*x)*)
If[test1 == 0 && test2 == 0,
Print["linear"]]
, {i, 1, Length[split]}]

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}]

Resources