I want this for loop to dispaly the smallest calculated value of PE. What would be the best way of going about this?

1 次查看(过去 30 天)
for t1 = 0:1:90,t2 = 0:1:90;
t1;
t2;
h1 = -b1*cosd(t1);
h2 = -(b1*cosd(t1)+b2*cosd(t2));
d1 = sqrt((x1-a1*sind(t1)).^2 + (y1+a1*cosd(t1)).^2)-L1;
d2 = sqrt((x1-(b1*sind(t1)+a2*sind(t2))).^2+(y2+(b1*cosd(t1)+a1*cosd(t2))).^2)-L2;
PE = (1/2)*k1*d1.^2 + w1*h1 - f1*b1*sind(t1) + (1/2)*k2*d2.^2 + w2*h2 - f2*(b1*sind(t1)+b2*sind(t2));
if PE == min
disp PE
end
end

回答(1 个)

Jeff Miller
Jeff Miller 2021-3-28
% something like this:
minPE = realmax;
for t1 = 0:1:90,t2 = 0:1:90;
t1;
t2;
h1 = -b1*cosd(t1);
h2 = -(b1*cosd(t1)+b2*cosd(t2));
d1 = sqrt((x1-a1*sind(t1)).^2 + (y1+a1*cosd(t1)).^2)-L1;
d2 = sqrt((x1-(b1*sind(t1)+a2*sind(t2))).^2+(y2+(b1*cosd(t1)+a1*cosd(t2))).^2)-L2;
PE = (1/2)*k1*d1.^2 + w1*h1 - f1*b1*sind(t1) + (1/2)*k2*d2.^2 + w2*h2 - f2*(b1*sind(t1)+b2*sind(t2));
if PE < minPE
minPE = PE;
end
end
disp minPE

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by