[RESOLVED] Solving the equation with three unknowns
Hi friends,
I want to ask a question about a programme will solve the equation with three unknowns.
I will make a programme will solve these equations. Now, I need to know, how I can make this. I need the idea of making this programme.
What do you think?
Thanks..
Re: Solving the equation with three unknowns
Please show us a specific example of what you need.
Re: Solving the equation with three unknowns
For example,
a+b+c = 7
3a+5b+9c = 29
2a+9b+7c =32
what are the values of a, b and c?
The programme will solve this
Re: Solving the equation with three unknowns
OK, so you are talking about a straightforward system of 3 linear equations and 3 unknowns. Do you know how to solve those values by hand? If not, I recommend reading this handy wikipedia page. In particular, pay attention to the elimination method, which uses Gaussian Elimination.
Thus, if you place the coefficients of a, b, and c in a matrix (aka two dimensional array), and the solution set in a one-dimensional array, you can perform operations to put the matrix in echelon form. Once that is done, your array will contain the correct values of a, b, and c.
If you're not sure the proper technique for getting a matrix into echelon form given your language, you can refer to this pseudocode, and then if you still need help, you can post specific questions in the forum for whichever language you are programming in.
Re: Solving the equation with three unknowns
It's worth mentioning that many, many languages have linear algebra libraries that should be used for this type of work rather than reinventing the wheel by implementing your own methods--though if this is an exercise in coding rather than "real" work reinventing the wheel is fine.
On the off chance your problem is restricted to precisely 3 variables and 3 equations, you can actually hard-code the solutions using, for instance, Cramer's Rule, which gives the magical formulas
Code:
ax + by + cz = j
dx + ey + fz = k
gx + hy + iz = l
implies
Code:
M = a*e*i + b*f*g + c*d*h - c*e*g - b*d*i - a*f*h
x = (j*e*i + b*f*l + c*k*h - c*e*l - b*k*i - j*f*h) / M
y = (a*k*i + j*f*g + c*d*l - c*k*g - j*d*i - a*f*l) / M
z = (a*e*l + b*k*g + j*d*h - j*e*g - b*d*l - a*k*h) / M
where if M is zero, the system has zero or infinitely many solutions. (For instance, take a=b=...=h=i=0 and j=k=l=0 or j=k=l=1.)
In the case you gave, this method correctly gives (x, y, z) = (4.25, 2.125, 0.625).
Re: [RESOLVED] Solving the equation with three unknowns
Dang, I forgot about Cramer's rule. Shows you what happens when you become addicted to Mad Money and lose track of the real world. :eek2:
Jemidiah, please forgive me for this shortcoming. I could write code for the solution, once the value of the coefficients are trapped within each equation. That's surprisingly easy.