Write a matlab program approximates the function cosx using
Write a matlab program approximates the function cos(x) using a Taylor series approximation. It should first prompts the user to enter the number of terms in the Taylor series and the value of x. It should print actual value and the error.
Solution
prompt = \'How many terms in Taylor series to consider? \';
num = input(prompt);
prompt = \'What is the value of x?\';
x = input(prompt);
approx=0;
for i=0 : num-1
approx = approx + ((-1)^i)* (x^(2*i))/factorial(2*i)
i
end
exact = cos(x);
error = abs(approx-exact);
disp([\'Actual value is \' num2str(exact)]);
disp([\'Error is \' num2str(error)]);
