Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.2K Views
|
14 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Calculate the Det and the inverse of a Matrix?

Posted 9 years ago

Hi, I can't understand why my code doesn't work. The problem is the matrix J, the code can't calculate the Det and the inverse. The det ll be -0.45cos... but it prints 0.-0.45cos..... can someone help me??

n = Real;

p1[s_, \[Theta]3_] = ( {
    {\[Theta]3},
    {s}
   } );
p1[n] = ( {
    {\[Theta]3},
    {s}
   } );
\[CapitalDelta]\[Theta] = 10*Pi/180;
l2 = 0.20;
l3 = 0.45;
\[Theta]1 = \[Pi];
n = 0;
\[Theta]2 = Pi/6;
\[Epsilon] = 0.00001;
\[Theta]2max = 2 Pi;
f1[p1[\[Theta]3_, s_]] = 
 s*Cos[\[Theta]1] + l2*Cos[\[Theta]2] + l3*Cos[\[Theta]3]
f2[p1[\[Theta]3_, s_]] = 
 s*Sin[\[Theta]1] + l2*Sin[\[Theta]2] + l3*Sin[\[Theta]3]
F1 = ( {
    {f1[p1[\[Theta]3, s]]},
    {f2[p1[\[Theta]3, s]]}
   } );
p1[0] = ( {
    {0},
    {l2 + l3}
   } );
MatrixForm[J[p1[\[Theta]3_, s_]] = ( {
    {D[f1[p1[\[Theta]3, s]], s], D[f1[p1[\[Theta]3, s]], \[Theta]3]},
    {D[f2[p1[\[Theta]3, s]], s], D[f2[p1[\[Theta]3, s]], \[Theta]3]}
   } )]
ddet = Det[( {
    {D[f1[p1[\[Theta]3, s]], s], D[f1[p1[\[Theta]3, s]], \[Theta]3]},
    {D[f2[p1[\[Theta]3, s]], s], D[f2[p1[\[Theta]3, s]], \[Theta]3]}
   } )]
M = MatrixForm[Inverse[J[p1[s, \[Theta]3]]]]
POSTED BY: lrenzo greco
14 Replies
Posted 9 years ago

OK, here is what I get when I run the code posted above:

Screenshot from my Mathematica Notebook

POSTED BY: Michael Helmle
Posted 9 years ago

OK, here is what I get when I run the code posted above:

Screenshot from my Mathematica Notebook

POSTED BY: Michael Helmle
Anonymous User
Anonymous User
Posted 9 years ago

