Homework 12 for MATLIB(Due:11:00 pm March.8) 1.Array manipulation Create a function a diagAndEdgeFilled (n) to create a n by n matrix called"a"that is zero everywhere except for ones in the first column and last column and ones in the first row and last row.There are also ones on the main diagonal of the matrix,(a(i,i)=1)for all i.Do it the MATLAB way!You should not use any iteration statement like for or while,otherwise,one point will be deducted for every iteration command you have used. 2.Matrix rotation and flipping Write 4 different functions that: Rotates a matrix in a clockwise fashion Rotates a matrix in a counterclockwise fashion Flips a matrix about its horizontal mid-line Flips a matrix about its vertical mid-line Hints and instructions. Recall that [Rows,Columns]=size(a)can be used to get the number of rows and columns of the matrix a Your functions have to work on arrays of any size Your functions do not have to provide output to the help and look for functions. Here's some sample output: >>a=rand(2,3) a Once again,you should not use any iteration 0.9218 0.1763 0.9355 command in those four functions. 0.7382 0.4057 0.9169 3.Redo the last homework 2. >disp(rotateClockWise(a)) From last homework,some of you have identified that 0.7382 0.9218 there are two roots for the function,you will get only 0.4057 0.1763 one of them depending on you initial guess.This time, 0.9169 0.9355 you are going to redo it so that it can take a vector >disp(rotateCounterClockWise(a)) containing two initial guess and get two vectors out 0.9355 0.9169 for x values and iteration steps respectively. 0.1763 0.4057 0.9218 0.7382 Hint:There is some tricky part on this assignment. >disp(flipVertical(a)) Because the iteration steps of the two guess will not 0.9355 0.1763 0.9218 be the same,which make the program much more 0.9169 0.4057 0.7382 challenge and interesting.Once you found a solution >disp(flipHorizontal(a)) for one initial guess,you should not iterate on that one 0.7382 0.4057 0.9169 anymore,you can use the following command identify 0.9218 0.1763 0.9355 the current workItemList-indices for those items you still should work on. workItemList find (abs (y(x))>=epsilon) Again,you need only to turn in the second part of the assignment as a single function
Homework 12 for MATLIB (Due: 11:00 pm March. 8) 1. Array manipulation Create a function a = diagAndEdgeFilled (n) to create a n by n matrix called “a” that is zero everywhere except for ones in the first column and last column and ones in the first row and last row. There are also ones on the main diagonal of the matrix, (a(i, i) = 1) for all i. Do it the MATLAB way! You should not use any iteration statement like for or while, otherwise, one point will be deducted for every iteration command you have used. 2. Matrix rotation and flipping Write 4 different functions that: • Rotates a matrix in a clockwise fashion • Rotates a matrix in a counterclockwise fashion • Flips a matrix about its horizontal mid-line • Flips a matrix about its vertical mid-line Hints and instructions. • Recall that [Rows, Columns] = size(a) can be used to get the number of rows and columns of the matrix a • Your functions have to work on arrays of any size • Your functions do not have to provide output to the help and look for functions. Here’s some sample output: Once again, you should not use any iteration command in those four functions. 3. Redo the last homework 2. From last homework, some of you have identified that there are two roots for the function, you will get only one of them depending on you initial guess. This time, you are going to redo it so that it can take a vector containing two initial guess and get two vectors out for x values and iteration steps respectively. Hint: There is some tricky part on this assignment. Because the iteration steps of the two guess will not be the same, which make the program much more challenge and interesting. Once you found a solution for one initial guess, you should not iterate on that one anymore, you can use the following command identify the current workItemList – indices for those items you still should work on. workItemList = find (abs (y(x)) >= epsilon) Again, you need only to turn in the second part of the assignment as a single function
4.Stars and Stripes forever At the end of this problem description,parts of a script M-file are given.This program may be used to draw the US flag.The US flag has 13 horizontal rows,alternating with red and white.Row 1 is red, row 2 is white,and so on,for 7 red rows and 6 white ones.A solid blue rectangle occupies the upper left corner,from rows 7 to 13 and it is half the width of the flag.There are 50 white stars arranged in the blue patch. Here's how you complete the code to finish the flag program. ·Write a function flag createFlagStrips O that will make the stripes and the blue patch.(Red value 0.15,blue has value 0.4,and white has value 0.0.)Plotted as indicated it should look like: Read through the rest of the MATLAB code and understand what it is trying to do.The bottom part is for placing the stars. Write the function plotStar(x,y,r).x and y are the points that define the center of the star,and r is its radius.Notice that the 5 points on a star are all located on a circle centered at x and y with radius r.If you drew lines from the center to the points of the star,those lines would be separated by 72 degrees,(72=360/5).To draw the star,imagine that the top point of the star is numbered 1, the one to its right 2,ands o on.You can make a star easily by drawing a line from point 1,to 4,to 2,to 5,to 3 and back to 1.Here's an example of plotStar(1,3,2). You might find it useful to use the sin (and cos (functions.The argument to these functions should be expressed in radians.In MATLAB,the conversion from degrees to radians is given by: rads=pi*a/180,where a is an angle in degrees and rads is the same angle expressed in radians. You should use the plot (function using white solid lines.Here are the details: PLOT Linear plot.PLOT(X,Y)plots vector Y versus vector X
4. Stars and Stripes forever At the end of this problem description, parts of a script M-file are given. This program may be used to draw the US flag. The US flag has 13 horizontal rows, alternating with red and white. Row 1 is red, row 2 is white, and so on, for 7 red rows and 6 white ones. A solid blue rectangle occupies the upper left corner, from rows 7 to 13 and it is half the width of the flag. There are 50 white stars arranged in the blue patch. Here’s how you complete the code to finish the flag program. • Write a function flag = createFlagStrips () that will make the stripes and the blue patch. (Red value 0.15, blue has value 0.4, and white has value 0.0.) Plotted as indicated it should look like: • Read through the rest of the MATLAB code and understand what it is trying to do. The bottom part is for placing the stars. • Write the function plotStar (x, y, r). x and y are the points that define the center of the star, and r is its radius. Notice that the 5 points on a star are all located on a circle centered at x and y with radius r. If you drew lines from the center to the points of the star, those lines would be separated by 72 degrees, (72 = 360/5). To draw the star, imagine that the top point of the star is numbered 1, the one to its right 2, ands o on. You can make a star easily by drawing a line from point 1, to 4, to 2, to 5, to 3 and back to 1. Here’s an example of plotStar (1, 3, 2). • You might find it useful to use the sin () and cos () functions. The argument to these functions should be expressed in radians. In MATLAB, the conversion from degrees to radians is given by: rads = pi*a/180, where a is an angle in degrees and rads is the same angle expressed in radians. • You should use the plot () function using white solid lines. Here are the details: PLOT Linear plot. PLOT(X,Y) plots vector Y versus vector X
Various line types,plot symbols and colors may be obtained with PLOT(X,Y,S)where S is a character string made from one element from any or all the following 3 columns: b blue point -solid 5 4.5 -0.5 0.5 1.5 2.6 green 0 circle dotted red x-mark dashdot cyan plus dashed For example,PLOT(X,Y,'c+:')plots a cyan dotted line with a plus Function plotusflag ( &This function will first plot a Us flag without stars sit then plot specified stars on the Us flag flag createFlagStrips O &Show the flag without stars using pcolor show flag; show(end+1,1)=0;&Hidden point,fixes the lower bound of the colormap show(end,end+1)=1;SHidden point,fixes the upper bound of the colormap pcolor(1:41,1:14,show) colormap vga shading flat axis off hold on %This code prompts the user to place the stars.. response input('Specify parameter for a star [x,y,size]->);
Various line types, plot symbols and colors may be obtained with PLOT(X,Y,S) where S is a character string made from one element from any or all the following 3 columns: b blue . point - solid For example, PLOT(X, Y, ’c+:’) plots a cyan dotted line with a plus Function plotUSflag () %This function will first plot a US flag without stars %it then plot specified stars on the US flag flag = createFlagStrips () %Show the flag without stars using pcolor show = flag; show(end+1,1) = 0; %Hidden point, fixes the lower bound of the colormap show(end,end+1) = 1; %Hidden point, fixes the upper bound of the colormap pcolor(1:41,1:14,show) colormap vga shading flat axis off hold on %This code prompts the user to place the stars... response = input('Specify parameter for a star [x, y, size] -> ');
x=response(1);y response(2);r=response(3); %call plotStar (to Plot a star using the algorithm specified before %the data for the five lines is returned as a [6,2]arrays.Each row %contains x and y coordinates of one end point of the star. star plotStar(x,y,r); Sample Run >plotusflag Specify parameter for a star [x,y,size]-[10,10,3] What to turn in: function flag=createFlagStrips (as sYourID hwk2 4 1.m .function star=plotStar(x,y,r)as s YourID_hwk2 4_2.m Notes for submission: You should save your homework in an m file and submit your homework to the Sakai using attachment.You should name you file in the following matter.The format is important,if you did not follow this rule,your homework may not be able to be grated correctly or on time. sYourlD_hwk2_x.m Example s5073709022hwk2X.m
x = response(1); y = response(2); r = response(3); %call plotStar () to Plot a star using the algorithm specified before %the data for the five lines is returned as a [6, 2] arrays. Each row %contains x and y coordinates of one end point of the star. star = plotStar(x,y,r); Sample Run What to turn in: • function flag = createFlagStrips () as sYourID_hwk2_4_1.m • function star = plotStar (x, y, r) as sYourID_hwk2_4_2.m Notes for submission: You should save your homework in an m file and submit your homework to the Sakai using attachment. You should name you file in the following matter. The format is important, if you did not follow this rule, your homework may not be able to be grated correctly or on time. sYourID_hwk2_x.m Example : s5073709022_ hwk2_x.m