Can I improve the code?

조회 수: 1 (최근 30일)
Hitesh Pradhan
Hitesh Pradhan 2021년 6월 26일
댓글: dpb 2021년 6월 26일
clc, clear all
%Variables and Parameters
v=20; %m/s, velocity
r=200; %m, radius
l=5; %m, distance between two ends of road
mu_s=0.4; %coefficient of friction
angle=10; %banking angle
theta=angle*(pi/180); %angle in rads
g=9.8; %gravity
%Calculations
Banking_Velocity=calcBANKvel(r,mu_s,g,theta);
Unbank_SafeVelocity=calcUNBANKsafe(mu_s,r,g);
Banking_Angle=calcBANKangel(v,r,g);
Bank_SafeVelocity=calcBANKsafe(r,g,theta);
Bank_Elevation=calcBANKelevation(l,v,r,g);
%Ouput
fprintf('The velocity of vehicle on a curved road is %4.2f m/s.\n',Banking_Velocity)
fprintf('The safe velocity on an unbanked road is %4.2f m/s.\n',Unbank_SafeVelocity)
fprintf('The angle of banking is %4.2f degrees.\n',Banking_Angle)
fprintf('The safe velocity on a banked road is %4.2f m/s.\n',Bank_SafeVelocity)
fprintf('The height of elevation is %4.2f m.\n',Bank_Elevation)
%Conditions
if v<Bank_SafeVelocity
disp('The velocity of the vehicle is safe for banking.')
end
if v>Bank_SafeVelocity
disp('The velocity of the vehicle is unsafe for banking.')
end
%Custom Functions
function BANK_velocity=calcBANKvel(r,mu_s,g,theta)
BANK_velocity=sqrt((r*g*(tan(theta)+mu_s))/(1-(mu_s*tan(theta))));
end
function BANK_unbankSafeVel=calcUNBANKsafe(mu_s,r,g)
BANK_unbankSafeVel=sqrt(mu_s*r*g);
end
function BANK_angle=calcBANKangel(v,r,g)
BANK_angle=atan((v^2)/(r*g))*(180/pi);
end
function BANK_bankSafeVel=calcBANKsafe(r,g,theta)
BANK_bankSafeVel=sqrt(r*g*tan(theta));
end
function BANK_elevation=calcBANKelevation(l,v,r,g)
BANK_elevation=l*(sin(atan((v^2)/(r*g))));
end
  댓글 수: 1
dpb
dpb 2021년 6월 26일
Without reading the solution equations to decipher just what the calculation is it isn't clear what the first output line "velocity of vehicle on a curved road " actually refers to. Some clarification of that would be helpful.
Also, while it's not likely going to be the case ever hit it exactly, there's no case for the critical case of exactly equal the safe velocity -- the comparisons are both one-sided.
Recasting as a function with the conditions passed in as arguments would make it more generally useful rather than as a script with hardcoded values...

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by