The abbreviation of function cos and sin?

I have a long matrix and i waana abbreviate my funcion cos and sin. For exmaple i wanna see cos and sin like c and s on the command window.haw can i do that.

15 Comments

That's not a good idea. It's too easy to inadvertently use 'c' and 's' for variable names, and the combination would give you some very strange results.
Is this for Symbolic Toolbox ?
I'm going along with Roger on this. Another reason not to do it is that it would be a maintainability nightmare. Anyone going over your code later would say "What the heck was this guy thinking?" I'd actually go the opposite way - use longer variable names and function names because they will be more descriptive and easier for others to follow your code. I can't stand it when I'm going over code people posted and it looks like a jumbled alphabet soup of cryptic 1 and 2 letter variable names, especially when there are no comments to explain them.
If this is symbolic toolbox work and the key is displaying the information, then just before displaying the formula, you could subs() in the c and s in place of cos and sin.
I can imagine, that the source code for the creation of a rotation matrix e.g. based on Euler angles, is easier to maintain, when the rows match in a line width with less than 80 characters:
s = @sin; % Abbreviation for readability
c = @cos;
M = [ ...
c(y).*c(x), s(y), -c(y).*s(x); ...
-c(z).*s(y).*c(x)+s(z).*s(x), c(z).*c(y), c(z).*s(y).*s(x)+s(z).*c(x); ...
s(z).*s(y).*c(x)+c(z).*s(x), -s(z).*c(y), -s(z).*s(y).*s(x)+c(z).*c(x)];
clear('c', 's'); % Clean up to avoid mistakes!
Of course temporary variables like |cx = cos(x)| would be more efficient in the sense of computing time and text width, but this is thought as an example only.
But without doubt, I agree with Roger's and Image Analyst's general recommendation to avoid too compact function names. As any rule, exceptions can be found and must be documented clearly and exhaustively in the code.
I write codes like you said s = @sin; % Abbreviation for readability c = @cos;
but cos and sins are still same on command window.I cant see c and s.
Can you explain what is your matrix? Are you using symbolic functions?
s = @sin; % Abbreviation for readability
c = @cos;
t1=sym('t1');
t2=sym('t2');
t3=sym('t3');
t4=sym('t4');
t5=sym('t5');
t6=sym('t6');
T01=[c(t1) 0 s(t1) 0 ; s(t1) 0 -c(t1) 0 ; 0 1 0 0.26 ; 0 0 0 1];
T12=[c(t2) -s(t2) 0 -0.23*c(t2) ; s(t2) c(t2) 0 -0.23*s(t2) ; 0 0 1 -0.013 ; 0 0 0 1];
T23=[c(t3) -s(t3) 0 -0.24*c(t3) ; s(t3) c(t3) 0 -0.24*s(t3) ; 0 0 1 0 ; 0 0 0 1];
T34=[c(t4) 0 -s(t4) 0 ; s(t4) 0 c(t4) 0 ; 0 1 0 0.075 ; 0 0 0 1];
T45=[c(t5) 0 s(t5) 0 ; s(t5) 0 -c(t5) 0 ; 0 0 1 0.044 ; 0 0 0 1];
T56=[c(t6) s(t6) 0 0 ; s(t6) -c(t6) 0 0 ; 0 0 1 0.08 ; 0 0 0 1];
T03=T01*T12*T23;
T03transpose=T03.';
T06=T01*T12*T23*T34*T45*T56;
T36=T03transpose*T06;
T36s=T34*T45*T56;
r11=T06(1,1)
and i see on command window that;
r11 =
cos(t6)*(sin(t1)*sin(t5) - cos(t5)*(cos(t4)*(cos(t1)*sin(t2)*sin(t3) - cos(t1)*cos(t2)*cos(t3)) + sin(t4)*(cos(t1)*cos(t2)*sin(t3) + cos(t1)*cos.....
i dont wanna see that sins and coses.
and thank you for tryıng to help.
Try this
syms x y
f=[cos(x)+2 sin(y);cos(y) sin(x)*x]
out1=sym(regexprep(char(f),'cos','c'))
out=sym(regexprep(char(out1),'sin','s'))
Pays attention to other variables that possibly contain 'cos' and 'sin', they will also be replaced by c and s.
But maybe you see i have too many equations like f. So i cant write this for my all aquations. You see on my code ,i have T01 T12 and so on.
For example
syms t1
T01=[c(t1) 0 s(t1) 0 ; s(t1) 0 -c(t1) 0 ; 0 1 0 0.26 ; 0 0 0 1];
out1=sym(regexprep(char(T01),'cos','c'))
out=sym(regexprep(char(out1),'sin','s'))
it works just for one equation.
I need funtion that works for all of my equations.
What 'end' is doing in your code?
T01=[c(t1) 0 s(t1) 0 ; s(t1) 0 -c(t1) 0 ; 0 1 0 0.26 ; 0 0 0 1];
out1=sym(regexprep(char(T01),'cos','c'))
out=sym(regexprep(char(out1),'sin','s'))
end?
i dont have end in my code.
i just write and input parameters for my inverse and forward kinematics and i need to read them.
After Edit, there is no 'end'. Ok did you try the above code? what is wrong?

Sign in to comment.

Asked:

on 29 Dec 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!