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.

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

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

Your answer cleared my point, but only for the angle part, as it displays only 1 output argument(which I checked is correct). I need to display all kx ky kz.... 1 thing I tried is created 3 separate functions for kx ky kz and refered these funct in the equi_axis_angle....but still can't figure out... my actual goal is to have angle and then a position vector displayed...in my final ans. I'm actually learning to devise algorithms and codes for a given problem.
If you simply want to display the output in the command window, then call the function with all of the arguments and without a trailing semi-colon:
[theta1, kx, ky, kz] = equi_axis_angle(X)
This will show the values of all outputs in the command window.
yes,I did that without a semi colon.....but my problem is that.....after running code...it displays only the 1st part(i.e the angle) of the question....it is not displaying kx ky kz...it should display all theta1, kx, ky, kz
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.
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)

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
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 Sparse 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!