I need someone to check my code Its gives me endless results
I need someone to check my code. Its gives me endless results. Here is the question...
This is my code...
x = 1;
st = sin(x);
j = 1;
errArr = [];
iteration = [];
sn = 0;
while 1
for i=1:j
k = (2*i)+1;
sn = sn + ((-1).^i)*((x.^k)/(factorial(k)));
end
error = abs((st-sn)/st)*100;
errArr(end+1) = error;
fprintf(\'%f %f \',st,sn);
fprintf(\'Iteration: %d Number of terms in the series: %d The error is: %f\ \',j,j,error);
sn = 0;
if(gt(error,1))
j = j + 1;
iteration(end+1) = j;
else
break;
fprintf(\'Terms required to get the error to less than 1 are : %d\ \',j);
end
end
plot(iteration,errArr);
Can someone please help me out?
iii. 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: (St-Sn) Error x 100 n-1 t Sin x (2n+1)! where Stand Snare 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 MAE 215: Introduction to Programming in MATLAB Course Project Due: Monday, November 28th by midnight 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.Solution
Solution:
Corrected code is given below:
%considered angle for the check
x=60;
%deg to radians
x=x*pi/180;
St=sin(x);
n=0;
Sn=0;
error=100;
%Loop executes until the 1E-4
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
%Display the result
fprintf(\'Iteration: %d Number of terms in the series: %d The error is: %f\ \',I,I,E);
%Plot the graph iteration vs. error
plot(I,E);
![I need someone to check my code. Its gives me endless results. Here is the question... This is my code... x = 1; st = sin(x); j = 1; errArr = []; iteration = [] I need someone to check my code. Its gives me endless results. Here is the question... This is my code... x = 1; st = sin(x); j = 1; errArr = []; iteration = []](/WebImages/44/i-need-someone-to-check-my-code-its-gives-me-endless-results-1139514-1761610832-0.webp)
![I need someone to check my code. Its gives me endless results. Here is the question... This is my code... x = 1; st = sin(x); j = 1; errArr = []; iteration = [] I need someone to check my code. Its gives me endless results. Here is the question... This is my code... x = 1; st = sin(x); j = 1; errArr = []; iteration = []](/WebImages/44/i-need-someone-to-check-my-code-its-gives-me-endless-results-1139514-1761610832-1.webp)