Solve command breaking when certain numbers are inserted?
23 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Connor Massey
am 2 Nov. 2022
Beantwortet: Walter Roberson
am 2 Nov. 2022
I am working on a 6-bar mechanism code for school, and I've run across an issue. I've got this code that is supposed to figure the positions of each link and plot them, but whenever the values for angle_2 are equal to 60 or 45, the code breaks, and I either get an error message or the plot is wrong. Works great for every other value so I am not sure what is the matter? I tried to use the assume command to make sure the angle values were right, but that broke the code as well. Any help would be appreciated!
clear;clc;
R1 = 12.8062; %O2-O4
R1a_ = [10,-8,0];
R1b_ = [-7,10.5,0];
R2 = 5; %OA
R3 = 7.5; %AB
R3_2 = 15; %AC
R4 = 15; %O4-B
R6 = 12.6194;
DO6 = 8;
angle_2 = 0:45:360;
thetag = 321.34;
thetag2 = 180 + atand(10.5/-7);
for i=1:length(angle_2)
syms angle_3 angle_4 R5_1 angle_5
Angle_eqs = [R2*cosd(angle_2(i))+R3*cosd(angle_3) == R4*cosd(angle_4)+R1*cosd(thetag);
R2*sind(angle_2(i))+R3*sind(angle_3) == R4*sind(angle_4)+R1*sind(thetag)];
Angle = solve(Angle_eqs,[angle_3 angle_4]);
angle_3 = eval(Angle.angle_3)
angle_4 = eval(Angle.angle_4)
angle_3_ = angle_3(1)
angle_4_ = angle_4(1)
OA = [R2*cosd(angle_2(i)),R2*sind(angle_2(i)),0];
AB = [OA(1)+R3*cosd(angle_3_),OA(2)+R3*sind(angle_3_),0];
AC = R3_2*[cosd(angle_3_),sind(angle_3_),0]+[OA(1),OA(2),0];
O4B = [R1a_(1)+R4*cosd(angle_4_),R1a_(2)+R4*sind(angle_4_)];
Angle_eqs2 = [R2*cosd(angle_2(i))+R3_2*cosd(angle_3_)==R5_1*cosd(angle_5)+R6*cosd(thetag2);
R2*sind(angle_2(i))+R3_2*sind(angle_3_)== R5_1*sind(angle_5)+R6*sind(thetag2)]; % %*cosd(thetag2)
Angle2 = solve(Angle_eqs2,[R5_1 angle_5]);
R5_1 = eval(Angle2.R5_1);
angle_5 = eval(Angle2.angle_5);
R5 = R5_1(1);
angle_5_ = angle_5(1);
R5val = R5;
R5 = R5*[cosd(angle_5_),sind(angle_5_),0]+R1b_;
D_O6 = R1b_- DO6*[cosd(angle_5_),sind(angle_5_),0];
CE = (27-R5val)*[cosd(angle_5_),sind(angle_5_),0]+AC;
plot([0,OA(1)],[0,OA(2)],[OA(1),AB(1)], ...
[OA(2),AB(2)],[AB(1),R1a_(1)],[AB(2),R1a_(2)] ...
,[OA(1),AC(1)],[OA(2),AC(2)],[R1b_(1),R5(1)],[R1b_(2),R5(2)],[R1b_(1),D_O6(1)],[R1b_(2),D_O6(2)] ...
,[AC(1),CE(1)],[AC(2),CE(2)])
axis ( [ -20 20 -20 20])
hold on
end
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 2 Nov. 2022
Never eval() a symbolic expression. There is no documented eval() for symbolic expressions.
In practice, eval() of a symbolic expression is the same as eval() of char() of the symbolic expression. But char() of a symbolic expression does not generally turn the symbolic expression into MATLAB code: it generally turns it into human-readable form that is not accepted as code.
Use subs() if you need to evaluate a symbolic expression after you have changed a symbolic variable to numeric. double() the result of subs() if you need double precision.
R1 = 12.8062; %O2-O4
R1a_ = [10,-8,0];
R1b_ = [-7,10.5,0];
R2 = 5; %OA
R3 = 7.5; %AB
R3_2 = 15; %AC
R4 = 15; %O4-B
R6 = 12.6194;
DO6 = 8;
angle_2 = 0:45:360;
thetag = 321.34;
thetag2 = 180 + atand(10.5/-7);
for i=1:length(angle_2)
syms angle_3 angle_4 R5_1 angle_5
Angle_eqs = [R2*cosd(angle_2(i))+R3*cosd(angle_3) == R4*cosd(angle_4)+R1*cosd(thetag);
R2*sind(angle_2(i))+R3*sind(angle_3) == R4*sind(angle_4)+R1*sind(thetag)];
Angle = solve(Angle_eqs,[angle_3 angle_4]);
angle_3 = double(subs(Angle.angle_3))
angle_4 = double(subs(Angle.angle_4))
angle_3_ = angle_3(1)
angle_4_ = angle_4(1)
OA = [R2*cosd(angle_2(i)),R2*sind(angle_2(i)),0];
AB = [OA(1)+R3*cosd(angle_3_),OA(2)+R3*sind(angle_3_),0];
AC = R3_2*[cosd(angle_3_),sind(angle_3_),0]+[OA(1),OA(2),0];
O4B = [R1a_(1)+R4*cosd(angle_4_),R1a_(2)+R4*sind(angle_4_)];
Angle_eqs2 = [R2*cosd(angle_2(i))+R3_2*cosd(angle_3_)==R5_1*cosd(angle_5)+R6*cosd(thetag2);
R2*sind(angle_2(i))+R3_2*sind(angle_3_)== R5_1*sind(angle_5)+R6*sind(thetag2)]; % %*cosd(thetag2)
Angle2 = solve(Angle_eqs2,[R5_1 angle_5]);
R5_1 = double(subs(Angle2.R5_1));
angle_5 = double(subs(Angle2.angle_5));
R5 = R5_1(1);
angle_5_ = angle_5(1);
R5val = R5;
R5 = R5*[cosd(angle_5_),sind(angle_5_),0]+R1b_;
D_O6 = R1b_- DO6*[cosd(angle_5_),sind(angle_5_),0];
CE = (27-R5val)*[cosd(angle_5_),sind(angle_5_),0]+AC;
plot([0,OA(1)],[0,OA(2)],[OA(1),AB(1)], ...
[OA(2),AB(2)],[AB(1),R1a_(1)],[AB(2),R1a_(2)] ...
,[OA(1),AC(1)],[OA(2),AC(2)],[R1b_(1),R5(1)],[R1b_(2),R5(2)],[R1b_(1),D_O6(1)],[R1b_(2),D_O6(2)] ...
,[AC(1),CE(1)],[AC(2),CE(2)])
axis ( [ -20 20 -20 20])
hold on
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
