help i dont know why my code isnt working

1 次查看(过去 30 天)
a = 0; % lower bound of interval
b = 2*π; % upper bound of interval
tol = 0.001; % tolerance
while (b-a) > tol
c = (a + b) / 2; % midpoint of interval
if dy(a)*dy(c) < 0
b = c;
else
a = c;
end
min_angle = (a + b) / 2; % angle of crank for minimum y-coordinate
max_angle = (a + b) / 2 + π; % angle of crank for maximum y-coordinate
min_angle = (a + b) / 2;
% angle of crank for minimum y-coordinate max_angle = (a + b) / 2 + pi;
% angle of crank for maximum y-coordinate
% Initialize arrays to store angles and y-coordinates angles = a:0.001:b;
y_coords = zeros(size(angles));
% Calculate y-coordinate
for each angle for i = 1:length(angles) theta = angles(i);
beta = asin((CD/BC)sin(theta));
phi = pi - beta;
psi = acos((DE^2 - BC^2 - CE^2)/(2BCCE));
alpha = phi - psi;
y_coords(i) = OAcos(theta) + sqrt(OB^2 - OA^2sin(theta)^2) + CDcos(alpha) - DE*cos(beta+alpha) - d;
end
% Find the minimum and maximum y-coordinates and their corresponding angles
[min_y, min_idx] = min(y_coords);
[max_y, max_idx] = max(y_coords);
min_angle = angles(min_idx); max_angle = angles(max_idx);
% Display the results
fprintf('Minimum y-coordinate: %f at angle %f\n', min_y, min_angle);
fprintf('Maximum y-coordinate: %f at angle %f\n', max_y, max_angle);

回答(1 个)

Walter Roberson
Walter Roberson 2023-2-26
b = 2*π; % upper bound of interval
π is a permitted character in MATLAB only in
  • comments
  • character vectors
  • string objects
Never as the name of a function or variable.
  3 个评论
Image Analyst
Image Analyst 2023-2-26
编辑:Image Analyst 2023-2-26
To expand on that, use
beta = asin((CD/BC) * sin(theta));
instead of
beta = asin((CD/BC)sin(theta));
If CD is actually the product of C and D instead of one variable name, then use
beta = asin(((C*D)/(B*C)) * sin(theta));
And if you want to work in degrees instead of radians, there are versions of those functions that end in d, like sind and asind
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
Walter Roberson
Walter Roberson 2023-2-26
B C D E are not defined so BC cannot be B*C
The notation and formulas hint that BC is intended to be the magnitude of the distance between two points labeled B and C, typically points in two dimensions.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulink Block Considerations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by