Though the code is pretty simple, still the value of theta remains 0 every time I execute the results with different values of w,x,y,z,u and v. The value of theta doesn't change anytime.

1 回表示 (過去 30 日間)
w=30; x=60; y=45; z=90; u=35; v=75;
% "w,x,y,z are known" and "u and v are free choices". %
a12_c=cosd(w); a12_s=sind(w); a13_c=cosd(x); a13_s=sind(x);
b12_c=cosd(y); b12_s=sind(y); b13_c=cosd(z); b13_s=sind(z);
c12_c=cosd(u); c12_s=sind(u); c13_c=cosd(v); c13_s=sind(v);
A=[1 0 1 0 -1 0; 0 1 0 1 0 -1; a12_c -a12_s c12_c -c12_s -b12_c b12_s;...
a12_s a12_c c12_s c12_c -b12_s -b12_c; a13_c -a13_s c13_c -c13_s -b13_c b13_s;...
a13_s a13_c c13_s c13_c -b13_s -b13_c];
B=[1 0 1 0 1 0]';
% disp(A); disp(B);
X=A\B; % X=[P Q R S T U]';
disp('The required matrix is:')
disp(X);
P=X(1,1); Q=X(2,1); R=X(3,1); S=X(4,1); T=X(5,1); U=X(6,1);
a=sqrt(P.^2 + Q.^2); b=sqrt(R.^2 + S.^2); c=sqrt(T.^2 + U.^2);
disp('The link lengths are:');
fprintf('a= %.3f \n',a); fprintf('b= %.3f \n',b);
fprintf('c= %.3f \n',c); fprintf('d= 1\n');
theta1_1_a= acosd(P/a); theta1_1_b=-theta1_1_a;
theta1_2_a= asind(Q/a); theta1_2_b=180-theta1_2_a;
theta=0;
fprintf('theta1_1_a= %.2f \n',theta1_1_a);
fprintf('theta1_1_b= %.2f \n',theta1_1_b);
fprintf('theta1_2_a= %.2f \n',theta1_2_a);
fprintf('theta1_2_b= %.2f \n',theta1_2_b);
if theta1_1_a==theta1_2_a || theta1_1_a==theta1_2_b
theta=theta1_1_a;
fprintf('theta= %.2f\n', theta1_1_a);
elseif (theta1_1_b==theta1_2_a) || (theta1_1_b==theta1_2_b)
theta=theta1_1_b;
end
fprintf('theta= %.2f\n', theta);

採用された回答

John D'Errico
John D'Errico 2020 年 6 月 25 日
Without even trying to exceute your code...
You set theta to zero.
The value of theta will ONLY ever be changed if a test is satisfied for exact equality between two floating point numbers. So tests like this:
if theta1_1_a==theta1_2_a || theta1_1_a==theta1_2_b
Now, l'll look at the result of your code.
[theta1_1_a, theta1_2_a]
ans =
64.4196851608345 -64.4196851608345
>> [theta1_1_a, theta1_2_b]
ans =
64.4196851608345 244.419685160834
So that test must fail.
Then you have an elseif clause.
elseif (theta1_1_b==theta1_2_a) || (theta1_1_b==theta1_2_b)
Is it satisfied?
[theta1_1_b, theta1_2_a]
ans =
-64.4196851608345 -64.4196851608345
Gosh, it looks like they are the same numbers. Are they really the same?
[theta1_1_b == theta1_2_a]
ans =
logical
0
theta1_1_b - theta1_2_a
ans =
1.4210854715202e-14
As you can see, they are not in fact identical.
NEVER TEST FOR EXACT EQUALITY BETWEEN FLOATING POINT NUMBERS. At least not until you truly understand why I just told you not to do so, and you understand when you can expect such a test to be valid. And even then, be careful.
Learn to use tolerances.
  1 件のコメント
SRINJOY SAHA
SRINJOY SAHA 2020 年 6 月 25 日
Thank you sir for your good explanation. I never knew that Matlab has such high precision for variables, since I am new to matlab. My doubt is clear now. Also, I wanted to know how to set the digit precision for float variables so that equality holds as done in my if-else clause.

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

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2020 年 6 月 25 日
Srinjoy - the problem is that you are using the equality operator when comparing floats. Look at your conditions for the if/elseif
if theta1_1_a==theta1_2_a || theta1_1_a==theta1_2_b
theta=theta1_1_a;
fprintf('theta= %.2f\n', theta1_1_a);
elseif (theta1_1_b==theta1_2_a) || (theta1_1_b==theta1_2_b)
theta=theta1_1_b;
end
See Avoiding Common Problems with Floating-Point Arithmetic for details. When comparing floats, you can use a tolerance check
if abs(theta1_1_a - theta1_2_a) < eps || abs(theta1_1_a - theta1_2_b) < eps
theta=theta1_1_a;
fprintf('theta= %.2f\n', theta1_1_a);
elseif abs(theta1_1_b - theta1_2_a) < eps || abs(theta1_1_b - theta1_2_b) < eps
theta=theta1_1_b;
end
Try the above and see what happens!
  1 件のコメント
SRINJOY SAHA
SRINJOY SAHA 2020 年 6 月 25 日
Thanks sir for the solution as well as clearing my doubt. I didn't knew that Matlab has such high precision values for floating numbers, since I am new to matlab. My code worked perfectly well after executing your piece of code.

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by