Can I improve the code?

1 view (last 30 days)
Hitesh Pradhan
Hitesh Pradhan on 26 Jun 2021
Commented: dpb on 26 Jun 2021
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 Comment
dpb
dpb on 26 Jun 2021
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...

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!