Write a script to calculate and plot the polynomials and the
Write a script to calculate and plot the polynomials, and then perform simple arithmetic on them. Use the following polynomials, and x between -1.5 and 1.5. Programming note: You\'ll be using the x limit values (-1.5 and 1.5) in quite a few places; best to make them variables so that you can change them easily later if you want. y1 = 0.1x^4 - 0.2x^2 + 0.5 y2 = 2x^3 - x^2 + 10x - 5 y3 = 2 - 1 First, declare all three polynomials in a script. Don\'t forget the zeros for the missing terms. The highest power is the first element in the vector. Now plot y1 and y2 in the same window. There\'s two ways to plot. You can declare your x values using linspace, then use polyval to create corresponding y values, then plot as normal yvalues = polyval (polynomial. xvalues) is OR, you can use fplot and create an anonymous function that calls polyval. In this case, you pass the x values to polyval, and hard-wire the polynomial in. Now create a new polynomial, y1 + y2. Don\'t forget the leading zeros to make the vectors be the same size. Plot the new polynomial in the same window. Now calculate the following and use disp to print the polynomial coefficients; you don\'t need plot these three (unless you want to). Use fprintf before the disp to print out which equation you\'re solving y3 - y1 y1 middot y3 (conv is the command you want y2/y3 (deconv is the command you want. Extra credit Write a function (as a function file) that prints out a polynomial in the correct form (similar to above, ie y(x) = 3x^3 + 5x^2 + ...). Use this function instead of fprintf and disp to print out your polynomials. Selfcheck: Add y1 to y2: 0.1xxx 2 xxx -x.2xxx xxxxxx -xxxx Subtract y1 from y3: -x.lxxx x 0.2xxx xxxxx -1 xxxx Multiply y1 by y3 x.2xxx -x.1xxx -x.xxxx xxxxx xxxxx -xxxxx Divide y2 by y3: 1 times 5
Solution
y1=[0.1 0 -0.2 0 0.5]
y2=[ 2 -1 10 -5]
y3=[2 -1]
xminlimit= -1.5
xmaxlimit=1.5
xvalues=linspace(xminlimit,xmaxlimit)
y1values=polyval(y1,xvalues)
y2values=polyval(y2,xvalues)
%y4=y1+y2
y4=[0.1 2 -1.2 10 -4.5]
y4values=polyval(y4,xvalues)
plot(xvalues,y1values,xvalues,y2values,xvalues,y4values)
%y5=y3-y1
y5=[-0.1 0 0.2 2 -1.5]
fprintf(\'subtract y1 from y3\')
disp(y5)
fprintf(\'multiply y1 by y3\')
disp(conv(y1,y3))
fprintf(\'divide y2 from y3\')
disp(deconv(y2,y3))
%to use deconv the first elements of y2 and y3 must be nonzero
%note some values in your ouput is wrong
