eval not working for a range of numbers

1 回表示 (過去 30 日間)
Matthew Covington
Matthew Covington 2021 年 4 月 5 日
回答済み: Steven Lord 2021 年 4 月 6 日
I have typed a code for the position of a mechanism with 6 links using vector loop equations with Euler. My code works from theta2 = 0*pi/180 to 97*pi/180. It also works around 330 degrees and higher. Why does it not work at 98 degrees or higher?
clc,clear
format shortg
% known values
Crank = 0.10 ; % length in meters
AB = 0.20 ; % legnth in meters
BC = 0.113 ; % length in meters
Link4 = 0.35 ; % length in meters
theta2 = 0*pi/180 ; % radians
%% Position
% known vectors
Crank_ = Crank*[cos(theta2) sin(theta2) 0] ;
Ground1_ = [0.1646 0.0250 0] ; % O2 to O6, meters
Ground2_ = [0.197 -0.2262 0] ; % O2 to O4, meters
Ground3_ = [0.0324 -0.2512 0] ; % O6 to O4, meters
% create symbolic variables
syms thetaAB theta4
% set assumptions
assume(theta4 >= 0 & theta4 <= pi)
assume(thetaAB >= 0 & thetaAB <= pi/2)
thetaBC = thetaAB - 64*pi/180 ;
% convert to polar coordinates
[theta_g1,Ground1] = cart2pol(Ground1_(1),Ground1_(2)) ;
[theta_g2,Ground2] = cart2pol(Ground2_(1),Ground2_(2)) ;
[theta_g3,Ground3] = cart2pol(Ground3_(1),Ground3_(2)) ;
% set up equations
Pos1 = Ground2*cos(theta_g2) + Link4*cos(theta4) - Crank*cos(theta2) - AB*cos(thetaAB) ;
Pos2 = Ground2*1i*sin(theta_g2) + Link4*1i*sin(theta4) - Crank*1i*sin(theta2) - AB*1i*sin(thetaAB) ;
% solve
sol = solve(Pos1, Pos2, thetaAB, theta4) ;
thetaAB = eval(sol.thetaAB) ;
theta4 = eval(sol.theta4) ;
% second syms to find theta6 and O6_C
syms theta6 O6_C
assume(O6_C >= 0 & O6_C <= 0.24)
assume(theta6 >= 0 & theta6 <= pi)
% set up equations
Pos3 = Ground1*cos(theta_g1) + O6_C*cos(theta6) - Crank*cos(theta2) - AB*cos(thetaAB) - BC*cos(thetaBC) ;
Pos4 = Ground1*1i*sin(theta_g1) + O6_C*1i*sin(theta6) - Crank*1i*sin(theta2) - AB*1i*sin(thetaAB) - BC*1i*sin(thetaBC) ;
% solve
sol = solve(Pos3, Pos4, theta6, O6_C) ;
theta6 = eval(sol.theta6) ;
O6_C = eval(sol.O6_C) ;

回答 (2 件)

Steven Lord
Steven Lord 2021 年 4 月 6 日
There's no need for eval.
% known values
Crank = 0.10 ; % length in meters
AB = 0.20 ; % legnth in meters
BC = 0.113 ; % length in meters
Link4 = 0.35 ; % length in meters
theta2 = 0*pi/180 ; % radians
%% Position
% known vectors
Crank_ = Crank*[cos(theta2) sin(theta2) 0] ;
Ground1_ = [0.1646 0.0250 0] ; % O2 to O6, meters
Ground2_ = [0.197 -0.2262 0] ; % O2 to O4, meters
Ground3_ = [0.0324 -0.2512 0] ; % O6 to O4, meters
% create symbolic variables
syms thetaAB theta4
% set assumptions
assume(theta4 >= 0 & theta4 <= pi)
assume(thetaAB >= 0 & thetaAB <= pi/2)
thetaBC = thetaAB - 64*pi/180 ;
% convert to polar coordinates
[theta_g1,Ground1] = cart2pol(Ground1_(1),Ground1_(2)) ;
[theta_g2,Ground2] = cart2pol(Ground2_(1),Ground2_(2)) ;
[theta_g3,Ground3] = cart2pol(Ground3_(1),Ground3_(2)) ;
% set up equations
Pos1 = Ground2*cos(theta_g2) + Link4*cos(theta4) - Crank*cos(theta2) - AB*cos(thetaAB) ;
Pos2 = Ground2*1i*sin(theta_g2) + Link4*1i*sin(theta4) - Crank*1i*sin(theta2) - AB*1i*sin(thetaAB) ;
% solve
sol = solve(Pos1, Pos2, thetaAB, theta4) ;
thetaAB = sol.thetaAB
thetaAB = 
theta4 = sol.theta4
theta4 = 
If you need the numeric values of thetaAB and theta4:
thetaAB_double = double(thetaAB)
thetaAB_double = 0.6297
thetaAB_vpa = vpa(thetaAB)
thetaAB_vpa = 
0.62968959483025850693174017557764
theta4_double = double(theta4)
theta4_double = 1.3850
theta4_vpa = vpa(theta4)
theta4_vpa = 
1.3850382471662801075251989294477

Image Analyst
Image Analyst 2021 年 4 月 5 日
Why are you even using eval at all instead of simply doing this:
thetaAB = sol.thetaAB;
theta4 = sol.theta4;
???
  2 件のコメント
Matthew Covington
Matthew Covington 2021 年 4 月 5 日
編集済み: Matthew Covington 2021 年 4 月 5 日
I didn't know you could do that, my professor just told us to use eval whenever we solve using syms.
Image Analyst
Image Analyst 2021 年 4 月 5 日
I don't know. I don't use syms. He may be right. But do you need to use syms? Can't you define theta over some range with linspace() and do it numerically? What does this show:
whos sol

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

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by