Write a function called poly_val that is called like this p poly val (ce,c,x), w
ID: 3888264 • Letter: W
Question
Write a function called poly_val that is called like this p poly val (ce,c,x), where ce and x are scalars, and p is a scalar. If e is an empty vector, then pace. If e is a scalar, then p .cetc. Otherwise, p equals the polynomial, where n is the length of the vector c. Hints 1. The functions isempty, isscalar, iscolumn, and length will tell you everything you need to know about the vector c. 2, when c is a vector, use an element by element exponent to determine the vector (r123···z"]. 3. When c is a vector, use the sum function with element by element multiplication. Matrix multiplication could also be used, but we have not yet covered that. Here are three example runs: >> p poly_val(-17,[1,see0) >> p poly_val(3.2,[3, -4,10],2.2) (C 96.9280 >> p - poly_val(1,[1,1,1,1],10) >> p = poly-val (8,5,4) 28Explanation / Answer
poly_val.m file
function p = poly_val(c0,c,x)
% if c is empty vector then = = c0
if(isempty(c))
p = c0;
% if c is a scalar then p = c0+c*x
elseif(isscalar(c))
p = c0+c*x;
else
%calculate given vector polynomial value
p = c0;
for i=1:length(c)
p = p + c(i)*(x.^i);
end
end
end
main.m file
p = poly_val(-17,[],5000)
p = poly_val(3.2,[3,-4,10],2.2)
p = poly_val(1,[1,1,1,1],10)
p = poly_val(8,5,4)
% sample output
%p = -17
%p = 96.920
%p = 11111
%p = 28
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.