Wikipedia:Reference desk/Archives/Mathematics/2008 October 12

= October 12 =

Multiplying matrices of functions/arrays (in MATLAB)
I am looking for a good way to multiply matrices of functions, or matrices of arrays in Matlab. The description of my problem is as follows:

(In the following, uppercase letters denote matrices and lowercase letters denote scalars.)

I want to find Z = A(x)*B(x) where,

A(x) = [a11(x), a12(x), ..... , a1n(x); a21(x), a22(x), ....., a2n(x); ..............................;        an1(x), an2(x), ....., ann(x)];

B(x) = [b11(x), b12(x), ..... , b1n(x); b21(x), b22(x), ....., b2n(x); ..............................;        bn1(x), bn2(x), ....., bnn(x)];

Here, (.)(x) means (.) is a function of x.

For example, the first element of Z would be

z11(x) = a11(x)*b11(x) + a12(x)*b21(x) + ... + a1n(x)*bn1(x)

Furthermore, I need to integrate Z(x) from x = a to b to get ZI, which would be a simple nxn matrix.

The functions aij(x) and bij(x) are known numerically, so each one of them is a 1D array (say, of size m).

As an example, consider 2x2 matrices

A = [a11(x), a12(x); a21(x), a22(x);]

B = [b11(x), b12(x); b21(x), b22(x);]

Where,

x = [1,2,3,4,5];

a11(x) = x + 1 = [2    3     4     5     6]

a12(x) = x.^2 = [ 1     4     9    16    25]

a21(x) = x - 1 = [  0     1     2     3     4]

a22(x) = 1.5*x = [ 1.5000   3.0000    4.5000    6.0000    7.5000]

b11(x) = sin(x) = [ 0.8415   0.9093    0.1411   -0.7568   -0.9589]

b12(x) = cos(x) = [ 0.5403   -0.4161   -0.9900   -0.6536    0.2837]

b21(x) = exp(-x) = [0.3679   0.1353    0.0498    0.0183    0.0067]

b22(x) = [1 1 1 1 1]

Then

Z = A*B = [a11(x)*b11(x) + a12(x)*b21(x), a11(x)*b12(x) + a12(x)*b22(x); a21(x)*b11(x) + a22(x)*b21(x), a21(x)*b12(x) + a22(x)*b22(x)]

That is, the terms of Z are the functions of x, such that for x = [1,2,3,4,5]

z11 = a11.*b11 + a12.*b21 = [2.0508    3.2692    1.0126   -3.4910   -5.5851]

z12 = a11.*b12 + a12.*b22 = [ 2.0806    2.7516    5.0400   12.7318   26.7020]

z21 = a21.*b11 + a22.*b21 = [ 0.5518   1.3153    0.5063   -2.1605   -3.7852]

z22 = a21.*b12 + a22.*b22 = [1.5000   2.5839    2.5200    4.0391    8.6346]

and integral of Z over x = 1 to 5 will be approximately (using trapezoidal rule)

ZI = [-0.9763, 34.9147; -1.9556, 14.2103]

I am looking for a simple way to do this in the general case. It must be possible to do it somehow using a matrix with three indices, but so far I have not been able to come up with a way.

Any help will be *greatly* appreciated. deeptrivia (talk) 02:53, 12 October 2008 (UTC)


 * Whether this is simple or not will depend on how your functions are defined, but it certainly possible to store values in three index arrays. The drawback is that they don't have natural arithmetic the way that two index matrices do.


 * For example on could implment it as:

A(1,1,:) = a11; B(1,1,:) = b11; A(2,1,:) = a21; B(2,1,:) = b21; etc...

