       Re: Questions on evaluate matrix operations

• To: mathgroup at smc.vnet.net
• Subject: [mg92924] Re: Questions on evaluate matrix operations
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Mon, 20 Oct 2008 07:32:20 -0400 (EDT)
• Organization: The Open University, Milton Keynes, UK
• References: <gdev9n\$3kf\$1@smc.vnet.net>

```rac00n wrote:

> I've given up on another system and I have a Mathematica question for you.
> Please bare with me as this is the first time I'm using Mathematica
> for anything else besides plotting.
>
> Here is my program so far:
>
> n = 1;
> m = 10;
> f[x_] := Sin[x*Pi];
>
> l = 0;
> t = 0;
> a = 1;
> h = 0.1;
> k = 0.01;
> LAMDA = a*a*k/(h*h);
>
> w = {T};
======^^^
Should be lowercase t, I believe.

> count = 1;
> Do[w = Append[w, f[count*h]]; count++, {m - 1}];
> w
>
> A1 = {0};
> count = 0;
> Do[A1 = Append[A1, 1 + 2*LAMDA]; count++, {m}];
> count;
> A1 = Drop[A1, 1];
>
> A2 = {0};
> Do[A2 = Append[A2, -LAMDA], {m - 1}];
> A2 = Drop[A2, 1];
>
> A = MatrixForm[
======^^^^^^^^^^^
This is the crux of your problem. See below.

>    DiagonalMatrix[A2, -1] + DiagonalMatrix[A1] +
>     DiagonalMatrix[A2, 1]];
> Ainv = Inverse[A];
>
> count = 0;
> Do[wN = Evaluate[A.w]; Print["W[", count, "]: ", wN]; w = wN;
>   count++, {n}];
>
> This gives the following output:
>
> the Matrix "A" dot the vector "w"
>
> If I Select the output and choose "Evaluate in Place/Cell" the
> following output is shown:
>
> {-0.30901699437494734`, 0.3392657308323691`, 0.6453217681275247`, \
> 0.8882092145372158`, 1.0441525545105133`, 1.0978869674096932`, \
> 1.0441525545105133`, 0.8882092145372158`, 0.6453217681275247`, \
> 0.33926573083236944`}
>
> How can I force: wN = Evaluate[A.w] to always be fully evaluated? Even
> though I have Evaluate[%] it still shows the full explicit answer. I
> want to flatten/reduce the the result. Everything I've tried so far
> does not work. Any ideas?

Miguel,

Two things to keep in mind:

. Mathematica is /case sensitive/, so t and T are not the same variable

. MatrixForm is a *wrapper* used for display only: if you assign it to a
variable, and so use it within computations, the results might be
surprising.

Below is the fixed code. (Note that to write more efficient code you may
want to use functions such as *Table* and *Join*.)

In:= n = 1;
m = 10;
f[x_] := Sin[x*Pi];

l = 0;
t = 0;
a = 1;
h = 0.1;
k = 0.01;
LAMDA = a*a*k/(h*h);

w = {t};
count = 1;
Do[w = Append[w, f[count*h]]; count++, {m - 1}];
w

A1 = {0};
count = 0;
Do[A1 = Append[A1, 1 + 2*LAMDA]; count++, {m}];
count;
A1 = Drop[A1, 1];

A2 = {0};
Do[A2 = Append[A2, -LAMDA], {m - 1}];
A2 = Drop[A2, 1];

A = DiagonalMatrix[A2, -1] + DiagonalMatrix[A1] +
DiagonalMatrix[A2, 1];
Ainv = Inverse[A];

count = 0;
Do[wN = Evaluate[A.w]; Print["W[", count, "]: ", wN]; w = wN;
count++, {n}];

Out= {0, 0.309017, 0.587785, 0.809017, 0.951057, 1., 0.951057, \
0.809017, 0.587785, 0.309017}

During evaluation of In:= W: \
{-0.309017,0.339266,0.645322,0.888209,1.04415,1.09789,1.04415,0.\
888209,0.645322,0.339266}

Regards,
-- Jean-Marc

```

• Prev by Date: Re: Questions on evaluate matrix operations
• Next by Date: Re: Pi Formula
• Previous by thread: Re: Questions on evaluate matrix operations
• Next by thread: ReplaceAll on Nested Expressions