H20 Find Topics: User defined Furretions command An Instructor has requested you
ID: 3917935 • Letter: H
Question
H20 Find Topics: User defined Furretions command An Instructor has requested you write two functions to adjust the grades based on the current score that will be sent to the function in a scalar original score and the new score. It wll return a scalar or a vector (same as sent to the function) with the adjusted score. These functions wll be call Sample for and Sample find. Both functions will adjust the scores for the returned value as follows: or a vector. I will then print a report that will show the Score 0-25 25-50 50-75 75-100 New Score +10% +8% +5% +354 0or 100 0% Sample for MUST use a for loop' and Sample find MUST use the find command. 1. To test your functions you will have to create a vector to send to the functions. Use x =-20: 10: 120; %creates a vector to test your functions. Then call them using the following commands: Sample for(x); Sample find(x); Notes(s): Sample Run(s): Upload BOTH your Function files (Sample for and Sample find) D2L's (Program 05) Dropbox >> x =-20: 10:120 >Sample_findx) Sample find Results Score Adjusted Score 20.00 10.00 20.00 10.00Explanation / Answer
------------------------Sample_for.m---------------------
function [arr] = Sample_for(vec)
% create vector of same dimension as vec
arr = [1 : length(vec)];
% traverse through vec
for i = 1 : length(vec)
% if score is in range 0 - 25
if vec(i) >= 0 && vec(i) < 25
% new score is 10% more than current score
arr(i) = 1.10 * vec(i);
% if score is in range 25 - 50
elseif vec(i) >= 25 && vec(i) < 50
% new score is 8% more than current score
arr(i) = 1.08 * vec(i);
% if score is in range 50 - 75
elseif vec(i) >= 50 && vec(i) < 75
% new score is 5% more than current score
arr(i) = 1.05 * vec(i);
% if score is in range 75 - 100
elseif vec(i) >= 75 && vec(i) < 100
% new score is 3% more than current score
arr(i) = 1.03 * vec(i);
% if score is <0 or >= 100
elseif vec(i) >= 100 || vec(i) < 0
% new score is 0% more than current score
arr(i) = vec(i);
end
end
end
-------------------------Sample_find.m------------------------
function [arr] = Sample_find(vec)
% create vector of same dimension as vec
arr = [1 : length(vec)];
% find index of elements in range 0 - 25
index = find( arr >= 0 & arr < 25 );
% if score is in range 0 - 25
for i = 1 : length(index)
% new score is 10% more than current score
arr(index(i)) = 1.10 * vec(index(i));
end
% find index of elements in range 25 - 50
index = find( arr >= 25 & arr < 50 );
% if score is in range 25 - 50
for i = 1 : length(index)
% new score is 8% more than current score
arr(index(i)) = 1.08 * vec(index(i));
end
% find index of elements in range 50 - 75
index = find( arr >= 50 & arr < 75 );
% if score is in range 50 - 75
for i = 1 : length(index)
% new score is 5% more than current score
arr(index(i)) = 1.05 * vec(index(i));
end
% find index of elements in range 75 - 100
index = find( arr >= 75 & arr < 100 );
% if score is in range 75 - 100
for i = 1 : length(index)
% new score is 3% more than current score
arr(index(i)) = 1.03 * vec(index(i));
end
% find index of elements in range <0 or >= 100
index = find( arr < 0 | arr >= 100 );
% if score is <0 or >= 100
for i = 1 : length(index)
% new score is 0% more than current score
arr(index(i)) = vec(index(i));
end
end
---------------------------main.m-------------------------------
x = [ -20 : 10 : 120 ];
arr1 = Sample_for(x);
arr2 = Sample_find(x);
fprintf('%10s %20s %20s ' , 'Score' , 'Adjusted Score 1' , 'Adjusted Score 2');
for i = 1 : length(x)
fprintf('%10f %20f %20f ', x(i) , arr1(i) , arr2(i));
end
Sample Output
Score Adjusted Score 1 Adjusted Score 2
-20.000000 -20.000000 -20.000000
-10.000000 -10.000000 -10.000000
0.000000 0.000000 0.000000
10.000000 11.000000 11.000000
20.000000 22.000000 22.000000
30.000000 32.400000 32.400000
40.000000 43.200000 43.200000
50.000000 52.500000 52.500000
60.000000 63.000000 63.000000
70.000000 73.500000 73.500000
80.000000 82.400000 82.400000
90.000000 92.700000 92.700000
100.000000 100.000000 100.000000
110.000000 110.000000 110.000000
120.000000 120.000000 120.000000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.