for k = 1:n Z(:,:,k) = A(:,:,k)*B(:,:,k); end Z = sum(Z*dx,3);


 * You'd still have to manually define the lists a_kj and b_kj, but you are going to have to do that anyway in telling Matlab what the functions are for each index. The "3" in the final sum tells Matlab to sum over the third index after multiplying by the increment dx and results in a n x n matrix of the integral you are looking for.  You can of course do more complicated approximations to the integral, but generally that will require defining the integration process inside a for loop.  Dragons flight (talk) 04:04, 12 October 2008 (UTC)


 * Thanks Dragon's flight. I have to define complicated expressions, and I would like to use operators like "*" to do multiplication. Is there any operator overloading in Matlab? I just figured out a cumbersome way to do transposes for these kind of "3d" matrices, but I'd like to overload that method onto the " ' " operator. And, I want to overload the method you suggested to the "*" operator. Is that possible? Thanks a ton! deeptrivia (talk) 05:11, 12 October 2008 (UTC)


 * Recent versions of Matlab do allow you define data classes with overloaded operators, though this is not an area I have much experience with. Each operation in Matlab has a builtin function name associated with it.  For example "A + B" is translated by the interpreter to "plus(A,B)", and within a class you can define a new "plus" function that will then become the "+" operation for that class.  This provides an introduction to class programming on Matlab, though it is somewhat cumbersome.  Unless you will be working with these things alot I'd think it would be simpler to write functions to deal with your specific cases rather than writing a data class and overloaded operators to deal with general cases.  Dragons flight (talk) 05:53, 12 October 2008 (UTC)


 * Thanks, Dragons flight. What if I just modified the default mtimes.m to check if the operands are 3D arrays. If so, use my routine, and if not, do the normal thing. mtimes.m is the following

function [varargout] = mtimes(varargin) if nargout == 0 builtin('mtimes', varargin{:}); else [varargout{1:nargout}] = builtin('mtimes', varargin{:}); end

How can I suitably modify this? Thanks, deeptrivia (talk) 16:49, 12 October 2008 (UTC)

