In Matlab Create a vector where each element is a term of th
In Matlab:
Create a vector where each element is a term of the series. Allow the user to input the value of x(as a scalar). Sum the terms of the series using a \"for\" loop and create a formatted output to show the user (1) the value of the approximation, (2) the actual value of sin(x) (from the MATLAB) function and (3) the error between the two. Use values x = 2, 2.5, 3 to compare.
Solution
Code:
x = input(\'Enter x value: \');
sinx = 0;
k = 2;
for i=1:2:17
sinx = sinx + (-1).^k*(x.^i)/factorial(i);
k = k + 1;
end
fprintf(\'The value of approximation is: %d\ \',sinx);
fprintf(\'The original value of sin(%.2f) is:%d\ \',x,sin(x));
fprintf(\'The error is: %d\ \',sinx-sin(x));
Output:
Enter x value: 2
The value of approximation is: 9.092974e-01
The original value of sin(2.00) is:9.092974e-01
The error is: 4.269252e-12
Enter x value: 2.5
The value of approximation is: 5.984721e-01
The original value of sin(2.50) is:5.984721e-01
The error is: 2.946692e-10
Enter x value: 3
The value of approximation is: 1.411200e-01
The original value of sin(3.00) is:1.411200e-01
The error is: 9.353375e-09
