I'm writing a function which takes a 3x3 matrix as input and gives an angle and a position vector as output.Position vector has 3 more output arguements. Please help me identify the mistake i have made.

5 views (last 30 days)
Code, I have written the function code:
function [ double(theta1), double(kx), double(ky), double(kz)] = equi_axis_angle( var11, var12, var13; var21, var22, var23; var31, var32, var33 )
theta1 = acos((var11 + var22 + var33 - 1) * 0.5);
kx = (var32 - var23)*0.5/sin(theta1);
ky = (var13 - var31)*0.5/sin(theta1);
kz = (var21 - var12)*0.5/sin(theta1);
end
When I run the code I get:
Error: File: equi_axis_angle.m Line: 1 Column: 18
Unbalanced or unexpected parenthesis or bracket.
Pls see var11, var12...are matrix elements.

Accepted Answer

Stephen23
Stephen23 on 2 Oct 2015
Edited: Stephen23 on 2 Oct 2015
In MATLAB do not define the output variable types (or classes) explicitly, and one cannot use the character ';' within the function input argument list. I removed these two syntax errors, and the code runs without error:
function [theta1, kx, ky, kz] = equi_axis_angle( var11, var12, var13, var21, var22, var23, var31, var32, var33 )
However given your arrangement of the ';'-character in the input argument list it seems that you want these inputs to be a matrix. If this should be a matrix, then it is one argument, not nine, and should be defined like this:
function [theta1, kx, ky, kz] = equi_axis_angle(var)
theta1 = acos((var(1,1) + var(2,2) + var(3,3) - 1) * 0.5);
kx = (var(3,2) - var(2,3))*0.5/sin(theta1);
ky = (var(1,3) - var(3,1))*0.5/sin(theta1);
kz = (var(2,1) - var(1,2))*0.5/sin(theta1);
end
And then called simply using the matrix:
X = [1,2,3;4,5,6;7,8,9];
[theta1, kx, ky, kz] = equi_axis_angle(X)
You can see that this is much simpler and less buggy than using nine separate input arguments. Learn to use indexing in MATLAB instead of creating lots of variables, it makes your own life easier.
  5 Comments
Stephen23
Stephen23 on 3 Oct 2015
Can you please copy the exact way that you are calling this function, and paste it into a comment. I cannot read your computer screen, and it is difficult to diagnose without seeing the exact code you are using to call this function.
Jai  Khullar
Jai Khullar on 3 Oct 2015
Edited: Jai Khullar on 3 Oct 2015
I got it!...You were right, ';' had suppressed my answers.it took long but I have made certain changes so that my algorithm handles the special cases like 0 or 180 deg in case of angle. Thanks for the help...I'm kind of stuck in another subroutine...which is just inverse of the above problem...

Sign in to comment.

More Answers (2)

Guillaume
Guillaume on 2 Oct 2015
Edited: Guillaume on 2 Oct 2015
I have no idea what you're trying to accomplish with your function declaration. The double() in the output list and the ; in the input list make absolutely no sense.
The proper syntax for declaring a function is:
function [output_list] = function_name(input_list)
where output_list and input_list is a list of variable names only separated by commas only.
Possibly you meant:
function [theta1, kx, ky, kz] = equi_axis_angle(var11, var12, var13, var21, var22, var23, var31, var32, var33)
Or possibly you meant to pass the input as a single matrix instead of a gazillion input variables, in which case:
function [theta1, kx, ky, kz] = equi_axis_angle(some_meaningful_name) %replace some_meaningful_name by something that actually has meaning. Not var!
theta1 = acos((some_meaningful_name(1,1) + some_meaningful_name((2,2) + some_meaningful_name(3,3) - 1) * 0.5);
kx = (some_meaningful_name(3,2) - some_meaningful_name(2,3))*0.5/sin(theta1);
ky = (some_meaningful_name(1,3) - some_meaningful_name(3,1))*0.5/sin(theta1);
kz = (some_meaningful_name(2,1) - some_meaningful_name(1,2))*0.5/sin(theta1);
end

Andrei Bobrov
Andrei Bobrov on 2 Oct 2015
function [th, kxz] = equi_axis_angle(var)
th = acos((trace(var)-1)*.5);
k = var - var.';
k = k(tril(ones(3),-1)>0).*[1;-1;1];
kxz = k*.5/sin(th);

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!