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

As we saw in class, Matlab also uses arrays to represent and manipulate polynomi

ID: 3887916 • Letter: A

Question

As we saw in class, Matlab also uses arrays to represent and manipulate polynomial functions. The general nth order polynomial where ai are constants, is represented in Matlab using the corresponding coefficient array p- [an an-1 ai ao], 2x3 + 4x2-x+ 7 would be represented in Matlab using the array so, for example, f(x) p=[2 4-1 7] The polyval function will evaluate a polynomial (represented by its coefficient array) at specified points, the roots function will give you an array containing all the roots of the polynomial, and the polyfit function will give you an array of coefficients for a polynomial that best fits the trends seen in a specified set of datapoints. Refer to lecture notes as needed to use these functions to solve the next two problems below: Write a script called polyplot which will accomplish the following: assuming the coefficients of the desired polynomial have already been defined in the workspace, the script should produce a graph of the function. The range of r values used in the graph should extend from one less than the smallest root, to one more than the largest root, with a spacing of 0.01. We will only test your code with polynomials that have all real roots, so you won't need to worry about complex numbers here. The script should then overlay on this plot red circles at every point where the graph crosses 0. Finally, have the script also place a grid on the plot, and some appropriate axis labels As an example, if p=[2 0-8] is defined in the workspace, running your polyplot script should produce an upward pointing parabola, with red circles labeling the zero crossings at x-12, and the x-axis values of the graph should span from -3 to 3. The TAs will check your script performance for a different p array.

Explanation / Answer

function polyplot(p)
    r = roots(p);
    minRoot = min(r)
    maxRoot = max(r)
    x = (minRoot-1) : 0.01 : (maxRoot+1);
    y = polyval(p, x);
    y2 = polyval(p, r);
  
    plot(r, y2, 'o');
    hold on
    plot(x,y,'-');
    hold off
end

p = [2 0 -16]
polyplot(p)

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