Write the script or function m-file to compute S = sigma_n=1^M n/n+1, where sigm
ID: 3787965 • Letter: W
Question
Write the script or function m-file to compute S = sigma_n=1^M n/n+1, where sigma_n=1^M indicates the summation from n = 1 to M and run the case M = 10. (a) Using for/end, but not array operators or sum. This is the standard. (b) Using array operators and sum. but not for/end Submit the codes and the numerical results. Write a function m-file that calculates the roots of ax^2 + bx + c = 0. Its synopsis is Quad Roots(a, b, c), where a, b and c are allowed to be vectors. First, use the example below to get the results (and show them), then give your own input of coefficient vectors a, b and c and perform the calculation and show the results. Finally, check your results with the roots command, which is a built-in MATLAB command to do the job. For example >> a=[1, 2, 3, 5, 5]; >> b=[2, 2, 4, 5, 6]; >> c=[3, 4, 4, 6, 7]; will produce the coefficients of five quadratic polynomials: {x^2 + 2x + 3 = 0 2x^2 + 2x + 4 = 0 3x^2 + 4x + 4 = 0 5x^2 + 5x + 6 = 0 5x^2 + 6x + 7 = 0} When you issue the command >> QuadRoots(a, b, c) you will get five set of roots of these five equations. The size of coefficient vectors a, b and c must be the same (in this case 1 times 5) but could be arbitrary (does not have to be 5 as above). The roots of a quadratic polynomial, as defined here, are given by the formula x = -b plusminus Squareroot d/2a, where the discriminant is defined by d = b^2 - 4ac. Depending on the sign of the discriminant you get either the real of complex roots. Therefore you need to include a segment of codes that distinguishes the case when d = b^2 - 4ac is positive, negative and zero. You can use if, else if else, end construct for this. Note that, when d is negative, MATLAB automatically produces complex roots, but do not rely on this capability and use the form x = -b plusminus i Squareroot -d/2a instead, where i is the imaginary number. Develop a function m-file that returns a letter grade (either A, B, C, D or F) given a numeric grade x according to: (A, B, C, D, F) for (90Explanation / Answer
1 (a)
function []=sum_function(M)
sum=0;
for n=1:M
sum=sum+(n/(n+1));
end
disp('The sum of m values');
sum
end
1(b)
m=10
A=[1:m]
ratio=bsxfun(@rdivide,A,A+1);
sum_ratio=sum(ratio)
2.
function [x1_real,x2_real,x1_imag,x2_imag]=QuadRoots(A,B,C)
%File: Quadratic_roots.m
%Purpose:
%This program finds the roots of the quadratic equation. It determines
%wether the roots are real or imaginery. It outputs regardless of the type
%of roots that the equation posses.
%
%Variables:
%A - Coefficient of x^2 term.
%B - Coefficient of x term.
%C - Constant term
%Discriminant - Discriminant of equation.
%x1 - First Solution.
%x2 - Second Solution
sz=size(A);
x1=[];
x2=[];
x1_real=[];x2_real=[];x1_imag=[];x2_imag=[];
for i=1:sz(2)
%Discriminant Equation.
Discriminant=B(i)^2-4*A(i)*C(i);
%Check if it is real or imaginary roots.
if(Discriminant>0)
disp('Roots are Real');
elseif(Discriminant==0)
disp('Roots are Real and Equal');
else
disp('Roots are Imaginery');
end;
%Roots of the equation are:
x1(i)= (-B(i) + sqrt(Discriminant))/(2*A(i));
x2(i)= (-B(i) - sqrt(Discriminant))/(2*A(i));
%result
x1_real(i)=real(x1(i));
x1_imag(i)=imag(x1(i));
x2_real(i)=real(x2(i));
x2_imag(i)=imag(x2(i));
end
3.
function result=letter_grade(x)
% Set up grades
grades = 'ABCDF';
%Grades min score is stored in this array
gradeMinScore = [90,80,70,60,0];
% Identifing grade for each score
result = grades(...
arrayfun(@(g) find(g>=gradeMinScore,1,'first'),x));
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.