Enquires about matlab questions & i’m not sure how to do it, and whether if the answer that i’ve done is correct

  Kiến thức lập trình

I need help with the questions stated below, and I need answers asap as this is assignment that’ll be due this week.

first question is stated in the picture.

enter image description here

Guideline #1 for question 1

Guideline #2 for question 1

Guideline #3 for question 1

This was a guideline given to me by my teacher for question 2

question 2
a) Write down the MATLAB conversion program that prompts user to input
number as any integer and units as string.

  • If the user input units as inch, then the given number is converted to
    centimetres using the formula centimetres = number * 2.54.
  • If the user input units as metre, then the given number is converted to
    centimetres using the formula centimetres = number * 100.
  • If the user input units as any other string, then display unit is unknown.

(b) Write a MATLAB program to calculate the taxi fare for the following:

There are two types of taxis:
• Online taxi: It can be booked by using an online app from phones
• Classic taxi: It can be booked anywhere on the road
The online taxis cost Oc for the first km and Od for every km afterward. The
classic taxis travel at a speed of Cs km per minute. The costs of classic taxis
are Cb, Cm, and Cd that represents the base fare, cost for every minute that
is spent in the taxi, and cost for each kilo meter that you ride.
You are going to the school from your home. Your task is to minimize the cost
that you are required to pay. The distance from your home to the school is D.
You are required to select whether you want to use online or classic taxis to
go to your school. If both the taxis cost the same, then you must use an online
taxi.

I did try doing question 2, but not sure how to do the first question. Here’s my answer but i’m not sure if it’s correct.

part a:

`num = input(‘Enter a number: ‘);
units = input(‘Enter units (inch or metre): ‘, ‘s’); %s = input as string

if strcmpi(units, ‘inch’) %check if units are inches
centimetres = num * 2.54;
disp([‘The conversion from inches to centimetres is: ‘, num2str(centimetres)]);
elseif strcmpi(units, ‘metre’)
centimetres = num * 100;
disp([‘The conversion from metres to centimetres is: ‘, num2str(centimetres)]);
else
disp(‘Unit is unknown.’);
end
`

part b:

D = input('Enter the distance from your house to the school (in km): ');
Cs = input('Enter the base fare for the classic taxi: ');
Cb = input('Enter the cost per minute for the classic taxi: ');
Cm = input('Enter the cost per kilometer after the first km of the classic taxi: ');
Cd = input('Enter the cost per kilometer for the online taxi: ');
Oc = input('Enter the cost of the first km for the online taxi: ');
Of = input('Enter the fixed distance for the online taxi: ');
Od = input('Enter the cost of every km afterwards for the online taxi: ');


%Calculate cost of classic taxi
T = D / Cs;   %calculate time taken for classic taxi (T) using the speed (Cs) and distance (D)
classicFare = Cb + (T * Cm) + (D * Cd);  %calculate total fare for classic taxi


%Calculate cost of online taxi
onlineFare = Oc + max(0, D - Of) * Od;   %calculate fare for online taxi 

%Compare and find which is the cheaper option
if onlineFare <= classicFare
    disp('Online Taxi');
else
    disp('Classic Taxi');
end


%Output of the chosen taxi & corresponding fare
if onlineFare <= classicFare
    disp(['Total fare for Online Taxi is: ', num2str(onlineFare)]);
else
    disp(['Total fare for Classic Taxi is: ', num2str(classicFare)]);
end`

New contributor

Cheryl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT