Write a MATLAB code for the following problem:

determine the number of terms necessary to approximate cosx to 8 significant figures using the Maclaurin series approximation. use x=0.3pi

cosx=1−(x2/2)+(x4/4!)−(x6/6!)+(x8/8!)

Respuesta :

Here is matlab that should work % cos(x) = 1 - (x^2)/2! + (x^4)/4! -(x^6)/6!+(x^8)/8!... % let y= x*x % cos(x) = sum( (-y)^n/(2n)! ) format short x= 0.3*pi; y= x*x; for N= 1:6 n= 0:N; s1= [(-y).^n./factorial(2*n) ] mac= sum(s1); cx= cos(x); str= sprintf('%d terms. series: %12.10f cos(x): %12.10f\n %12.10f',... N, mac,cx, (cx-mac)); disp(str); end;