(let's see if this looks better)

Attachments:
POSTED BY: Anonymous User
Anonymous User
Anonymous User
Posted 9 years ago

i'm a little more lost than the others but understand det and matrix algebra well

however when i "ran your code" i just got allot of errors. here's what i'm seeing so far, and i'm not sure whether it is what your looking for or not...

In[1094]:= Module[{n, [Theta]3, s, [CapitalDelta][Theta], l3, l2, l1, [Theta]1, [Theta]2, f1, f2, F1, p1, J}, n = Real; p1[s, [Theta]3] = ({{[Theta]3}, {s}}); p1[n_] = ({{[Theta]3}, {s}}); [CapitalDelta][Theta] = 10*Pi/180; l2 = Rationalize@0.20; l3 = Rationalize@0.45; [Theta]1 = [Pi]; n = 0; [Theta]2 = Pi/6; [Epsilon] = 0.00001; [Theta]2max = 2 Pi; f1[p1[[Theta]3, s]] = sCos[[Theta]1] + l2Cos[[Theta]2] + l3*Cos[[Theta]3]; f2[p1[[Theta]3, s]] = sSin[[Theta]1] + l2Sin[[Theta]2] + l3*Sin[[Theta]3]; F1 = ({{f1[p1[[Theta]3, s]]}, {f2[p1[[Theta]3, s]]}}); p1[0] = ({{0}, {l2 + l3}}); J[p1[[Theta]3, s]] = ({{D[f1[p1[[Theta]3, s]], s], D[f1[p1[[Theta]3, s]], [Theta]3]}, {D[f2[p1[[Theta]3, s]], s], D[f2[p1[[Theta]3, s]], [Theta]3]}}); ddet = Det[({{D[f1[p1[[Theta]3, s]], s], D[f1[p1[[Theta]3, s]], [Theta]3]}, {D[f2[p1[[Theta]3, s]], s], D[f2[p1[[Theta]3, s]], [Theta]3]}})]; Print@ddet; M = MatrixForm[Inverse[J[p1[s, [Theta]3]]]] ]

During evaluation of In[1094]:= 0

Out[1094]//MatrixForm= \!( TagBox[ RowBox[{"(", "", GridBox[{ { RowBox[{"-", "1"}], RowBox[{"-", RowBox[{"Tan", "[", "[Theta]3$", "]"}]}]}, {"0", FractionBox[ RowBox[{"20", " ", RowBox[{"Sec", "[", "[Theta]3$", "]"}]}], "9"]} }, GridBoxAlignment->{ "Columns" -> {{Center}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}}, GridBoxSpacings->{"Columns" -> { Offset[0.27999999999999997], { Offset[0.7]}, Offset[0.27999999999999997]}, "ColumnsIndexed" -> {}, "Rows" -> { Offset[0.2], { Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}}], "", ")"}], Function[BoxForme$, MatrixForm[BoxForme$]]])

POSTED BY: Anonymous User
Anonymous User
Anonymous User
Posted 9 years ago

Trig functions (most proggies solve using a reiman sum or built-in math in intel/amd/arm chip) may yield numeric results.

But many Mathematica facilities are aware of Trignomic Identities, and can keep arbitrary precision because they dont' use "intel math", but the symbolic math solving rules.

POSTED BY: Anonymous User
Posted 9 years ago

The problem is that you assigned MatrixForm to mat. In my original post there was a bracket (mat ...)//MatrixForm which avoids this. So please insert the brackets again.

POSTED BY: Michael Helmle
Anonymous User
Anonymous User
Posted 9 years ago

yes you know calculations using 0.45 will be inaccurate if done continually (see the opener in any free physics book or lab book about calculations involving precision and accuracy)

4/5 + 5/6 is done by the Kernel without ever digitizing numbers, it does this whenever possible, it's called "arbitrary precision numberic support" and most mathematic products (ie, MathCad) cannot even do it (Mathematica has always done it)

If you can, always use rational numbers (fractions) in calculations, use them until it's time to Print[] the number (view it, or let Graph chop/digitize it as it needs to). the old mathematica book suggested that, i'm hoping the new one still does.

it's not a worry until you see "0.04" in a book and your career might depend on knowing if that 4 is in an estimated last digit, if rounded from one, or is fully known (guess digit omitted), and if anyone knows what the next digits is :)

on the other hand, some Mathematica functions (you need to know which) may not give you an arbitrary precision result (ie, the approximation functions), but when they do it they keep the precision you ask for (which may be quite large, but if doing astronomical eqn, may need to be quite large to cover losses during calculations)

POSTED BY: Anonymous User
Posted 9 years ago

Thank you David, if the apporoximations are not the problems,why the multiplication (last output) is printed in yhat way? enter image description here

enter image description here

POSTED BY: lrenzo greco
Posted 9 years ago

The approximate number 0.45 is introduced in the input, and therefore produces an approximate number in the output. Compare to the code below, which uses the exact number 45/100. However, an approximate number is a number, and does nothing to interfere with multiplication, matrix or otherwise.

In[1]:= f1 = s Cos[\[Theta]1] + l2 Cos[\[Theta]2] + l3 Cos[\[Theta]3];

In[2]:= f2 = s Sin[\[Theta]1] + l2 Sin[\[Theta]2] + l3 Sin[\[Theta]3];

In[3]:= (jmat = {{D[f1, s], D[f1, \[Theta]3]}, {D[f2, s], 
      D[f2, \[Theta]3]}}) // MatrixForm ;

In[4]:= ddet = Det[jmat] ;

In[5]:= Simplify[ddet /. {l3 -> 45/100, \[Theta]1 -> \[Pi]}] ;

In[6]:= mat = 
 Simplify[Inverse[jmat] /. {l3 -> 45/100, \[Theta]1 -> \[Pi]}]


Out[6]= {{-1, -Tan[\[Theta]3]}, {0, (20 Sec[\[Theta]3])/9}}
POSTED BY: David Keith
Posted 9 years ago

Hi Michael, i mean that matrix mat is printed with approximate number(-1.) and,for this, i cant use this matrix in a multiplication. enter image description here

enter image description here

PS The approximation in the matrix mat is in the code posted by you.

POSTED BY: lrenzo greco
Posted 9 years ago

Hi Irenzo,

could you please explain what do you mean with "mat has still the approximation"?

POSTED BY: Michael Helmle
Posted 9 years ago

Can someone help me?

POSTED BY: lrenzo greco
Posted 9 years ago

Thank you Michael for your post. Why the matrix "mat" still has the apporoximation? enter image description here

POSTED BY: lrenzo greco
Posted 9 years ago

Hello Irenzo,

coming directly back to your question: you can use Simplify[ddet = .....] to achieve the result without the leading 0. But there are some other topics: you called J[p1[s, [Theta]3] in the last line of your code with the arguments in reversed order to the definition. Unless this is on purpose this will lead to s and theta3 values being exchanged in this function. In general the style of your code can be changed to make the whole calculation much more compact:

f1 = s Cos[\[Theta]1] + l2 Cos[\[Theta]2] + l3 Cos[\[Theta]3];
f2 = s Sin[\[Theta]1] + l2 Sin[\[Theta]2] + l3 Sin[\[Theta]3];

(jmat = {{D[f1, s], D[f1, \[Theta]3]}, {D[f2, s], 
     D[f2, \[Theta]3]}}) // MatrixForm

ddet = Det[jmat]

Simplify[ddet /. {l3 -> 0.45, \[Theta]1 -> \[Pi]}]

(mat = Simplify[
    Inverse[jmat] /. {l3 -> 0.45, \[Theta]1 -> \[Pi]}]) // MatrixForm
POSTED BY: Michael Helmle
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard