Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please solve the following question \"Homework Problem 3\" in Matlab. The code s

ID: 3348424 • Letter: P

Question

Please solve the following question "Homework Problem 3" in Matlab. The code should be able to reproduce the test cases. I am including the code for evalPoly (evaluates a general polynomial) which you will need to use to complete the 2nd part of this problem. Thanks!

function [y] = evalPoly(coef, power, x)

y = 0;

for ii = 1:numel(coef)

y = y + (coef(ii)*x^power(ii));

end

end

Homework Problem 3: Calculating Roots Write a function called quadratic that evaluates the roots of a general degree two polynomial. The input for this function should be coef the coefficients in order so that ax2 + bx + is read in as a b c). The output should be roots which is a two element array where the first element is the root associated with the i the quadratic formula and the second is the root associated with the Start up a new seript file called labi hwproblem2.m. In this file, create a anonymous function called errorlnApprox that evaluates the absolute error made between the polynomial approximation of a function and the function itself. This function should have the following inputs: (a) coef the coefficients of each of the terms in the polynomia, (b) power the exponent of the corresponding term in the coefficient array, (c) func the function handle of the function represented by the polynomial stored in the coef array, (d) x - the point where you are calculating the error. Your function should use your evalPoly function from Problem 2 to compute the polynomial approximation. Your code should be able to reproduce the following test cases: >>coef-1]; >> quadratic(coef) ans 0.6180-1.618 >> coef [1 2-3]; >> quadratic(coef) ns Inside your script file lab4 hwproblem2.m, write the following lines after creating your anonymous function sine-poly = [0 1 0-1/6 0 1/120 0-1/5040]; sine-pow 0: length (sine-poly) -1; error sine 1- errorInApprox (sine poly, sine pow, sin, pi/2) error sine 2-errorInApprox (sine poly sine pow, sin, pi) You should get the following output upon running your code: lab4 hwproblem2 error_sine 1 - 1.5690e-04 error sine 2 - 0.0752 Store the code of this problem in the filenames quadratic.m and lab4 hwproblem2.m. These files must go into your zipped folder as they be evaluated.

Explanation / Answer

%Matlab code for lab4_hwproblem2.m

clear all
close all
coef=[1 1 -1];
quadratic(coef)
coef=[1 2 -3];
quadratic(coef)
sine_poly=[0 1 0 -1/6 0 1/120 0 -1/5040];
sine_pow=0:length(sine_poly)-1;
error_sine_1=errorInApprox(sine_poly,sine_pow,@sin,pi/2)
error_sine_2=errorInApprox(sine_poly,sine_pow,@sin,pi)


ans =
    0.6180   -1.6180
ans =
     1    -3
error_sine_1 =
   1.5690e-04
error_sine_2 =
    0.0752

Functions for running the previous code..

1. quadratic.m function

function a=quadratic(coef)

    a(1)=(-coef(2)+sqrt((coef(2))^2-4*coef(1)*coef(3)))/(2*coef(1));

    a(2)=(-coef(2)-sqrt((coef(2))^2-4*coef(1)*coef(3)))/(2*coef(1));

end

2. evalPoly.m function

function [y]=evalPoly(coef,power,x)

    y=0;

    for ii=1:numel(coef)

        y=y+(coef(ii)*x^power(ii));

    end

end

3. errorInApprox.m function

function error=errorInApprox(coef,power,func,x)

    y1=evalPoly(coef,power,x);

    y2=func(x);

    error=y2-y1;

end