Machine Problem 5-8

Share Embed Donate


Short Description

Descripción: MATLAB...

Description

JAN MARIE A. DATING ChE 517 – Computer Applications in Chemical Engineering MHARK LOUIE T. SARDAÑA 06 October 2016 BS Chemical Engineering V MACHINE PROBLEM 5 Write a function called fare that computes the bus fare one must pay in a given city based on the distance travelled. Here is how the fare is calculated: the first mile is $2. Each additional mile up to a total trip distance of 10 miles is 25 cents. Each additional mile over 10 miles is 10 cents. Miles are rounded to the nearest integer other than the first mile which must be paid in full once a journey begins. Children 18 or younger and seniors 60 or older get a 20% discount. The inputs to the function are the distance of the journey and the age of the passenger in this order. Return the fare in dollars, e.g., 2.75 would be the result returned for a 4-mile trip with no discount. SOLUTION: Code:

end

function f = fare( distance, age )

end

if (distance >0 && distance 10 f = f + (miles-10)*0.1; miles = 10; end f = f + (miles-1)*0.25 + 2; if (age=60) f = f*0.8;

Output:

JAN MARIE A. DATING ChE 517 – Computer Applications in Chemical Engineering MHARK LOUIE T. SARDAÑA 06 October 2016 BS Chemical Engineering V MACHINE PROBLEM 6

The function move_me is defined like this: function w = move_me(v,a). The first input argument v is a row-vector, while a is a scalar. The function moves every element of v that is equal to a to the end of the vector. For example, the command >> x = move_me([1 2 3 4],2); makes x equal to [1 3 4 2]. If a is omitted, the function moves occurrences of zeros.

SOLUTION: Code: function x = move_me( a, s ) if nargin==1 e = 0; else e = s; end x = a(a~=e); y = a(a==e); x = horzcat(x,y); end

Output:

JAN MARIE A. DATING ChE 517 – Computer Applications in Chemical Engineering MHARK LOUIE T. SARDAÑA 06 October 2016 BS Chemical Engineering V MACHINE PROBLEM 7

Write a function called centuries that takes a positive integer smaller than or equal to 3000 representing a year as its input and returns a string with the century the given year falls into. If the input is invalid, the function returns the empty string. Centuries are specified using roman numerals. Note that we require the shortest legal roman number. For a complete list, refer to: http://www.romannumerals.co/roman-numerals-1-to-30. Note that a century goes from year 1 to 100, so for example, the XX th century ended on December 31st, 2000. As an example, the call >> cent = centuries(1864); will make cent equal to ‘XIX’. SOLUTION: Code:

a{3} = 'III';

function cent = centuries( y )

a{4} = 'IV';

cent ='';

a{5} = 'V';

if ~isscalar(y) || y ~=fix(y)

a{6} = 'VI';

cent = '';

a{7} = 'VII';

return;

a{8} = 'VIII';

end

a{9} = 'IX';

if y =1

one = ''; if mod(year, 10) ~=0

cent = 'I';

one = a{mod(year, 10)};

return; end;

end;

year = ceil(y/100);

if y > 1 && y
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF