- #1
ibjeepn247365
- 1
- 0
Hi, hopefully you guys can point me in the right direction. The problem says to determine the number of terms necessary to approximate cos(x) to 8 significant digits using Maclaurin series approximation for x = .3*pi. I don't really have a problem with the actually question, my problem comes when we have to do this in matlab. I have come up with something in MATLAB that gives me the answer but wondering if I could use a for or while loop to make it shorter because it seems like there is another way to do it. Thank you for your help in advance. This is a rough copy of MATLAB and just to see if I got the right anser.
n = 8; %Significant Digits
x = .3*pi;
format long
epsilon_s = (.5*10^(2-n));
maclaurin_1 = 1;
maclaurin_2 = 1 - x^2/2;
maclaurin_3 = 1 - x^2/2 + x^4/factorial(4);
maclaurin_4 = 1 - x^2/2 + x^4/factorial(4) - x^6/factorial(6);
maclaurin_5 = 1 - x^2/2 + x^4/factorial(4) - x^6/factorial(6) + x^8/factorial(8);
maclaurin_5 = 1 - x^2/2 + x^4/factorial(4) - x^6/factorial(6) + x^8/factorial(8)...
-x^10/factorial(10);
maclaurin_6 = 1 - x^2/2 + x^4/factorial(4) - x^6/factorial(6) + x^8/factorial(8)...
-x^10/factorial(10) + x^12/factorial(12);
epsilon_a_1 = (maclaurin_2 - maclaurin_1)/maclaurin_2;
epsilon_a_2 = (maclaurin_3 - maclaurin_2)/maclaurin_3;
epsilon_a_3 = (maclaurin_4 - maclaurin_3)/maclaurin_4;
epsilon_a_4 = (maclaurin_5 - maclaurin_4)/maclaurin_5;
epsilon_a_5 = (maclaurin_6 - maclaurin_5)/maclaurin_6;
if abs(epsilon_a_1) < epsilon_s
fprintf('True1\n');
end
if abs(epsilon_a_2) < epsilon_s
fprintf('True2\n');
end
if abs(epsilon_a_3) < epsilon_s
fprintf('True3\n');
end
if abs(epsilon_a_4) < epsilon_s
fprintf('True4\n');
end
if abs(epsilon_a_5) < epsilon_s
fprintf('True5\n');
end
n = 8; %Significant Digits
x = .3*pi;
format long
epsilon_s = (.5*10^(2-n));
maclaurin_1 = 1;
maclaurin_2 = 1 - x^2/2;
maclaurin_3 = 1 - x^2/2 + x^4/factorial(4);
maclaurin_4 = 1 - x^2/2 + x^4/factorial(4) - x^6/factorial(6);
maclaurin_5 = 1 - x^2/2 + x^4/factorial(4) - x^6/factorial(6) + x^8/factorial(8);
maclaurin_5 = 1 - x^2/2 + x^4/factorial(4) - x^6/factorial(6) + x^8/factorial(8)...
-x^10/factorial(10);
maclaurin_6 = 1 - x^2/2 + x^4/factorial(4) - x^6/factorial(6) + x^8/factorial(8)...
-x^10/factorial(10) + x^12/factorial(12);
epsilon_a_1 = (maclaurin_2 - maclaurin_1)/maclaurin_2;
epsilon_a_2 = (maclaurin_3 - maclaurin_2)/maclaurin_3;
epsilon_a_3 = (maclaurin_4 - maclaurin_3)/maclaurin_4;
epsilon_a_4 = (maclaurin_5 - maclaurin_4)/maclaurin_5;
epsilon_a_5 = (maclaurin_6 - maclaurin_5)/maclaurin_6;
if abs(epsilon_a_1) < epsilon_s
fprintf('True1\n');
end
if abs(epsilon_a_2) < epsilon_s
fprintf('True2\n');
end
if abs(epsilon_a_3) < epsilon_s
fprintf('True3\n');
end
if abs(epsilon_a_4) < epsilon_s
fprintf('True4\n');
end
if abs(epsilon_a_5) < epsilon_s
fprintf('True5\n');
end