Post Reply 
Struggling with CAS programming
09-17-2018, 12:54 AM (This post was last modified: 09-17-2018 12:57 AM by rushfan.)
Post: #1
Struggling with CAS programming
I wrote a program to calculate the state space form a transfer function. It works a bit like the Matlab tf2ss function, but return state space in canonical phase form. It receives a vector num and a vector den, and returns 4 matrices A B C D. I managed to make it work line by line in the CAS mode, but when I run it as a CAS function, it does not work.
Code:

#cas
tf2ss(num,den):=
BEGIN
// declare vars and alocate matrices
local ordem := size(den)-1;
local a:= identity(ordem)*0;
local b:= identity(ordem)*0;
local c:= revlist(num);
local d:=0;
// fill A is in the form [0 1 0; 0 0 1; -p/m -o/m -n/m], where n m o p are from den
a:= replace(a,{ordem,1},-revlist(den[2..(ordem+1)])/den(1));
FOR lpv FROM 1 TO ordem-1 DO
a(lpv,lpv+1) = 1;
end;
// fill b 
b:= delrows(b,2..ordem);
b:= replace(b,{1,ordem},[1]);
b:= tranpose(b);

return a;
//return b;
//return c;
//return d;
END;
#end

When I run the function with tf2ss([1 2 3 4], [1 2 3 4 5]) I receive "Error: Invalid Input". I haven't been able to figure out what is wrong with the code. Documentation is sparse and hard to find.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Struggling with CAS programming - rushfan - 09-17-2018 12:54 AM



User(s) browsing this thread: 1 Guest(s)