Matlab function call issue with matrices

Post here for assistance in troubleshooting a problem using GMAT

Matlab function call issue with matrices

Postby prince » Wed Jul 25, 2012 5:24 am

I wrote a small Matlab inverse function so that I can call it from GMAt script. I am seeing that the result I am getting in script is actually a transpose of the actual result. My script code and the Matlab function are given below:

Code: Select all

%General Mission Analysis Tool(GMAT) Script
%Created: 2012-07-24 08:48:48

%----------------------------------------
%---------- Arrays, Variables, Strings
%----------------------------------------

Create Array a[2,2] ai[2,2];
Create String newrow;
GMAT newrow = ' ';
GMAT a(1,1) = 2;
GMAT a(1,2) = 4;
GMAT a(2,1) = 5;
GMAT a(2,2) = 6;

Create ReportFile ReportFile1;
GMAT ReportFile1.SolverIterations = Current;
GMAT ReportFile1.UpperLeft = [ -0.0075046904315197 -0.04827586206896552 ];
GMAT ReportFile1.Size = [ 1.0140712945591 1.060344827586207 ];
GMAT ReportFile1.RelativeZOrder = 1312;
GMAT ReportFile1.Filename = 'ReportFile1.txt';
GMAT ReportFile1.Precision = 16;
GMAT ReportFile1.WriteHeaders = On;
GMAT ReportFile1.LeftJustify = On;
GMAT ReportFile1.ZeroFill = Off;
GMAT ReportFile1.ColumnWidth = 20;
GMAT ReportFile1.WriteReport = true;

%----------------------------------------
%---------- Functions
%----------------------------------------

Create MatlabFunction inverse;
GMAT inverse.FunctionPath = 'M:\My Documents\GMAT Sim';

%----------------------------------------
%---------- Mission Sequence
%----------------------------------------

BeginMissionSequence;

Report ReportFile1 a(1,1) a(1,2) a(2,1) a(2,2);

GMAT [ai] = inverse(a);
Report ReportFile1 ai(1,1) ai(1,2) ai(2,1) ai(2,2);


Matlab function
Code: Select all

function [y] = inverse(x)
   y = x^-1;
end


GMAT Results:

Code: Select all

a(1,1)                 a(1,2)                 a(2,1)                 a(2,2)                 
2                      4                      5                      6                     
ai(1,1)                ai(1,2)                ai(2,1)                ai(2,2)               
-0.75                  0.625                  0.5                    -0.25                 



Matlab Result:
Code: Select all
K>> a=[2,4;5,6]
a =
    2.00000000000000e+000    4.00000000000000e+000
    5.00000000000000e+000    6.00000000000000e+000
K>> a^-1
ans =
   -750.000000000000e-003    500.000000000000e-003
    625.000000000000e-003   -250.000000000000e-003
prince
 
Posts: 16
Joined: Mon Jul 02, 2012 8:02 pm

Re: Matlab function call issue with matrices

Postby jjkparker » Wed Jul 25, 2012 12:52 pm

Hi,

Yes, this is a known bug with passing matrices from GMAT to MATLAB: see GMT-2921. It's scheduled to be fixed by R2013a (i.e. by November), but maybe we can bump it up some.
Joel J. K. Parker
Flight dynamics engineer, GMAT team
http://gmatcentral.org
jjkparker
 
Posts: 617
Joined: Thu Jan 07, 2010 9:48 pm
Location: NASA Goddard Space Flight Center, Greenbelt, MD

Re: Matlab function call issue with matrices

Postby prince » Wed Jul 25, 2012 6:14 pm

Not related to this, I saw GMAT math architecture doc says that there are some basic inbuilt functions like inv, transpose, max etc. for matrices. Do we have these functionality in the current release (my gmat doesnot recognize these commands)?
prince
 
Posts: 16
Joined: Mon Jul 02, 2012 8:02 pm

Re: Matlab function call issue with matrices

Postby jjkparker » Wed Jul 25, 2012 8:38 pm

Yes, there are a few:

Code: Select all
norm
det
inv


And transpose is the single-quote operator: A'
Joel J. K. Parker
Flight dynamics engineer, GMAT team
http://gmatcentral.org
jjkparker
 
Posts: 617
Joined: Thu Jan 07, 2010 9:48 pm
Location: NASA Goddard Space Flight Center, Greenbelt, MD

Re: Matlab function call issue with matrices

Postby prince » Wed Jul 25, 2012 9:49 pm

I guess it is not working for me. I am not sure my syntax is correct:

Code: Select all
GMAT [m3] = inv(mat);
Report ReportFile1 m3;

GMAT m4 = mat';
Report ReportFile1 m4;

====

Interpreting scripts from the file.
***** file: M:\My Documents\Research\Research SVN\IST-Rolla\Interplanetary GNC\GMAT Sim\test.script
1: **** ERROR **** Interpreter Exception: Nonexistent GmatFunction file "inv.gmf" referenced in "inv"


Single quote is producing script interpreter error as it is used for strings also.
prince
 
Posts: 16
Joined: Mon Jul 02, 2012 8:02 pm

Re: Matlab function call issue with matrices

Postby jjkparker » Thu Jul 26, 2012 12:21 pm

What version of GMAT are you using?

The attached script works for me:
Code: Select all
Create Array a[3,3] a_inv[3,3] a_trans[3,3]
Create Variable a_det
Create ReportFile r

BeginMissionSequence

% populate the matrix
a(1,1) = 8
a(1,2) = 1
a(1,3) = 6
a(2,1) = 3
a(2,2) = 5
a(2,3) = 7
a(3,1) = 4
a(3,2) = 9
a(3,3) = 2

% use built-ins
a_inv = inv(a)
a_trans = a'
a_det = det(a)

Report r a a_inv a_trans a_det

Here's the result:
Code: Select all
a                     
8 1 6                 
3 5 7                 
4 9 2                 
a_inv                 
0.1472222222222222 -0.1444444444444444 0.06388888888888888     
-0.06111111111111111 0.02222222222222222 0.1055555555555556   
-0.01944444444444444 0.1888888888888889 -0.1027777777777778   
a_trans               
8 3 4                 
1 5 9                 
6 7 2                 
a_det                 
-360                   


This all seems to work fine in R2012a (for me, anyway).
Attachments
matrixfuncs.script
(349 Bytes) Downloaded 318 times
Joel J. K. Parker
Flight dynamics engineer, GMAT team
http://gmatcentral.org
jjkparker
 
Posts: 617
Joined: Thu Jan 07, 2010 9:48 pm
Location: NASA Goddard Space Flight Center, Greenbelt, MD

Re: Matlab function call issue with matrices

Postby prince » Thu Jul 26, 2012 6:26 pm

thanks. my syntax was wrong. I was having GMAT keyword in the beginning of the statement. It works now.
prince
 
Posts: 16
Joined: Mon Jul 02, 2012 8:02 pm

Re: Matlab function call issue with matrices

Postby jjkparker » Thu Jul 26, 2012 7:41 pm

Huh, I would have expected that to work, too. I'll look into it further. Thanks for the report!

EDIT: Oh, I see, it was the square brackets in this line that was causing the problem: GMAT [m3] = inv(mat);
Joel J. K. Parker
Flight dynamics engineer, GMAT team
http://gmatcentral.org
jjkparker
 
Posts: 617
Joined: Thu Jan 07, 2010 9:48 pm
Location: NASA Goddard Space Flight Center, Greenbelt, MD

Re: Matlab function call issue with matrices

Postby pyxypecex » Wed Nov 07, 2018 10:58 am

The assignment of the new scheme has been done for the approval of the topics for the individuals. The roadmap of the success and uk bestessay has been done for the newly followed items for the help of the purely followed items for the candidates.
pyxypecex
 
Posts: 1
Joined: Wed Nov 07, 2018 10:34 am


Return to Troubleshooting

Who is online

Users browsing this forum: No registered users and 2 guests