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

Write a Matlab program that will allow the user to enter the coefficients of a p

ID: 3849512 • Letter: W

Question

Write a Matlab program that will allow the user to enter the coefficients of a polynomial and make some calculations. Selections and data entry will be done by presenting the user with a menu (using menu function) and provide her/him with the following choices:

Enter the degree and coefficients of the polynomial

Evaluate the polynomial at a user defined point

Display the roots of the polynomial

Exit

Other Requirements:

Selecting choice 2 and 3 before entering the degree and coefficients will result in an error message.

The program will terminate only when Exit has been selected

Calculations will be done by functions.

o Function polynomial() will ask for the degree and the coefficients of the polynomial and would return the coefficients.

o Function polyEval() will accept a parameter, the evaluation point, and returns the value of the polynomial at that point.

o Function polyRoots() will accept the coefficients and returns the roots. Results are displayed with 2 decimal place accuracy.

Explanation / Answer

Matlab Code:

Main Script:

clc

status=1;

plyset=0;

while status

disp('MENU');

disp('1: Enter the degree and coefficients of the polynomial');

disp('2: Evaluate the polynomial at a user defined point');

disp('3: Display the roots of the polynomial');

disp('4: EXIT');

inp=input('Enter your choice: ');

x;

switch inp

    case 1

        x=Polynomial()

        polyset=1;

    case 2

        if polyset == 0

            disp('Error, input the polynomial first!!!');

        else

            inp=input('Enter the evaluation point: ');

            result=polyEval(inp, x)

        end

    case 3

         if polyset == 0

            disp('Error, input the polynomial first');

        else

        root=polyRoots(x);

         end

       

    case 4

        status =0;

end

end

Auxiliary functions: function to read input

function coe= polynomial()

global y

degree=input('Enter the degree: ')

disp('Enter the coefficients of polynomial');

i=1;

while i<=degree+1

coeff(i)=input(' ');

i=i+1;

end

coe=coeff;

end

function to evaluate the polynomial

function result= polyEval(point, x)

result=polyval(x,point);

end

function to find the root

function r=polyRoots(y)

    r = roots(y)

end

Output:

MENU

1: Enter the degree and coefficients of the polynomial

2: Evaluate the polynomial at a user defined point

3: Display the roots of the polynomial

4: EXIT

Enter your choice: 1

Enter the degree: 2

degree =

     2

Enter the coefficients of polynomial

1

4

4

x =

     1     4     4

MENU

1: Enter the degree and coefficients of the polynomial

2: Evaluate the polynomial at a user defined point

3: Display the roots of the polynomial

4: EXIT

Enter your choice: 3

r =

    -2

    -2

MENU

1: Enter the degree and coefficients of the polynomial

2: Evaluate the polynomial at a user defined point

3: Display the roots of the polynomial

4: EXIT

Enter your choice:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote