symbolically solving nonlinear equation - solver

I can't find a way to obtain from Maxima the solution of a relatively simple equation. In fact
solve(x^n=a*x^m,x);
returns
[x^n=a*x^m]
whereas I'd like to obtain
x=a^(1/(n-m))
Any clue?

Octave can do it:
> pkg load symbolic
> solve(x^n==a*x^m, x)
ans = (sym)
-1
─────
m - n
a

Related

How does this solution of Project Euler Problem 27 in the Haskell Wiki work?

I have been solving some random Project Euler problems to practice my haskell. After solving the problem, I usually look up the solution on the haskell wiki.
For Problem 27, I solved it the regular way, i.e, using a combination of isPrime and maps. But then, I saw this solution on the wiki. I have no idea how this solution works. The only other mention I find of it is from this closed thread on math stackexchange.
problem_27 :: Integer
problem_27 = -(2 * a - 1) * (a ^ 2 - a + 41)
where
n = 1000
m = head $ filter (\x -> x ^ 2 - x + 41 > n) [1 ..]
a = m - 1
My attempt at understanding this code
As I understand, this code does the following
Let n=1000
Let m be the first natural number x, such that x^2 - x + 41 is greater than n(here 1000)
Then a = m - 1
The answer to the problem is -(2a-1) * (a^2-a+41): (I think that this means that the two are the coefficients.)
But I do not understand why this program gives the correct answer. Or in other words, What is the reasoning behind this algorithm?

Solving a Nonlinear equation with Julia

I am trying to solve a nonlinear equation with Julia,
I have the following nonlinear equation
Nfoc(k,k1,z,n)=(1-α)*exp(z)*(k/n)^α/(exp(z)*(k^α)*(n^(1-α))+k*(1-δ)-k1) - A/(1-n)
and I have a grid of values for k,k1 and z and I am trying to find the values of x that are the roots of this equation for each k,k1, and z, by using this loop:
MatrixN=zeros(nkk,M,nkk)
for i=1:nkk,j=1:M
for i2=1:nkk
MatrixN[i,j,i2]=roots(Nfoc[K[i],K[i2],z(j),n])
end
end
However, its obvious that the command roots its not functioning.
I would deeply appreciate any help in the less techical way possible!
I don't have enough knowledge to work on your use case, but in general, one way to find roots of a parametric function could be:
using FastAnonymous # Creating efficient "anonymous functions" in Julia
using Roots
f(x,k,k1,z,n) = exp(x) - x^4 + k + k1 + z + n
function f_gen(k,k1,z,n)
#anon x -> f(x,k,k1,z,n)
end
fzero(f_gen(0,0,0,0), 1) # => finds x so f(x,0,0,0,0) = 0 using a derivative free method

How to define the variable as a matrix in sage?

I want to define a function which deals with matrices for example..
If I have a characteristic polynomial of a matrix with me and I want to check the cayley hamilton theorem.. What can be done better?
var('x')
f(x)=2x^2+x+3 # this the characteristic polynomial of $A$ (say)
print f(A)# this is what I want as an answer..
In the above if I want to replace my x by a matrix what I have to do?
So, ultimate aim is to find define a polynomial which can take matrix
Thanks in advance...
Amazingly, apparently this hasn't come up very often despite having already been mentioned six years ago, so we haven't fixed it.
sage: M = matrix([[1,2],[3,4]])
sage: g(x) = x^2-5*x-2
sage: g(M)
TypeError: no canonical coercion from Full MatrixSpace of 2 by 2 dense matrices over Integer Ring to Callable function ring with argument x
(Doing at least something about this is Trac 15487.)
However, try using this trick. The problem is only with symbolic expressions, not polynomials.
sage: M = matrix([[1,2],[3,4]])
sage: f = M.charpoly()
sage: f.subs(x=M)
[0 0]
[0 0]
Edit: in general, try something like this.
M = matrix([[1,2],[3,4]])
R.<t> = PolynomialRing(SR)
f = t^2+t+1
f(M)

How can I write an algorithm to solve this formula for closest point to a set of lines in 3d

I am trying to understand how I can write an algorithm to solve the formula written at the end of this answer
I know simple equations systems may be solved through matrices when you have Ax=b you can solve with x = A^(-1)b but this is a little more complicated for me
I think I should come to a form such as A vec(c) = b but I don't have idea how to deal with sums and dot products..
Use the last formula in the linked answer.To simplify it, normalize direction vectors d(i) (to exclude denominator)
Sum[i=1..N] (c - a(i) - d(i) * DotProduct(c-a(i), d(i))) = 0
Sum[i=1..N] (c - a(i) -
d(i) * ((c.x-a(i).x) * d(i).x + (c.y-a(i).y) * d(i).y +(c.z-a(i).z) * d(i).z)) = 0
etc
You have a system of three linear equations. You can solve this system with simple elimination method (which is close to the school approach for solving eq. systems)

How is `(d*a)mod(b)=1` written in Ruby?

How should I write this:
(d*a)mod(b)=1
in order to make it work properly in Ruby? I tried it on Wolfram, but their solution:
(da(b, d))/(dd) = -a/d
doesn't help me. I know a and b. I need to solve (d*a)mod(b)=1 for d in the form d=....
It's not clear what you're asking, and, depending on what you mean, a solution may be impossible.
First off, (da(b, d))/(dd) = -a/d, is not a solution to that equation; rather, it's a misinterpretation of the notation used for partial derivatives. What Wolfram Alpha actually gave you was:
, which is entirely unrelated.
Secondly, if you're trying to solve (d*a)mod(b)=1 for d, you may be out of luck. For any value of a and b, where a and b have a common prime factor, there are an infinite number of values of d that satisfy the equation. If a and b are coprime, you can use the formula given in LutzL's answer.
Additionally, if you're looking to perform symbolic manipulation of equations, Ruby is likely not the proper tool. Consider using a CAS, like Python's SymPy or Wolfram Mathematica.
Finally, if you're just trying to compute (d*a)mod(b), the modulo operator in Ruby is %, so you'd write (d*a)%(b).
You are looking for the modular inverse of a modulo b.
For any two numbers a,b the extended euclidean algorithm
g,u,v = xgcd(a, b)
gives coefficients u,v such that
u*a+v*b = g
and g is the greatest common divisor. You need a,b co-prime, preferably by ensuring that b is a prime number, to get g=1 and then you can set d=u.
xgcd(a,b)
if b = 0
return (a,1,0)
q,r = a divmod b
// a = q*b + r
g,u,v = xgcd(b, r)
// g = u*b + v*r = u*b + v*(a-q*b) = v*a+(u-q*v)*b
return g,v,u - q*v

Resources