Exercises
Please complete each exercise or answer the question before reviewing the posted solution comments.
-
Define a function that computes the polynomial: y = Ax2 + Bx + C and test it with some known values for A, B, C, and x. Define your function so that it works on scalars for the values of A, B, and C and a vector of x values.
Define it so that this call will return the value of y as described by the polynomial above when A, B, C, and x are correctly defined.
>> yValue = my_quadratic_poly(A,B,C,x)
This table shows some of the results your function should return. Test the function that you defined with these constants and x values.
y = Ax2 + Bx + C y A B C x 1 0 0 1 10 201 2 0 1 10 -169 -2 3 1 10 2 -1 -8 -19 -34 -2 3 1 [1:5] Here's the function definition.
function y = my_quadratic_poly(A,B,C,x) y = A* x.^2 + B*x + C;
Use your calculator to confirm that each quadratic polynomial was evaluated correctly. Here's the code that calls the function. Then, compare each result against the correct value.
>> my_quadratic_poly(0, 0, 1,10) ans = 1 >> my_quadratic_poly(2, 0, 1,10) ans = 201 >> my_quadratic_poly(-2, 3, 1,10) ans = -169 >> my_quadratic_poly(-2,3,1,[1:5]) ans = 2 -1 -8 -19 -34 >>