Write MATLAB statements to perform the functions described below: If x is greate
ID: 3571754 • Letter: W
Question
Write MATLAB statements to perform the functions described below: If x is greater than or equal to zero, then assign the squareroot of x to the variable squareroot _x and print out the result. Otherwise, print out an error message about the argument of the squareroot function and set squareroot _x equal to zero. The cost per mile for a rented vehicle is $1.25 for the first 100 miles. $0.75 for the next 200 miles, and $0.70 for all miles in excess of 300 miles. Write MATLAB statements that determine the total cost and the average cost per mile for a given number of miles (stored in the variable distance).Explanation / Answer
Here is the code for you:
%If x is greater than or equal to zero, then assign the square root of x to
%the variable sqrt_x and print out the result. Otherwise, print out an
%error message about the argument of the square root function and set
%sqrt_x equal to zero.
if x >= 0
sqrt_x = sqrt(x);
else
fprintf('Negative numbers doesn''t have real square root. ');
sqrt_x = 0;
end
%The cost per mile for a rented vehicle is $1.25 for the first 100 miles,
%$0.75 for the next 200 miles, and $0.70 for all miles in excess of 300
%miles. Write MATLAB statements that determine the total cost and average
%cost per mile for a given number of miles (stored in the variable
%distance).
if(distance > 300)
totalCost = 1.25 * 100 + 0.75 * 200 + (distance - 300) * 0.70;
else if(distance > 100)
totalCost = 1.25 * 100 + (distance - 100) * 0.75;
else if(distance > 0)
totalCost = 1.25 * distance;
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.