hi,

i want to solve 3 equations, which has 3 unknowns like this:

a x1^2 + bx1 + c = y1
a x2^2 + bx2 + c = y2
a x3^2 + bx3 + c = y3

can anybody suggest mw how can i do it in C?

i tried to do it with matrices, but couldn't.

Drew.

## All 8 Replies

Since none of the equations depend on each other at all you can solve it as 3 separate quadratic equations so either with a root finder or the brute force (-b+- sqrt(b^2 - 4ac)) /2a business.

as with any programming maths problem first you need to know
roughly what you are trying to by hand.

Another important factor in this instance is why you are doing it. If it is just to find the answer then just put the equation in a function. But if it is to use a particular set of programming tools then it is important to know which ones you have to use to solve the problem.

Are you trying to find a, b, & c
as finding the best fit to three points?

in which case you can either iterate in steps for a, b, & c
to see which values give the smallest error or rearrange
c = y1 - a * ( x1 * x1) - b * x; (1)
b * (x1 - x2) = (y1 - y2) + a * (x2 * x2 - x1 * x1); (2)

(1) ->

c = y1 - (y1 - y2) * x + a( x1* x1 - x1* x2* x2 + x1* x1* x1); (3)
you can use 3 with y1->y3 & x1->x3
like a simple simultaneous equation
to find c then a then b.

If the algebra is too scary even from here, you can iterate over 'c ' and find the values
of 'a' that fit then you can use these values to plot a graph

The matrix methods would be trickiest but may be what you are being asked to do.

hi,
i tried to do it with matrices, but couldn't.

hello tetron,

you gave me thiss equation:

c = y1 - (y1 - y2) * x + a( x1* x1 - x1* x2* x2 + x1* x1* x1); (3)

this equation contains 'a' variable. now i don't know a's value. then how would be i able to get the value of c?

all the equations will have a now.

Drew.

If you don't know any of the values for the parameters you're not going to be able to solve the equations. If you don't have any of the parameter, do you have values for x1,x2,x3 and y1,y2,y3? Please tell us what you have been given.

Assuming that x1, y1, x2, y2, x3, y3

c = y3 - (y3 - y2) * x3 + a(x3^2 - x3*x2^2 + x3^3); (4)

is of the form c = k1 * a + k2; (a)
k1 and k2 are both directly calculable

combined with
c = k3 * a + k4; (b)
therefore
k1 * a + k2 = k3 * a + k4
(k1 - k4) = a * (k3 - k2)
a = (k1 - k4) / (k3 - k2);

from (b)
c = k3 *(k1 - k4)/(k3 - k2) + k4;
where
k1 = y3 - x3 * (y3 - y2);
k2 = (x3^3 - x3 * x2^2 + x1^3)
k3 = y1 - x1 * (y1 - y2);
k4 = (x1^2 - x1 * x2^2 + x1^3);

I have done this rearranging quickly so there is a small
risk of an algerbriac error

The only requirement is that k3 != k2
which is that none of the points are identical to each other

Having said this if this is not just trying to find the quadratic for
drawing a graph a more computer based task is probably wanted.

There are several other options you can use:
1 - a convergence technique
2 - Write code to rearrange simple formulae (like I have done)
3 - Matrix definition of the equation

(a, b, c) * (x1^2; = y1
x1;
1)
Now both 1 & 3 require iteration and convergence
and 3 is tricky
all of the techniques require that you understand how to do the problem for one case

hi,

i want to solve 3 equations, which has 3 unknowns like this:

a x1^2 + bx1 + c = y1
a x2^2 + bx2 + c = y2
a x3^2 + bx3 + c = y3

can anybody suggest mw how can i do it in C?

i tried to do it with matrices, but couldn't.

Drew.

take int x- matrix of the unknowns
A- matrix of the coeffecients
B -matrix of y values

use the following steps
1) x * A = B;
compute the inverse of A then
2) x = B * inverse of A;
multiplication of matrix B with matrix A yields matrix of unknowns

The general way to solve N equations of N unknows of ANY powers (e.g. x^5) is the use Bezout's method.
See:

(Also there is a lot of literature to improve the speed and numerical stability).

The method is highly tractable to code because as matrix is created and used to reduce the equations down one by one and reducing the number of variables one by one.

At the end of the method you are left with one polynomial in one which you solve by your favorite solving method [eg. the gnu scientific library : http://www.gnu.org/software/gsl/

The resultant polynomial can be of a relative high power, e.g three quadratics in three unknowns will result in a 16th order poynominal.

commented: Best suggestion +2

Assuming that x1, y1, x2, y2, x3, y3

c = y3 - (y3 - y2) * x3 + a(x3^2 - x3*x2^2 + x3^3); (4)

is of the form c = k1 * a + k2; (a)
k1 and k2 are both directly calculable

combined with
c = k3 * a + k4; (b)
therefore
k1 * a + k2 = k3 * a + k4
(k1 - k4) = a * (k3 - k2)
a = (k1 - k4) / (k3 - k2);

from (b)
c = k3 *(k1 - k4)/(k3 - k2) + k4;
where
k1 = y3 - x3 * (y3 - y2);
k2 = (x3^3 - x3 * x2^2 + x1^3)
k3 = y1 - x1 * (y1 - y2);
k4 = (x1^2 - x1 * x2^2 + x1^3);

I have done this rearranging quickly so there is a small
risk of an algerbriac error

The only requirement is that k3 != k2
which is that none of the points are identical to each other

Having said this if this is not just trying to find the quadratic for
drawing a graph a more computer based task is probably wanted.

There are several other options you can use:
1 - a convergence technique
2 - Write code to rearrange simple formulae (like I have done)
3 - Matrix definition of the equation

(a, b, c) * (x1^2; = y1
x1;
1)
Now both 1 & 3 require iteration and convergence
and 3 is tricky
all of the techniques require that you understand how to do the problem for one case

the above solution did not generate the right results for me, however I have derived the equations myself using substitution (I used maxima to make sureI made no mistakes)

my points are efined as (xs0,ys0) (xs1,ys1) and (xs2,ys2)

[a=((xs1-xs0)*ys2+(xs0-xs2)*ys1+(xs2-xs1)*ys0)/((xs1-xs0)*xs2^2+(xs0^2-xs1^2)*xs2+xs0*xs1^2-xs0^2*xs1)]

[c=-(xs0*ys1-xs1*ys0-a*xs0*xs1^2+a*xs0^2*xs1)/(xs1-xs0)]

hope this helps as I could not find anywhere online with this solution detailed.

I agree using matrices is the easiest. I was able to solve in scipy in 2mins using XA=Y therefore A=X-1Y. however we don't have a matrix class in our c++ libraries....doh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.