Question

Write a regular function (i.e. in a function .m file) to calculate the series expansion of...

Write a regular function (i.e. in a function .m file) to calculate the series expansion of cosine(x). The number of terms calculated in the series should be specified in the input list of the function. Write a separate function that determines when the series expansion begins to deviate by at least 10% from the true value of cosine(x) based on the number of terms calculated in the series expansion, n. For n = 1, 2, … 10, determine the values of x where deviation between cosine(x) and the series expansion occur. Display the values in a MATLAB® Table. Create a single, properly-formatted plot that visually shows your results are correct.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any clarification needed please ask. If you need any extra plots or results needed on the same please do ask.

Main code

clc;
clear;
x=[-179:1:180]*(pi/180); %(deg to rad conversion)
for i=1:length(x)
for j=1:10
c = cos_series(x(i),j);
[t,er] = cos_series_dev(x(i),j);
if t==1
r(:,i)=[(180/pi)*x(i),j,er];
j=1;
end
  
end
end

[ind]= find(r(1,:)==0)
r(:,ind)=[];

angle_in_deg=r(1,:);
number_of_terms=r(2,:);
table(angle_in_deg',number_of_terms')
stem(r(1,:),r(3,:));
ylable('Error');
xlable('Angle');

__________________________________________________________________________________________________

Functions

__________________________________________________________________________________________________


function [c] = cos_series( x,n )
%Computes series epantion of cos(x)
%v: The x value
%n: Number of terms in the expansion
c=0;
for i=1:length(x)
for k=0:n
term_k=((-1)^k)*(x^(2*k))/(factorial(2*k));
c=c+term_k;
end
end
end
--------------------------------------------------------------------------------------------------------------------------------------------------


function [t,deviation] = cos_series_dev( x,n )
%Computes deviation of cos(x) values
%v: The x value
%n: Number of terms in the expansion
d=sum(abs(cos(x)-cos_series(x,n)));
deviation=abs((d/cos(x))*100);
if deviation>=10
% disp('The series expansion deviates from the actual value');
t=1;
else
t=0;
  
end

end

______________________________________________________________________

Results

Var1-angle, var2:Min number of terms to have err<10

Var1 Var2
____ ____

-179 3   
-178 3   
-177 3   
-176 3   
-175 3   
-174 3   
-173 3   
-172 3   
-171 3   
-170 3   
-169 3   
-168 3   
-167 3   
-166 3   
-165 3   
-164 3   
-163 3   
-162 2   
-161 2   
-160 2   
-159 2   
-158 2   
-157 2   
-156 2   
-155 2   
-154 2   
-153 2   
-152 2   
-151 2   
-150 2   
-149 2   
-148 2   
-147 2   
-146 2   
-145 2   
-144 2   
-143 2   
-142 2   
-141 2   
-140 2   
-139 2   
-138 2   
-137 2   
-136 2   
-135 2   
-134 2   
-133 2   
-132 2   
-131 2   
-130 2   
-129 2   
-128 2   
-127 2   
-126 2   
-125 2   
-124 2   
-123 2   
-122 2   
-121 2   
-120 2   
-119 2   
-118 2   
-117 2   
-116 2   
-115 2   
-114 2   
-113 2   
-112 2   
-111 2   
-110 2   
-109 2   
-108 2   
-107 2   
-106 2   
-105 2   
-104 2   
-103 2   
-102 2   
-101 2   
-100 2   
-99 2   
-98 2   
-97 2   
-96 2   
-95 2   
-94 2   
-93 2   
-92 2   
-91 2   
-90 9   
-89 2   
-88 2   
-87 2   
-86 2   
-85 2   
-84 2   
-83 2   
-82 1   
-81 1   
-80 1   
-79 1   
-78 1   
-77 1   
-76 1   
-75 1   
-74 1   
-73 1   
-72 1   
-71 1   
-70 1   
-69 1   
-68 1   
-67 1   
-66 1   
-65 1   
-64 1   
-63 1   
-62 1   
-61 1   
61 1   
62 1   
63 1   
64 1   
65 1   
66 1   
67 1   
68 1   
69 1   
70 1   
71 1   
72 1   
73 1   
74 1   
75 1   
76 1   
77 1   
78 1   
79 1   
80 1   
81 1   
82 1   
83 2   
84 2   
85 2   
86 2   
87 2   
88 2   
89 2   
90 9   
91 2   
92 2   
93 2   
94 2   
95 2   
96 2   
97 2   
98 2   
99 2   
100 2   
101 2   
102 2   
103 2   
104 2   
105 2   
106 2   
107 2   
108 2   
109 2   
110 2   
111 2   
112 2   
113 2   
114 2   
115 2   
116 2   
117 2   
118 2   
119 2   
120 2   
121 2   
122 2   
123 2   
124 2   
125 2   
126 2   
127 2   
128 2   
129 2   
130 2   
131 2   
132 2   
133 2   
134 2   
135 2   
136 2   
137 2   
138 2   
139 2   
140 2   
141 2   
142 2   
143 2   
144 2   
145 2   
146 2   
147 2   
148 2   
149 2   
150 2   
151 2   
152 2   
153 2   
154 2   
155 2   
156 2   
157 2   
158 2   
159 2   
160 2   
161 2   
162 2   
163 3   
164 3   
165 3   
166 3   
167 3   
168 3   
169 3   
170 3   
171 3   
172 3   
173 3   
174 3   
175 3   
176 3   
177 3   
178 3   
179 3   
180 3

6000 5000 4000 3000 2000 1000 50 100 150200 200 150 100 Angle

Add a comment
Know the answer?
Add Answer to:
Write a regular function (i.e. in a function .m file) to calculate the series expansion of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT