Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solving multivariate equations #218

Closed
DavidDoesDev opened this issue Sep 22, 2014 · 11 comments
Closed

Solving multivariate equations #218

DavidDoesDev opened this issue Sep 22, 2014 · 11 comments

Comments

@DavidDoesDev
Copy link

Hi. This library looks amazing! Thanks for making it. Pardon my simpleness, but I couldn't figure this out with the docs—are you able to solve for multivariate equations in mathjs at this point? Something along these lines?

x = z - y
y = x * 2
z = 3 / x

Solve for x, y, and z

I'm writing a program which may need to solve about 50 interrelated variables. A library that could handle that would be a lifesaver!

@josdejong
Copy link
Owner

Thanks. Yes, solving equations would be awesome. It is ok with you to mark this issue as a duplicate of #38 and continue any discussion there?

@DavidDoesDev
Copy link
Author

Thanks, I'll reply on that ticket.

@DavidDoesDev DavidDoesDev reopened this Sep 22, 2014
@DavidDoesDev
Copy link
Author

(Tried to mark as duplicate. I think you need to be a project owner)

@Itangalo
Copy link

DavidDoesDev: Just to make sure – this request is not restricted to solving systems of linear equations?
The example is close to a set of linear equations, but the last equation turns it from something simple to something rather hairy.

(Sorry for continuing the discussion on this closed thread – this question didn't seem appropriate in issue #38.)

@DavidDoesDev
Copy link
Author

(No worries.) My math skills are pretty rudimentary, so please ask me a different way if I don't totally answer your question. You're asking if exponential and other equation types of equations should be included? Ideally, for sure. Solving for multivariate linear equations alone would still be useful, though. But, I think at some point you're gonna wish you had the full functionality. My main use case for this feature is in balancing chemical formulas, which often go into that hairy territory. I'm sure there are other use cases too. I like this guy's implementation a lot. See this for a little more depth on the feature I'm talking about: http://www.aaron-powell.com/posts/2013-01-22-hello-mathy.html

@Itangalo
Copy link

DavidDoesDev: Sorry for using geeky lingo in my question. What I really wanted to ask is this.
Can your equations always be re-written on the form

3x – 2y + 1.9z + … + 6w = 22
5x + 0y – 0.1z + … + 2w = -1

…and so on? (Exactly one constant coefficient for each variable, and only sums. The number of equations must also match the number of unknowns.) Your example is close to this, but the last equation has a 3/x term which spoils things.

If this is the case, there is a straight-forward way of solving the equations by using matrices (which mathsj already handles). Let me know if this would be useful, and I'll summarize the method.

@DavidDoesDev
Copy link
Author

Thanks, yeah, I think that would handle my immediate needs.

@Itangalo
Copy link

Hi

Sorry for the late reply. I started writing up something about how to solve systems of equations using matrices, but then I realized that others have done this in a much better way already.

Here's a page that covers it quite nicely:
http://www.mathsisfun.com/algebra/systems-linear-equations-matrices.html

I have a weak spot for Khan Academy, and here's a video where Salman Khan (eventually) solves systems of equations using matrices:
https://www.youtube.com/watch?v=AUqeb9Z3y3k

The syntax for using matrices in mathjs is
A = [[124,3,1,4],[0,40,0,8],[1,0,13,0],[2,13,25,95]] <-- variable coefficients
B = [1, 2, 3, 4] <-- right side constants
C = inv(A)*B <-- gives solution to the equations

I hope that helps!

@DavidDoesDev
Copy link
Author

Amazing! I think that would absolutely work! My equations are slightly more complicated, though. How would you approach something like this? I think this is still the same coefficient situation as your example, but there are 2 important gotchas:

// There is sometimes an implicit ratio between some of the coefficients.
// In the image example way at the bottom, we already know 12.5 and 100.
x1 + x2 + x3 + x4 + x5 + 12.5 = 100

// Similarly in that example, there is a desired ratio between the 2 parts of the sugar mix.
9 SUCROSE : 1 HFCS

This is a pretty typical example of what my application would need to solve. There might be more ingredients or groupings, but the structure of the equation will be like this pretty much always. You wouldn't know of a super helpful video for this (for dummies), would you? Infinite thanks!

alt text

@josdejong
Copy link
Owner

I guess you could rewrite x1 + x2 + x3 + x4 + x5 + 12.5 = 100 as x1 + x2 + x3 + x4 + x5 = 100 - 12.5, simplified as x1 + x2 + x3 + x4 + x5 = 87.5.

If you have two variables which have a static ratio (like 1 HFCS = 9 SUCROSE), you can replace one with the other, reducing the total number of variables in the equation with one. After you have solved the equation, you can calculate the other variable from the calculated one by multiplying with the ratio.

@DavidDoesDev
Copy link
Author

Many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants