Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matrix of functions?

Subject: Matrix of functions?

From: Allen

Date: 13 Oct, 2005 02:24:38

Message: 1 of 9

Hi again! [Guess it's twice in the same day.. sorry!]

I was wondering if it's possible to make a matrix of functions in
matlab. In other words:

sin(theta) cos(theta)+cos(psi) 0
cos(theta) -sin(theta) 1
0 0 0

That type of thing... If possible, then I can do simple matrix math with
multiple matrixes like this, supplying the theta and psi as well...

Now, I could do this all by hand, taking each index separately.

But, I thought I'd check to ensure Matlab didn't have this capability first.

Thanks!
-Allen

Subject: Matrix of functions?

From: Jerome

Date: 13 Oct, 2005 03:41:34

Message: 2 of 9

Hi,

why don't you create your own function :

function M=mymatrix(theta,psi)

M=zeros(3,3);
M(1,1)=sin(theta);
M(2,1)=cos(theta);
M(1,2)=cos(theta)+cos(psi);
M(2,2)=-M(1,1);
M(2,3)=1;

Then simply call :

theta=...
psi=...
A=mymatrix(theta,psi)

Hope this helps
Jérôme

Subject: Matrix of functions?

From: Nasser Abbasi

Date: 13 Oct, 2005 08:18:34

Message: 3 of 9

 

"Allen" <ajhalldeleteme@gmaildot.com> wrote in message
news:dil16r$k1e$1@news.ks.uiuc.edu...
> Hi again! [Guess it's twice in the same day.. sorry!]
>
> I was wondering if it's possible to make a matrix of functions in
> matlab. In other words:
>
> sin(theta) cos(theta)+cos(psi) 0
> cos(theta) -sin(theta) 1
> 0 0 0
>
> That type of thing... If possible, then I can do simple matrix math
> with multiple matrixes like this, supplying the theta and psi as
> well...
>
> Now, I could do this all by hand, taking each index separately.
>
> But, I thought I'd check to ensure Matlab didn't have this
> capability first.
>
> Thanks!
> -Allen
>

With symbolic toolbox you can do all of this easily.

As you know, with numeric Matlab, a variable needs to be a have a
value before it is referenced.

i.e. one can not write

F = m*a;
m=5; a=3;

but one must write
m-5; a=3;
F=m*a;

With the symbolic toolbox, you can write as in the first case above,
i.e. one can have free variables, these are variables that are not
bounded to any value at the time they are encountered. (called syms in
Matlab)

For example, with extended symbolic toolbox, I could write

'R:=Matrix([
[sin(theta),cos(theta)+cos(psi),0],[cos(theta),-sin(theta),1],[0,0,0]]
 )'

Even thought at the time, theta, psi have no values.

Later on, one creates instances of this variable using different
values of psi and theta, without using functions, by simply
substituting different values for theta and psi each time as follows:

>> maple('with(LinearAlgebra)');
maple('Digits:=6');
maple('R:=Matrix([
[sin(theta),cos(theta)+cos(psi),0],[cos(theta),-sin(theta),1],[0,0,0]]
 )');
maple('A:=subs({theta=Pi/2,psi=Pi/4},R)');
maple('B:=subs({theta=Pi/8,psi=Pi/8},R)');
maple('convert(evalf(A+B),listlist)')

ans =

[[1.38268, 2.55487, 0.], [.923880, -1.38268, 2.], [0., 0., 0.]]

>>

Nasser

Subject: Matrix of functions?

From: Brad Phelan

Date: 13 Oct, 2005 05:52:45

Message: 4 of 9

Allen wrote:
> Hi again! [Guess it's twice in the same day.. sorry!]
>
> I was wondering if it's possible to make a matrix of functions in
> matlab. In other words:
>
> sin(theta) cos(theta)+cos(psi) 0
> cos(theta) -sin(theta) 1
> 0 0 0
>
> That type of thing... If possible, then I can do simple matrix math with
> multiple matrixes like this, supplying the theta and psi as well...
>
> Now, I could do this all by hand, taking each index separately.
>
> But, I thought I'd check to ensure Matlab didn't have this capability first.
>
> Thanks!
> -Allen

You could use an anonymous function at the matlab command prompt

f = @(th, psi) [ sin(th) cos(th)+cos(psi) 0 ; ...
                  cos(th) -sin(th) 1 ;
                  0 0 0 ];

m = f(pi, pi/2)


--
==========================================
Solutions For Matlab, Simulink & RTW
http://xtargets.com
__________________________________________

Subject: Matrix of functions?

From: Joerg J. Buchholz

Date: 13 Oct, 2005 04:26:17

Message: 5 of 9

Nasser,

you don't even have to knock on Maple's door:

>> syms th ps
>> R =3D [sin(th), cos(th) + cos(ps), 0; cos(th), -sin(th), 1; 0, 0, 0]

R =3D

[ sin(th), cos(th)+cos(ps), 0]
[ cos(th), -sin(th), 1]
[ 0, 0, 0]


>> subs (R, {th, ps}, {pi/2, 0})

ans =3D

    1.0000 1.0000 0
    0.0000 -1.0000 1.0000
         0 0 0

J=F6rg

Subject: Matrix of functions?

From: Nasser Abbasi

Date: 13 Oct, 2005 15:41:21

Message: 6 of 9

 

>>>
"Joerg J. Buchholz" <jjbuchholz@gmail.com> wrote in message
news:1129202777.204321.180200@g44g2000cwa.googlegroups.com...
Nasser,

you don't even have to knock on Maple's door:

>> syms th ps
>> R = [sin(th), cos(th) + cos(ps), 0; cos(th), -sin(th), 1; 0, 0, 0]

R =

[ sin(th), cos(th)+cos(ps), 0]
[ cos(th), -sin(th), 1]
[ 0, 0, 0]


>> subs (R, {th, ps}, {pi/2, 0})

ans =

    1.0000 1.0000 0
    0.0000 -1.0000 1.0000
         0 0 0

Jörg
>>>

hi Jorg;

That is true ofcourse in this case, but using syms has its limitation
as I am sure you know. In addition one needs to learn new syntax for
using syms and for using special functions in main matlab designed to
use syms only.

So, basically there are 3 systems: 1. numeric Matlab, 2. basic syms
toolbox, 3. extended syms toolbox.

system 1 above is can do all the numeric well. system 3 above can do
all the symbolic well, system 2 above can do half the symbolic well.

So, it seems to me to be more worthwhile to learn system 3 well if one
wants to use symbolic in Matlab. I think It is worthwhile to buy the
extended syms toolbox and learn that than having to deal with syms
variables and its limited functionality.

A simple example, I do not think there is a function in Matlab to
obtain the adjoint of a symbolic matrix

>> adjoint(R)
??? Undefined command/function 'adjoint'.

but If I use the extend syms toolbox, I have full access to all of
Maple's functions that works on symbolic, one could write

>> maple('with(LinearAlgebra)');
maple('R:=Matrix([
[sin(theta),cos(theta)+cos(psi),0],[cos(theta),-sin(theta),1],[0,0,0]]
 )');
maple('convert(Adjoint(R),listlist)')

ans =

[[0, 0, cos(theta)+cos(psi)], [0, 0, -sin(theta)], [0,
0, -sin(theta)^2-cos(theta)^2-cos(theta)*cos(psi)]]

>>

Nasser

Subject: Matrix of functions?

From: Allen

Date: 13 Oct, 2005 15:19:12

Message: 7 of 9

Jérôme wrote:
> Hi,
>
> why don't you create your own function :
>
> function M=mymatrix(theta,psi)
>
> M=zeros(3,3);
> M(1,1)=sin(theta);
> M(2,1)=cos(theta);
> M(1,2)=cos(theta)+cos(psi);
> M(2,2)=-M(1,1);
> M(2,3)=1;
>
> Then simply call :
>
> theta=...
> psi=...
> A=mymatrix(theta,psi)
>
> Hope this helps
> Jérôme

:) Yes, this was the way I figured I'd have to go. ;) Thanks for your
help, Jerome!

-Allen

--
------
a.s.p. Brother of the Briar, May 2001
http://tobaccocellar.org/tinlist.php?cellar=119

Subject: Matrix of functions?

From: Allen

Date: 13 Oct, 2005 15:20:00

Message: 8 of 9

Nasser and Jeorg,

Thanks very much for your help! I didn't know that matlab could do
symbolic at all!! :) So, I greatly appreciated the quick rundown of
the 3 types!

Thank you!! :)
-Allen

Subject: Matrix of functions?

From: Allen

Date: 13 Oct, 2005 15:20:37

Message: 9 of 9

Anonymous function- another great idea! :)

Thanks, Brad!!
-Allen

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us