I have two equations:
(x-6)^2 + (y-2)^2 = 6^2
(x-2)^2 + (y-2)^2 = 3^2
I subtracted the 2nd equation from the 1st and got the answer. However, I want to find x and y from the set of equations using any programming language. Can anybody help to get the source code for this?
If you want to solve mathematical equations symbolically like this, there are many programming languages and softwares available:
Julia (free)
Sympy (python library - free)
Wolfram Mathematica (paid)
Maple (paid)
(Wolfram Alpha which is just a solver online. (freemium))
Here are some "source code" as you requested:
sympy:
import sympy as sp
x=sp.Symbol('x')
y=sp.Symbol('y')
print(sp.solve([(x-6)**2+(y-2)**2-6**2, (x-2)**2+(y-2)**2-3**2], [x, y]))
Mathematica:
FullSimplify[Solve[{(x - 6)^2 + (y - 2)^2 == 6^2, (x - 2)^2 + (y - 2)^2 == 3^2}, {x, y}]]
Mathematica also has it's own SE site here
Just for the sake of examples: here is a wolfram alpha link
Related
The Octave interpreter evaluates this expression as false:
>> 2/3 + 1/6 == 5/6
ans = 0
cause
>> 2/3 + 1/6 - 5/6
ans = -1.11022302462516e-16
This can be avoided with the rat (or rats) function, or casting the values, but the resulting expression lacks the clear formatting of the initial one:
>> all(rat(2/3 + 1/6) == rat(5/6))
ans = 1
>> single(2/3 + 1/6) == single(5/6)
ans = 1
When using Octave to teach kids arithmetic, 'dirty' translations of mathematical expressions are of no use.
Is there any global adjustment that could be done to evaluate as true the original expression?
Julia has a rational numbers type and is free. You don't need to use Octave symbolics. You can use a Jupyter notebook. Note that notebooks make great teaching tools. My professor used these to teach. Some examples are here.
2//3+1//6
5//6
Using Mathematica I need to evaluate the integral of a function. Since it is taking the program too much to compute it, would it be possible to use parallel computation to shorten the time needed? If so, how can I do it?
I uploaded a picture of the integrand function:
I need to integrate it with respect to (x3, y3, x, y) all of them ranging in a certain interval (x3 and y3 from 0 to 1) (x and y from 0 to 100). The parameters (a,b,c...,o) are preventing the NIntegrate function to work. Any suggestions?
If you evaluate this
expr=E^((-(x-y)^4-(x3-y3)^4)/10^4)*
(f x+e x^2+(m+n x)x3-f y-e y^2-(m+n y)y3)*
((378(x-y)^2(f x+e x^2+(m+n x)x3-f y-e y^2-(m+n y)y3))/
(Pi(1/40+Sqrt[((x-y)^2+(x3-y3)^2)^3]))+
(378(x-y)(x3-y3)(h x+g x^2+(o+p x)x3-h y-g y^2-(o+p y)y3))/
(Pi(1/40+Sqrt[((x-y)^2+(x3-y3)^2)^3])))+
(h x+g x^2+(o+p x)x3-h y-g y^2-(o +p y) y3)*
((378(x-y)(x3-y3)(f x+e x^2+(m+n x)x3-f y-e y^2-(m+n y)y3))/
(Pi(1/40+Sqrt[((x-y)^2+(x3-y3)^2)^3]))+
(378 (x3 - y3)^2 (h x + g x^2 + (o + p x)x3-h y-g y^2-(o+p y)y3))/
(Pi(1/40+Sqrt[((x-y)^2+(x3-y3)^2)^3])));
list=List ## Expand[expr]
then you will get a list of 484 expressions, each very similar in form to this
(378*f*h*x^3*x3)/(Pi*(1/40+Sqrt[(x^2+x3^2-2*x*y+y^2-2*x3*y3+y3^2)^3]))
Notice that you can then use NIntegrate in this way
f*h*NIntegrate[(378*x^3*x3)/(Pi*(1/40+Sqrt[(x^2+x3^2-2*x*y+y^2-2*x3*y3+y3^2)^3])),
{x,0,100},{y,0,100},{x3,0,1},{y3,0,1}]
but it gives warnings and errors about the convergence and accuracy, almost certainly due to your fractional powers in the denominator.
If you can find a way to pull out the scalar multipliers which are independent of x,y,x3,y3 and then perform that integration without warnings and errors and get an accurate result which isn't infinity then you could perhaps perform these integrals in parallel and total the results.
Some of the integrands are scalar multiples of others and if you combine similar integrands then you can reduce this down to 300 unique integrands.
I doubt this is going to lead to an acceptable solution for you.
Please check all this very carefully to make certain that no mistakes have been made.
EDIT
Since the variables that are independent of the integration appear to be easily separated from the dependent variables in the problem posed above, I think this will allow parallel NIntegrate
independentvars[z_] := (z/(z//.{e->1, f->1, g->1, h->1, m->1, n->1, o->1, p->1}))*
NIntegrate[(z//.{e->1, f->1, g->1, h->1, m->1, n->1, o->1, p->1}),
{x, 0, 100}, {y, 0, 100}, {x3, 0, 1}, {y3, 0, 1}]
Total[ParallelMap[independentvars, list]]
As I mentioned previously, the fractional powers in the denominator result in a flood of warnings and errors about convergence failing.
You can test this with the following much simpler example
expr = f x + f g x3 + o^2 x x3;
list = List ## Expand[expr];
Total[ParallelMap[independentvars, list]]
which instantly returns
500000. f + 5000. f g + 250000. o^2
This is a very primitive method of pulling independent symbolic variables outside an NIntegrate. This gives absolutely no warning if one of the integrands is not in a form where this primitive attempt at extraction is not appropriate or fails.
There may be a far better method that someone else has written out there somewhere. If someone could show a far better method of doing this then I would appreciate it.
It might be nice if Wolfram would consider incorporating something like this into NIntegrate itself.
So, I have a vector that corresponds to a given feature (same dimensionality). Is there a package in Julia that would provide a mathematical function that fits these data points, in relation to the original feature? In other words, I have x and y (both vectors) and need to find a decent mapping between the two, even if it's a highly complex one. The output of this process should be a symbolic formula that connects x and y, e.g. (:x)^3 + log(:x) - 4.2454. It's fine if it's just a polynomial approximation.
I imagine this is a walk in the park if you employ Genetic Programming, but I'd rather opt for a simpler (and faster) approach, if it's available. Thanks
Turns out the Polynomials.jl package includes the function polyfit which does Lagrange interpolation. A usage example would go:
using Polynomials # install with Pkg.add("Polynomials")
x = [1,2,3] # demo x
y = [10,12,4] # demo y
polyfit(x,y)
The last line returns:
Poly(-2.0 + 17.0x - 5.0x^2)`
which evaluates to the correct values.
The polyfit function accepts a maximal degree for the output polynomial, but defaults to using the length of the input vectors x and y minus 1. This is the same degree as the polynomial from the Lagrange formula, and since polynomials of such degree agree on the inputs only if they are identical (this is a basic theorem) - it can be certain this is the same Lagrange polynomial and in fact the only one of such a degree to have this property.
Thanks to the developers of Polynomial.jl for leaving me just to google my way to an Answer.
Take a look to MARS regression. Multi adaptive regression splines.
Basically I just want to solve k. Note that the equation equals to 1.12
import math
from sympy import *
a = 1.45
b = 4.1
c = 14.0
al = math.log(a, 2)
bl = math.log(b, 2)
cl = math.log(c, 2)
k = symbols('k')
print solve(Eq(1/k**al + 1/k**bl + 1/k**cl, 1.12), k)
This raises OverflowError: Python int too large to convert to C long
Solution using other libraries welcomed too.
Since you are using numerical values, I am assuming that you are looking for a numerical solution. In that case, you should not use solve, because it tries to find a symbolic solution. The issue here is that it converts these floating point exponents into rational exponents, which have very large numerators and denominators, and it then at some point tries to make polynomials of degree corresponding to those large numbers, which is where it fails.
To solve numerically, you can use nsolve.
>>> print nsolve(Eq(1/k**al + 1/k**bl + 1/k**cl, 1.12), 2)
1.82427203413783
It's better to use numeric libraries like SciPy if you are interested in numeric solutions, though. You can use lambdify to convert your SymPy expressions into functions more suited for libraries like SciPy that use NumPy arrays.
It is a known issue.
You may try
solve(Eq(1/k**al + 1/k**bl + 1/k**cl, 1.12), k, rational=False)
I have to built dynamically equations like following:
x + x/3 + (x/3)/4 + (x/3/4)/2 = 50
Now I would like to evaluate this equation and get x. The equation is built dynamically. x is the leaf node in a taxonomy, the other 3 nodes are the super concepts. The divisor represents the number of children of the child nodes.
Is there a library that allows to build such equations dynamically and resolve x?
Thanks, Chris
Are your equations always of this form (linear in x)?
If so, when building the equation, just set x to 1 and evaluate the lhs.
This will give you lhs = 1 + 1/3 + (1/3)/4 + (1/3/4)/2 = 1.4583..
Then calculate x = rhs / lhs = 50 / 1.4583
It might help you to do some algebra on it.
Note that:
x= 3*x/3 = (x*4*3*2)/(4*3*2)
x+x/3 = 3x/3 + x/3 = 4x/3
and in your particular case:
x + x/3 + (x/3)/4 + (x/3/4)/2 = (x*4*3*2)/(4*3*2) + (x*4*2)/(4*3*2) + (x*2)/(4*3*2) + (x)/(4*3*2)
= (4*3*2x + 4*2x + 2*x + x)/(4*3*2)
Perhaps if you can find a way to have the left hand side rewritten as a single big fraction like this, the solution will come much easier.
Also, factor out the x
(4*3*2x + 4*2x + 2*x + x)/(4*3*2) = x*(4*3*2 + 4*2 + 2 + 1)/(4*3*2)
Then solve for x
50= x*(a/b)
50*(b/a) = x
Since you have some code generating the polynomial, you should be able to generate this big (a/b) fraction thing pretty easily too. I purposely did not simplify the multiplications so that it is clear where each component comes from.
If you're planning to use Java, you can try JAS. It claims to be able to solve polynomials equations.
FTA:
The Java Algebra System
(JAS) is an object oriented, type safe
and multi-threaded approach to
computer algebra. JAS provides a well
designed software library using
generic types for algebraic
computations implemented in the Java
programming language. The library can
be used as any other Java software
package or it can be used
interactively or interpreted through
an jython (Java Python) front end. The
focus of JAS is at the moment on
commutative and solvable polynomials,
Groebner bases and applications. By
the use of Java as implementation
language JAS is 64-bit and multi-core
cpu ready.