Tags:
create new tag
, view all tags
-- SamPreston - 07 Nov 2007

Maple Tips

Solving systems of equations

As an example, the problem I've been working with is to solve a general solution for catmull-rom splines where the domain isn't [0,1]. Given four data points,
{p0, p1, p2, p3}
we want to find a polynomial:
f(t) = c0 + c1*t + c2*t^2 + c3*t^3
such that f(t) interpolates point p1 at time t1 with slope (p1-p0)/(t1-t0) (slope pointing from p0 to p1) and point p2 at t2 with slope (p3-p2)/(t3-t2). Therefore we have:
f(t1) = p1
f(t2) = p2
f'(t1) = (p1-p0)/(t1-t0)
f'(t2) = (p3-p2)/(t3-t2)
Or,
p1 = c0 + c1*t1 + c2*t1^2 + c3*t1^3
p2 = c0 + c1*t2 + c2*t2^2 + c3*t2^3
(p1-p0)/(t1-t0) = c1 + 2*c2*t1 + 3*c3*t1^2
(p3-p2)/(t3-t2) = c1 + 2*c2*t2 + 3*c3*t2^2
Now, we can use MAPLE to solve this system of equations for the coefficients using the 'solve' command:
sol:=solve({\
p1=c0+c1*t1+c2*t1^2+c3*t1^3,\
p2=c0+c1*t2+c2*t2^2+c3*t2^3,\
(p1-p0)/s=c1+2*c2*t1+3*c3*t1^2,\
(p3-p2)/s=c1+2*c2*t2+3*c3*t2^2\
},{c0,c1,c2,c3});
where I have simplified a little by using
s = t1-t0 = t2-t1
which gets rid of the t0 and t3 terms, since for my purposes I'll always have evenly-spaced points.

This will give a hard-to-read system of equations, assigned to the variable 'sol'. To look at an individual equation, try for example:

sol[1];
to see the solution to c0. Sometimes solving the equations rearranges the order, so you may have the solution of, say, c2 in sol[1]. In this case you can do something like:
eval(c0,sol);
to get the solution to c0.

Now, to make this equation easier to handle, for each of the coefficient equations c0..c3 we can collect the terms that multiply each of the points p0..p3 so that we have something of the form:

c0 = x00*p0 + x01*p1 + x02*p2 +x03*p3
c1 = x10*p0 + x11*p1 + x12*p2 +x13*p3
c2 = x20*p0 + x21*p1 + x22*p2 +x23*p3
c3 = x30*p0 + x31*p1 + x32*p2 +x33*p3
where all the x?? terms are constants based on t0, t1, t2, and t3 (and s, given our simplification)

To do this, we use the 'collect' command:

collect(sol[1], {p0,p1,p2,p3});
This would give us the the c0 equation above (but with all the x0?'s expanded).

We can test to see that this is the correct equation by substituting in the standard values of t0, t1, t2, t3 for catmull-rom splines:

t0 = -1
t1= 0
t2 = 1
t3 = 2
or for our equations:
t1=0
t2=1
s = 1
with the following command:
subs({t1=0,t2=1,s=1},sol);
Which yeilds the equations:
c0 = p1
c1 = p1 - p0
c2 = 2p0 - 5p1 + 4p2 - p3
c3 = -p0 + 3p1 - 3p2 + p3
Which a quick google search for catmull-rom splines will quickly show is the correct solution (see here for example)

Except wait! We're off by a factor of two! See here for and explanation of tau, the tension parameter.

Actually, this equation is still pretty complicated, and there is another simplification I can make -- I don't really care what the values of t are, just the spacing. So, I can move t1 to be zero, in which case for spacing s:

t0 = -s
t1 = 0
t2 = s
t3 = 2s
In our equation, since we already defined t0 and t3 in terms of s, we just have:
subs({t1=0,t2=s},sol);
or to make it easier to read:
simplify(subs({t1=0,t2=s},sol));

Matrices

To use matrices, you'll need to import the linalg package:
with(linalg);
You can then create a matrix by:
A:=matrix(4,3,[1,2,3,4,5,6,7,8,9,10,11,12]);
The multiply, transpose, etc. functions can be used to perform operations on the matrix.

To generate a system of equations from the matrix, use:

geneqns(A,[x1,x2,x3],[b1,b2,b3])
You can also use genmatrix to generate a matrix from a set of equations.

payday loans

secured loans

debt advice

link building

pension transfer

life insurance quote

Topic revision: r4 - 2009-11-06 - DerekHosewood
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2012 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback