help i dont know why my code isnt working

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 日

0 投票

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 件のコメント

Walter Roberson
Walter Roberson 2023 年 2 月 26 日
You had part of a comment about for each angle down on a line where it was executable code.
You have not defined BC CD CDcos DE CE OA OAcos OB
2BCCE is not a valid variable name.
2sin is not a valid variable name.
Please note that MATLAB has absolutely no implied multiplication -- not anywhere . Not even in the hidden language of the symbolic toolbox. Every multiplication must be stated using one of the two multiplication operators. I recommend that you use the .* multiplication operator at all times and the ./ division operator at all times (until you start needing to do linear algebra). .* and ./ are not always strictly necessary, but you will have fewer problems if you treat them as the rule instead of treating .* and ./ as the exceptions.
a = 0; % lower bound of interval
b = 2*pi; % 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 + pi; % 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));
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
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);
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.

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

製品

質問済み:

2023 年 2 月 25 日

コメント済み:

2023 年 2 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by