%% CSCI_1190 Quiz_3 for Fall 2016

% Your Name:      Shuxian Zhao

%% Q1(3%) Write a command to create a 3 x 2 array with any values you choose.

% Place your answer here:

a=[1 2;3 4;5 6]

%% Q2(4%)

% Use A, B, and C below to create vector Y and

% output to the command window as follows:

%

% Y =[1 3 5 7 11 13 17 19 23]

%

A=[1 3 5];

B=[7 11 13];

C=[17 19 23];

% Place your answer here:

Y= [A B C]

%% Q3(5%)

M1=magic(5)

% The above magic command generates the following array M1

%   M1 =

%    17    24     1     8    15

%    23     5     7    14    16

%     4     6    13    20    22

%    10    12    19    21     3

%    11    18    25     2     9

%

% Write a command to slice M1 to get the array M2 as follows:

%

% M2 =

%     6    22

%     18    9

M2=M1([3 5],[2 5])

%% Q4 (5%)

A = magic(10)

% Write a command to output all elements of the third column form A in the Command Window.

A(1:end, 3)

%% Q5 (5%)

clear, clc

Y =[ 2:2:7; 6:2:11; 11:2:15]

% Write a command to change the value 8 to 18 in the matrix Y

% using the subscripts of the array.

% Write your answer here:

Y(2,2)=18

%% Q6 (5%)

E = [3 4 5 6 7]

% Write a command to raise each element of E to power 3.

% Write your answer here:

E=E.^3

%% Q7 (3%)

% Write the MATLAB command that displays variables in the Workspace,

% along with each variable’s size, bytes, and class?

% Write your answer here:

whos

%% Q8 (8%)

% Write a MATLAB code to implement the following mathematical expression:

%

%   E= ln(15) x e^2 x  sin(60) x log(100)

%

% Note 1: 60 is an angle in degree

% Note 2: “ln(15)” refers to a natural logarithm.

% Note 3: log(100) refers to a base 10 logarithm.

%  Write your answer here:

E=log(15)*exp(2)*sind(60)*log10(100)

%% Q9(5%)

% A vector M is given as follows:

M = [ 5, -1, 4, -10, 20, 0, -50, 35];

% Write a MATLAB command to display the smallest value of M and its index.

% Write your answer here:

[M,I] = min(M)

%% Q10(10%)

% In this question, you first create a theta vector using the range operator.

% The theta’s first value is 0, the increment is 0.1, and the ending value is

% 2*pi.  Then you plot two circles as shown in the separated file Quiz3.pdf.

% Place your answer here:

theta=[0:0.1:2*pi];

x=1*cos(theta);

y=1*sin(theta);

figure(1);

subplot(1,2,1);

plot(x,y);

subplot(1,2,2);

plot(x,y,’ro’);

%% Q11

% Write a function to convert temperatures between Celsius

%and Fahrenheit.

% The detailed requirements:

% The function name: tconv

% The function takes two parameters: degree and scale(F

% or C)

% The function returns a converted temperature

% The tconv function provides the help message as shown

% in the above

% three lines.

% If the input scale character is F, convert the degree

%to its C scale

% The formula converting a Fahrenheit to its Celsius

% counterpart:

% ¡ãc = (¡ãf – 32) x 5/9

%

% The formula converting a Celsius to its Fahrenheit

% counterpart:

% ¡ãf = ¡ãc x 9/5 + 32

%% Q12(12%) This question is related to the above question Q11.

% The following three steps are expected to write a script:

% Step 1: Write commands to input a temperature value and a scale letter,

%         and prompt with the following text:

%

%       ‘Please input a temperature degree:  ‘

%       ‘Please input a temperature scale F or C:  ‘

%

%  write your code here:

d=input(‘Please input a temperature degree:’);

s=input(‘Please input a temperature scale F or C:’,’s’);

% Step 2: Call tconv function you implemented in Q11 for conversion.

% Place your code here:

[Temperature,Scale]=tconv (d,s);

% Step 3:

% Use fprintf to display the result in the Command Window.

%

% The output examples:

% 60.000000 F = 15.555556 C

% or

% 20.000000 C = 68.000000 F

%

% Note: You may need to use the if … else statment to dynamically decide

% the output scale letter as shown in the above output examples.

%

fprintf(‘%f %s = %f %sn’,d,s,Temperature,Scale);

%% Q13(15%)

% The “scores” array below has test scores.

% Use a for loop, and if … else statment for following requirements:

%   Display ‘A’ if a score if greater than or equal to 90

%   Display ‘B’ if a score if greater than or equal to 80

%   Display ‘C’ if a score if greater than or equal to 70

%   Display ‘D’ if a score if greater than or equal to 60

%   Display ‘F’ otherwise

%

% Use the fprintf command to output the following:

% 1. The number of students who got B above.  Example:

%       “20 students among total 35 got B above.”

% 2. The percentage of the students who passed the exam

%    if his/her score is greater than or equal to 60. Example:

%       “Over 94.285714 percent students passed the exam.”

clear, clc

scores = [95, 83, 83, 95, 83, 76, 65, 85, 100, 65, 82, 95, …

                 66, 85, 95, 72, 82, 75,  60, 65, 66, 90, 55, …

                 100, 86, 75, 50, 90, 65, 80, 75, 66, 90, 86, 100];

m=0;

n=0;

P=0;

for i=1:35

     if scores(i)>=90;

         disp(‘A’);

         m=m+1;

         P=P+1;

     elseif scores(i)>=80 && scores(i)<90;

        disp(‘B’);

        n=n+1;

        P=P+1;

     elseif scores(i)>=70 && scores(i)<80;

        disp(‘C’);

        P=P+1;

     elseif scores(i)>=60 && scores(i)<70;

        disp(‘D’);

        P=P+1;

     else

         disp(‘F’);

     end

end

fprintf(‘%d among total 35 got B above.n’,m+n);

percentage=(P/35)*100;

fprintf(‘Over %f percent students passed the exam.’,percentage);

%% Q15(8%)

%

% The ODE equation:   dy/dx = 3x

% The initial value:  y(-10) = -5

% The interval:       [-10, 10]

% Requirements:

% 1. Write an anonymous function for the ODE

% 2. Apply the ode45 solver

% 3. Plot the numerical solution in blue color

% 2. Display the elapsed time for running your code.

%

% Write your code here:

func = @(X,Y)3*X;

interval=[-10 10];

y0=-5;

[x,y] = ode45(func,interval,y0);

plot(x,y,’b-‘);

%% Optional question (6% extra credit)

% Write sybolic math expression to calculate the area of a circle with a

% radius r. You also want to substitute the symbolic variable r with a number,

% for example 5, and display the computed area’s real value in the Command

% Window.

syms r

area = r^2 *pi ;

subs(area, r, 5)