Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please help me solve this matlab problem. thank you very much! 3. Consider a ser

ID: 3885919 • Letter: P

Question

please help me solve this matlab problem. thank you very much!


3. Consider a series approximation of sin(x) as k-1 ,(2k-l) x+(-1) sin(x) = x + 3! 5! 7! (2k-1) ! where N is the total number of terms in the series. Write a MATLAB code to find the value of sin(x) using the above series by successive adding terms. That is, vary N from 2 up to 200 in step of 1. Then consider x-10. The exact value can be directly obtained MATLAB built-in function sin. The approximate result is obtained by the above series. Define error as Error=exact-approximate l . (a) Tabulate the values of the Error as N varies from 2 to 200. (b) What happens when k>155 and why? (c) A Tolerance is the amount of Relative Error that is allowed. The Relative Error is defined as Relative Error I(exact - approximate)/exact| Specify the tolerance to be 109. Set a condition that when the Relative Error is less than the tolerance, we have reached sufficient accuracy to stop adding terms in the series. How many terms are needed to reach this accuracy?

Explanation / Answer

Given below are the answers for a, b and c. Hope the answer helps. If it did, please don't forget to rate the answer. Thank you very much.

Code for part a and b


x = 10;
exact = sin(x);
fprintf("exact sin(10) = %f ", exact);

for N = 2 :200
sinx = x;
for k = 2:N
sinx = sinx +( ((-1.0)^(k-1)) * (x ^ (2*k-1))/factorial(2*k-1) );
end
fprintf("%d %f ", N , sinx);
end

output


exact sin(10) = -0.544021
2 -156.666667
3 676.666667
4 -1307.460317
5 1448.271605
6 -1056.939234
7 548.965150
8 -215.751223
9 65.394502
10 -16.811850
11 2.761091
12 -1.107079
13 -0.462384
14 -0.554221
15 -0.542911
16 -0.544127
17 -0.544012
18 -0.544022
19 -0.544021
20 -0.544021
21 -0.544021
22 -0.544021
23 -0.544021
24 -0.544021
25 -0.544021
26 -0.544021
27 -0.544021
28 -0.544021
29 -0.544021
30 -0.544021
31 -0.544021
32 -0.544021
33 -0.544021
34 -0.544021
35 -0.544021
36 -0.544021
37 -0.544021
38 -0.544021
39 -0.544021
40 -0.544021
41 -0.544021
42 -0.544021
43 -0.544021
44 -0.544021
45 -0.544021
46 -0.544021
47 -0.544021
48 -0.544021
49 -0.544021
50 -0.544021
51 -0.544021
52 -0.544021
53 -0.544021
54 -0.544021
55 -0.544021
56 -0.544021
57 -0.544021
58 -0.544021
59 -0.544021
60 -0.544021
61 -0.544021
62 -0.544021
63 -0.544021
64 -0.544021
65 -0.544021
66 -0.544021
67 -0.544021
68 -0.544021
69 -0.544021
70 -0.544021
71 -0.544021
72 -0.544021
73 -0.544021
74 -0.544021
75 -0.544021
76 -0.544021
77 -0.544021
78 -0.544021
79 -0.544021
80 -0.544021
81 -0.544021
82 -0.544021
83 -0.544021
84 -0.544021
85 -0.544021
86 -0.544021
87 -0.544021
88 -0.544021
89 -0.544021
90 -0.544021
91 -0.544021
92 -0.544021
93 -0.544021
94 -0.544021
95 -0.544021
96 -0.544021
97 -0.544021
98 -0.544021
99 -0.544021
100 -0.544021
101 -0.544021
102 -0.544021
103 -0.544021
104 -0.544021
105 -0.544021
106 -0.544021
107 -0.544021
108 -0.544021
109 -0.544021
110 -0.544021
111 -0.544021
112 -0.544021
113 -0.544021
114 -0.544021
115 -0.544021
116 -0.544021
117 -0.544021
118 -0.544021
119 -0.544021
120 -0.544021
121 -0.544021
122 -0.544021
123 -0.544021
124 -0.544021
125 -0.544021
126 -0.544021
127 -0.544021
128 -0.544021
129 -0.544021
130 -0.544021
131 -0.544021
132 -0.544021
133 -0.544021
134 -0.544021
135 -0.544021
136 -0.544021
137 -0.544021
138 -0.544021
139 -0.544021
140 -0.544021
141 -0.544021
142 -0.544021
143 -0.544021
144 -0.544021
145 -0.544021
146 -0.544021
147 -0.544021
148 -0.544021
149 -0.544021
150 -0.544021
151 -0.544021
152 -0.544021
153 -0.544021
154 -0.544021
155 NaN
156 NaN
157 NaN
158 NaN
159 NaN
160 NaN
161 NaN
162 NaN
163 NaN
164 NaN
165 NaN
166 NaN
167 NaN
168 NaN
169 NaN
170 NaN
171 NaN
172 NaN
173 NaN
174 NaN
175 NaN
176 NaN
177 NaN
178 NaN
179 NaN
180 NaN
181 NaN
182 NaN
183 NaN
184 NaN
185 NaN
186 NaN
187 NaN
188 NaN
189 NaN
190 NaN
191 NaN
192 NaN
193 NaN
194 NaN
195 NaN
196 NaN
197 NaN
198 NaN
199 NaN
200 NaN

The output from 155 onwards becomes NaN (not a number). This is because both the numerator and denominator terms tend to be very large values(inf) because and division of inf / inf happens leading to result of NaN.

The modified code for part C is given below with tolerance.

x = 10;
exact = sin(x);
fprintf("exact sin(10) = %f ", exact);
tolerance = 10^-9;

for N = 2 :200
sinx = x;
for k = 2:N
sinx = sinx +( ((-1.0)^(k-1)) * (x ^ (2*k-1))/factorial(2*k-1) );
end
  
fprintf("%d %f ", N , sinx);
err = abs((exact - sinx)/exact);
if err <= tolerance
break;
end
end

fprintf("%d terms are enough for specified tolerance", N);

output


exact sin(10) = -0.544021
2 -156.666667
3 676.666667
4 -1307.460317
5 1448.271605
6 -1056.939234
7 548.965150
8 -215.751223
9 65.394502
10 -16.811850
11 2.761091
12 -1.107079
13 -0.462384
14 -0.554221
15 -0.542911
16 -0.544127
17 -0.544012
18 -0.544022
19 -0.544021
20 -0.544021
21 -0.544021
Only 21 terms are enough for specified tolerance