Why is my function returning these error messages?

2 ビュー (過去 30 日間)
Stella Mackenzie
Stella Mackenzie 2021 年 3 月 8 日
コメント済み: Stella Mackenzie 2021 年 3 月 8 日
I am new to matlab, and though I am versed in java as a language this is my first time writing code in a context such as this one.
For context, this code is an attempt at the creation of a function that would rotate an input 3d coordinate about the line y = x = z by a given angle. This involves creating new variables (t, u and v) that represent the rotation matrix multiplied by the input x, y and z values.
The coordinate input would take the form of (p,q,s)=(x,y,z) and the angle of rotation is labelled as 'alpha'.
The code:
function [t,u,v] = rotation(p,q,s,alpha)
t = p((1-cosd(alpha))/3 + cosd(alpha)) + q(((1-cosd(alpha))/3 - sind(alpha)/sqrt(3))) + s((1-cosd(alpha)/3)+(sind(alpha)/sqrt(3)));
u = p((1-cosd(alpha)/3)+(sind(alpha)/sqrt(3))) + q((1-cosd(alpha)+cosd(alpha))/3) + s(((1-cosd(alpha))/3 - sind(alpha)/sqrt(3)));
v = p(((1-cosd(alpha))/3 - sind(alpha)/sqrt(3))) + q((1-cosd(alpha)/3)+(sind(alpha)/sqrt(3))) + s((1-cosd(alpha)+cos(alpha))/3);
end
I had hoped that when using the function in the command window, such as
>> rotation(24,31,62,60)
  • Where the coordinate is (24,31,62) and the angle = 60
Would output the rotated coordinates, in the form [t,u,v].
However, these two errors keep returning and I'm not sure what went wrong:
Array indices must be positive integers or logical values.
Error in rotation (line 2)
t = p((1-cosd(alpha))/3 + cosd(alpha)) + q(((1-cosd(alpha))/3 - sind(alpha)/sqrt(3))) +
s((1-cosd(alpha)/3)+(sind(alpha)/sqrt(3)));
I know the second error is likely a syntax mistake of mine, but I can't spot it, and the first error is new to me.
Please help!
Thanks and apologies if this is a simple fix. I'm still learning :)
  1 件のコメント
Stella Mackenzie
Stella Mackenzie 2021 年 3 月 8 日
Upon further inspection, with Weikang's help in mind, I realised quite how ridiculously faulty the above function is.
For any poor souls attempting something similar to me, here is my revised code that now works:
function [t, u, v] = rotation(p,q,s,alpha)
a = (1-cosd(alpha))/3 +cosd(alpha);
c = (1-cosd(alpha))/3 + sind(alpha)/sqrt(3);
b = (1-cosd(alpha))/3 - sind(alpha)/sqrt(3);
t = p*a + q*b + s*c;
u = p*c + q*a + s*b;
v = p*b + q*c + s*a;
end
I also was calling the function incorrectly.
>>[t, u, v] = rotation(p,q,s,alpha)
Should return the correct values for t, u and v.
Thanks again Weikang!!

サインインしてコメントする。

採用された回答

weikang zhao
weikang zhao 2021 年 3 月 8 日
You may have missed all the multiplication signs, such as:
t = p*((1-cosd(alpha))/3 + cosd(alpha)) + q*(((1-cosd(alpha))/3 - sind(alpha)/sqrt(3))) + s*((1-cosd(alpha)/3)+(sind(alpha)/sqrt(3)));
Matlab will treat pqs as a matrix if you use () directly after them, but the content in the parentheses is not an integer and cannot be indexed, so an error is raised.
by the way, the third formula should be wrong, because you use the sin function instead of sind. Please recheck the correctness of the formula used in this program.
  1 件のコメント
Stella Mackenzie
Stella Mackenzie 2021 年 3 月 8 日
Oh, of course! Thanks so much Weikang :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by