Proving inequality for derivates (defined in Royden's Real Analysis)
Hello, I am working on a homework problem and I am not necessarily even looking for a hint but it seems to be wrong and I just want to see what others think. It's number 3 from Chapter 5 of Royden's book, 3rd edition. It says If $$f\,$$ is continuous on [a, b] and assumes a local maximum at $$c \in (a, b)$$, then $$D_- f(c) \leq D^- f(c) \leq 0 \leq D_+ f(c) \leq D^+ f(c)$$.

Any way, the outer two inequalities are immediate, so the inner two are the problem. In case you do not know what a "derivate" is, because it does not seem to be a very common term, the definitions for the two needed ones are

$$D_+ f(x) = \liminf_{h \rightarrow 0+} \frac{f(x + h) - f(x)}{h}$$

and

$$D^- f(x) = \limsup_{h \rightarrow 0+} \frac{f(x) - f(x - h)}{h}$$

So, my problem is this. If there is a local maximum at $$c\,$$, then $$f(x) \leq f(c)$$ for all $$x \in [a, b] \cap N_\delta(c)$$, for some $$\delta > 0\,$$. Then, $$f(c + h) - f(c)\,$$ should be negative for $$h\,$$ close enough to $$c\,$$ so that $$D_+ f(c)\,$$ should be nonpositive, whereas we are asked to prove it is nonnegative. Similarly, $$f(c) - f(c - h)\,$$ should be positive so that $$D^- f(c)\,$$ should be nonnegative, whereas we are asked to prove it is nonpositive.

Am I completely missing something here? I'm not looking for a solution. I just want to understand this part. Thanks StatisticsMan (talk) 03:04, 12 October 2008 (UTC)


 * You look right; it seems that either the definition provided or the homework problem have a sign flipped somewhere.  Eric.  131.215.159.187 (talk) 03:43, 12 October 2008 (UTC)

Differentiation problem
Find the coordinates of the stationary points on the graph $$y = (x-2)^3 - 12(x-2)$$.

So I differentiated to get $$\frac{dy}{dx} = 3(x-2)^2 - 12$$ And then found the x coordinates to be 0 and 4 and therefore the coordinates are (4, -16) and (0, 16)

However then I have to give the points of intersection with the axes which I dont know how to do. I know the intersection with the y axis from above is (0,16) but how do i get the x intersections.

This is what i tried: $$0 = (x-2)^3 - 12(x-2)$$

Multiplied out the using the binomial theorem to get: $$0 = x^3 - 6x^2 + 16$$

Is this correct? If so what now? --RMFan1 (talk) 12:55, 12 October 2008 (UTC)


 * Where a graph intersects the x-axis is where the function equals zero, so your method is right. It's equivalent to saying "find the roots of the polynomial". --Tango (talk) 13:26, 12 October 2008 (UTC)

Yes I know how you find intersections i just dont know how i can simplify the above. I could also have $$0 = (x-2)^3 - 12(x-2)$$ so that $$0 = (x-2)^2 - 12$$ but when I try to solve with the quadratic formula, the discriminant is negative so I cant simplify it. --RMFan1 (talk) 13:34, 12 October 2008 (UTC)
 * If the discriminant is negative then there are no real solutions, so it doesn't intersect the x-axis. That means x=2 is your only intersection point. --Tango (talk) 14:07, 12 October 2008 (UTC)


 * First think visually. Sketch the graph. You already know two points on it, (0,16) and (4,-16). When x is large and negative then y is also negative, so the graph comes up from the bottom left. It must cross the x axis at least once in the range x<0 to get up to (0,16). Then it must cross the x axis at least once more in the range 04. But a cubic equation can have at most three real roots - so we have one root less than 0, a second root between 0 and 4, and a third root greater than 4.
 * Now suppose we recast the equation in terms of t=x&minus;2. As a function of t, we have
 * $$y=t^3-12t$$
 * The (t,y) graph looks just like the (x,y) graph except it is shifted to the left by 2 units - so in the (t,y) graph the maximum and minimum are at (-2,16) and (2,16). Finding the three real roots of
 * $$y=t^3-12t$$
 * is quite easy - one root is obviously t=0. To find the correspoding values of x, just use x=t+2. Gandalf61 (talk) 14:12, 12 October 2008 (UTC)

Linear algebra puzzler
Let A be a 3&times;2 matrix and B a 2&times;3 matrix. Suppose that
 * $$AB = \begin{bmatrix}5&-1&2\\-1&5&2\\2&2&2\end{bmatrix}.$$

Find BA. Your solution should also show that there is a unique right answer. siℓℓy rabbit (  talk  ) 15:15, 12 October 2008 (UTC)


 * My first thought would be to consider transposes. --Tango (talk) 15:24, 12 October 2008 (UTC)
 * Not that that seems to work... I can't get anywhere with it (but I may be missing something, the question still screams "take transposes" to me). You could always try it the hard way - write A=(aij), B=(bij), multiply out both ways round and try and solve the horrible mess of simultaneous quadratic equations - I wouldn't recommend it, though! --Tango (talk) 15:38, 12 October 2008 (UTC)
 * I'm pretty sure the uniqueness of $$BA=6I$$ can be proved by considering all possible singular value decompositions of $$AB$$. $$A$$ and $$B$$. 84.239.160.166 (talk) 16:29, 12 October 2008 (UTC)


 * On a quick look I'd say it was probably 6 times the 2x2 identity matrix since multiplying the matrix by itself gives itself multiplied by 6. Just did that because I got BA in the middle of AB.AB. Dmcq (talk) 16:52, 12 October 2008 (UTC)

I suppose I should give the answer, although I was expecting this to generate somewhat more interest.

Method 1. Note that AB2=6AB. That is ABAB=6AB. Since AB is of rank 2, both A and B are also rank 2, and so A:R2&rarr;R3 is injective and B:R3&rarr;R2 is surjective. Thus A is cancellable on the left and B is cancellable on the right of ABAB=6AB, and so BA=6I.

Method 2. From ABAB=6AB, we have
 * $$A^TABABB^T = 6 A^TABB^T.$$

Since rank(ATA) = rank(A) = 2 and rank(BBT) = rank(B) = 2, we have
 * $$BA = 6 (A^TA)^{-1}A^TABB^T(BB^T)^{-1} = 6I.$$

Remark. I seem to recall that there was a way to solve this problem using orthogonality somehow (e.g., the SVD or QR decomposition). I was unable to recall the details for the purposes of this solution, but feel free to add a "Method 3" if you can get this to work. Cheers, siℓℓy rabbit  (  talk  ) 14:36, 13 October 2008 (UTC)


 * Method 3. AB is a real symmetric matrix with eigenvalues (6,6,0).  It therefore has the eigendecomposition
 * AB = P . 6I . PT
 * (equivalent for such matrices to singular value decomposition), where I is the 2x2 identity matrix, P is the 3x2 orthonormal matrix made from its non-nullspace eigenvectors, and PT is the transpose of this matrix.
 * AB must therefore be made up of factors
 * AB = (P . M) . (M-1 . 6I . PT)
 * where M can be any invertible 2x2 matrix.
 * Therefore,
 * BA = (M-1 . 6I . PT) . (P . M)
 * = M-1 . 6I . M
 * = 6I . M-1 . M
 * = 6I


 * Jheald (talk) 11:20, 14 October 2008 (UTC)

cost of transporting goods
Con you tell be the cost to transport 100 lbs of material in a car that gets 25 mpg / gas @ $3.50 gal. My husband feels that the 'junk' I was to take is worth less than the gas. We will travel 1000 miles. Many ThanksTwinkle2toes (talk) 21:48, 12 October 2008 (UTC)


 * Can't be done with mathematics only. If I were to assume that fuel consumtion is proportional to the loaded mass of the vehicle, and the vehicle weighs around 2,000 lbs, then you'd end up with around $7 additional cost. But I'm pretty sure it's not proportional, and I wouldn't be surprised if the physics behind it is complicated. Fuel economy in automobiles only says that engine efficiency varies with the mass of the automobile and its load, but not how. We need input from someone who knows something about cars... -- Jao (talk) 22:51, 12 October 2008 (UTC)
 * Not just cars, but that specific car. It's going to vary widely from car to car, for example a lightweight car is going to affected by an extra 100lbs more than a very heavy car (I would imagine, anyway). --Tango (talk) 23:03, 12 October 2008 (UTC)


 * The cost to transport "100 pounds of material" is like talking about the cost to bring along a small teenager or a box or two of treasured family photos and such . The answer is more subjective than practical remembering that "one person's junk is another's treasure". Neither the actual dollar cost of transporting nor the emotional cost (even though you didn't ask) of leaving the "material" behind can't be calculated with the information given.  -hydnjo talk 23:51, 12 October 2008 (UTC)


 * Fuel efficiency would be more or less proportional to the mass assuming things are ideal (like the engine is a carnot engine). This makes some sense because both kinetic and potential energy are proportional to mass, and so is any frictional loss during braking, etc. So, Jao's estimate is the best we can do unless we decide to go really fancy. 128.118.150.91 (talk) 00:20, 13 October 2008 (UTC)


 * Yeah, $5 to $10 seems about right. -hydnjo talk 00:31, 13 October 2008 (UTC)


 * The fuel economy depends on whether the extra 100 hundred pounds is being driven in town or on the highway. Once you're at a steady highway speed (the 1000-mile trip), air drag dominates and an 100 extra pounds isn't going to matter much.  Take the stuff out for around-town driving, though.  Saintrain (talk) 12:55, 13 October 2008 (UTC)


 * $5 sounds about right. Let's say you get a 2% drop in gas mileage, down from 25 mpg to 24.5 and that means instead of using 40 gallons of fuel, you will use instead 41 gallons of fuel. Sentriclecub (talk) 15:20, 13 October 2008 (UTC)


 * The cost will be $3.5/gallon * 1000 miles / 25 mpg = $140. Assumptions: You wouldn't do the travel if not fetching the stuff, consumption is for a loaded car (difference will probably be negligible anyway...) TERdON (talk) 16:03, 15 October 2008 (UTC)