Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For MATLAB programming: Put all answers in a MATLAB script named hw5.m Use double precision unless otherwise stated. Problem 1 : Write a function rgb

For MATLAB programming: Put all answers in a MATLAB script named hw5.m Use double precision unless otherwise stated. Problem 1: Write a function rgb color.m to display color (red, green, blue, yellow, magenta, cyan, white) as a result of mixing primary colors (red, green, blue). The function should have the following header: function [color] = rgb color(rgb) where the input rgb is a 3-element vector having a value of either 0 or 1 to denote the three primary colors (red, green, blue) respectively. The output color is a string denoting the color of the mixture. See http://en.wikipedia.org/wiki/Additive_color for information on how colors are mixed. For example, when rgb = [1 0 0], the resulting color is red. When rgb = [1 1 1], the resulting color is white. When the input rgb is not a valid input, the output color should be the string 'Invalid input'. Remember to give the function a description. Use nested if statements. (a) Set p1a=evalc('help rgb color'). (b) Set p1b=rgb color([1 1 1]). (c) Set p1c=rgb color([1 0 0]). (d) Set p1d=rgb color([0 1 0]). (e) Set p1e=rgb color([0 0 1]). (f) Set p1f=rgb color([1 1 0]). (g) Set p1g=rgb color([0 1 1]). (h) Set p1h=rgb color([1 0 1]). (i) Set p1i=rgb color([0 0 0]). Problem 2: Write a function my_stat.m to compute statistics of a vector. The function should have the following header: function [stat] = my stat(vect) where the input vect is a 3-element row vector. The function produces the statistics in the output vector stat in the following manner : The first element of stat has the median value of vect. The second element of stat has the minimum value of vect. The third (last) element of stat has the maximum value of vect. If any two elements in vect are equal, then the two equal elements are listed first in stat. If all three elements in vect are equal, then stat is equal to vect. For example, for vect = [1 3 2], stat = [2 1 3]. For vect = [1 3 3], stat = [3 3 1]. Remember to give the function a description. You can use any type of if statements as well as the built-in functions: max, min, and median. (a) Set p2a=evalc('help my stat'). (b) Set p2b=my stat([10.5 12.6 10.9]). (c) Set p2c=my stat([130 313 420]). (d) Set p2d=my stat([12 12 9]). (e) Set p2e=my stat([8 7 7]). (f) Set p2f=my stat([6 9 6 ]). (g) Set p2g=my stat([5 5 5]).

Problem 3: Write a function days_in_month.m to display the number of days in a given month. The function should have the header: function [days] = days in month(month, leap) where the input month is an all-lower-case string denoting the first three letters of the month. The input leap has logical value (0 or 1) indicating the leap year. The output days displays the number of days in the input month. February has 28 days ( 29 days in leap year). The following months have 30 days: April, June, September and November. Other months have 31 days. In cases where the inputs are invalid, the output days should be the string 'Invalid inputs'. The function should include a description. Use nested switch statements. (a) Set p3a=evalc('help days in month'). (b) Set p3b=days in month('jan',0). (c) Set p3c=days in month('feb',0). (d) Set p3d=days in month('feb',1). (e) Set p3e=days in month('apr',0). (f) Set p3f=days in month('aug',1). (g) Set p3g=days in month('oct',0). (h) Set p3h=days in month('nov',1). (i) Set p3i=days in month('Dec',0). Problem 4: Italian mathematician Fibonacci is famous for introducing the 'Fibonacci series' to modern mathematics. Any term in the Fibonacci series is the sum of the previous two terms. For example, the first 5 terms of the series are 1, 1, 2, 3, 5 Use for loop to explore the series. (a) Compute the first 40 terms of the series and put the answer in vector p4a. (b) What is the sum of all terms in the series in part (a) ? Put the answer in p4b. (c) Find the ratio of two consecutive terms in the series in part (a) and put the answer in vector p4c. Hint: set p4c(1) = 0, p4c(2)=p4a(2)/p4a(1) and so on ... Problem 5: Use for loop or nested for loops to evaluate the following series: (a) image text in transcribed Set the answer to p5a. Hint: The answer is approximately 1. (b) image text in transcribed Set the answer to p5b. Hint: The answer is approximately e 2 1. (c) image text in transcribed Set the answer to p5c. Hint: The answer is approximately 9/4. (d) X 1000 m=0 Ximage text in transcribedm n=0 1 3m+n Set the answer to p5d. Hint: The answer is approximately 27/16.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions