MATLAB HELP A professor will assign grades to his class as follows: any score mo
ID: 2962508 • Letter: M
Question
MATLAB HELP
A professor will assign grades to his class as follows: any score more than 15 points above average will receive an A, from 0-15 points (inclusive) above average will receive a B, from 0-15 points below average will receive a C, and more than 15 points below average will receive a D. Write a Matlab function called grades .m which will take a single input, which is an array of grades, and return 4 outputs. The first output is the number of A's in the class, the second is the number of B's, the third is the number of C's, and the fourth is the number of D's. As a check, the Matlab commands t=l:140; [A,B,C,D]=grades(75+25*cos(t) .*sin(3*t)) should give 20 A's, 57 B's. 38 C's, and 25 D's. (Hopefully the grade distribution at the end of this course will be somewhat better!) Hints: Remember that the Matlab mean command will calculate the average of an array of numbers. You will need a loop with a nested conditional to do the counting. No simple formulae exist to compute the zeros of a general function f(x) (i.e. the values of x for which f(x) = 0). This includes even polynomial functions of degree greater than 4. Instead, numerical methods must be used to find these zeros. We have previously considered a "brute force" approach to this problem: compute f(x) on a large range of values for x, and look for the one which results in f(x) being close to zero. In addition to being inelegant, this approach requires a large amount unnecessary computation (and hence time). It is far more efficient, and elegant, to use a more sophisticated "guess and check" algorithm to "home in" on a value of x for which f(x) = 0, using as few computations as possible. One common algorithm is known as Newton's method., which can be summarized as follows: Start with an (essentially arbitrary) initial guess z for one of the zeros of the function f(x). Compute f(z). If f(z) = 0 then z is a zero and we are done. Of course, it is unlikely that the initial guess for z really is a zero, so generally f(z) = 0. In this case, Newton's method generates a new, "improved" guess for the zero z according to the formula In this formula the "old" guess for z is used on the right-hand side of the equation, and the result is used to define the new guess for 2. We then check this new z to see if f(z) = 0, and if not repeat the above calculation with the new z to generate yet another new z, continuing in this fashion as long as necessary until f(z) = 0. Of course, the finite precision of the computer means that we can't really continue the process of generating better and better guesses until f(z) is exactly zero, as this would require essentially infinite time. Rather, must settle for zeros which satisfy |f{z)|Explanation / Answer
1) function [y]= grades(x)
avg=mean(x);
y(1)=0;
y(2)=0;
y(3)=0;
y(4)=0;
for i=1:length(x)
if(x(i)>avg+15)
y(1)=y(1)+1;
else if(x(i)>=avg)
y(2)=y(2)+1;
else if(x(i)>=avg-15)
y(3)=y(3)+1;
else
y(4)=y(4)+1;
end
end
disp('As');
disp(y(1));
disp('Bs');
disp(y(2));
disp('Cs');
disp(y(3));
disp('Ds');
disp(y(4));
2) function [y]=polyroot(p)
format long;
init=-1;
old=init;
new=init;
tol=10^(-10);
while(abs(polyval(p,new))>=tol)
old=new;
new= new - (polyval(p,new)/polyval(polyder(p),new);
end
y=new;
3) function [y]=kepler(e,n)
format long;
init=pi/2;
old=init;
new=init;
tol=10^(-10);
while(abs(new-e*sin(new)-2*n*pi)>=tol)
old=new;
new= new - (new-e*sin(new)-2*n*pi)/(1-e*cos(new));
end
y=new;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.