In Matlab Consider the generalized form of the Taylor series
In Matlab:
Consider the generalized form of the Taylor series for sin(x);
Sum the first 100 terms of the series using a \"for\" loop and create a formatted output to show the user the value of the approximation. Allow the user to input the value x as a scalar. Repeat this problem with the first 250 terms. Use values x = 2, 2.5, 3.
Solution
% matlab code
sumSinx = 0;
x = input(\"Enter x: \");
n = input(\"Enter n: \");
for i=0:n
sumSinx = sumSinx + ((-1)^i)*(x^(2*i+1)/(factorial(2*i +1)));
end
fprintf(\"n: %d\\tx: %f\\tsinx = %0.10f\ \",n,x,sumSinx);
% end matlab code
%{
output:
Enter x: 2
Enter n: 100
n: 100 x: 2.000000 sinx = 0.9092974268
Enter x: 2
Enter n: 250
n: 250 x: 2.000000 sinx = 0.9092974268
Enter x: 2.5
Enter n: 250
n: 250 x: 2.500000 sinx = 0.5984721441
Enter x: 3
Enter n: 250
n: 250 x: 3.000000 sinx = 0.1411200081
%}
