How many terms are required in iii to reduce the relative er
How many terms are required in (iii) to reduce the relative error to less than 1%? 0.00001%? Assume that the relative error (percent) is defined as: % Error = | (s_t-s_n)/s_t| times 100 S_t = sin x, S_t = sigma^infinity_n=0 (-1)^n x^2n+1/(2n+1) ! where S_t and S_n are the true and numerical solutions, respectively. The \"numerical\" solution will come from the summation of terms while the true solution can be obtained using the built-in MATLAB function for sin(x). Each iteration through the loop, create a formatted output that shows (1) the iteration number, (2) the number of terms in the series and (3) the percent error between the obtained values. The output printed during the loop should be done so that a new line is created each time a new output is printed. When the loop has completed, display the result, the final percent error, and the number of terms using a single command. Save the percent error per iteration and plot % error vs. iteration when the loop has completed. For (iii), create a flowchart showing the logic of your code. Considering that this is the course project, please be sure to create this flowchart in a word editor (i.e. it must be typed!)
Solution
x=60; %considered ange for the check
x=x*pi/180; %deg to radians
St=sin(x);
n=0;
Sn=0;
error=100;
while error>1E-4
Sn=Sn+((-1)^n*x^(2*n+1)/factorial(2*n+1));
error=abs((St-Sn)/St)*100;
n=n+1;
I(n)=n;
E(n)=error;
end
fileID = fopen(\'exp.txt\',\'w\');
M=[I; I; E];
fprintf(fileID,\'%4s %10s %12s \ \',\'Iteration\',\'numberOfTerms\',\'%Error\');
fprintf(fileID,\'%4.2f %15.2f %12.2f\ \',M);
type exp.txt
%out put
Iteration numberOfTerms %Error
1.00 1.00 20.92
2.00 2.00 1.18
3.00 3.00 0.03
4.00 4.00 0.00
5.00 5.00 0.00
