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
  • Instructions: Submit your script in a file named hwk08.m to the dropbox before 11:59 pm on the du...

    Instructions: Submit your script in a file named hwk08.m to the dropbox before 11:59 pm on the due date NOTE: This assignment is neither quick nor simple. You will be well served to start on it early, and to ask for help if you need it. Being a more substantial assignment than earlier hwk, it is worth- s. When you ask your calculator for the value of a function for a specified argument, (e.g., sin(22), cos(74), In(6.5)), it almost certainly...

  • You are given a finite step function xt=-1  0<t<4 1  4<t<8.           Hand calculate the FS coefficients of...

    You are given a finite step function xt=-1  0<t<4 1  4<t<8.           Hand calculate the FS coefficients of x(t) by assuming half- range expansion, for each case below. Modify the code below to approximate x(t) by cosine series only (This is even-half range expansion). Modify the below code and plot the approximation showing its steps changing by included number of FS terms in the approximation. Modify the code below to approximate x(t) by sine series only (This is odd-half range expansion).. Modify...

  • Problem 5 xx The Taylor series expansion for sin(x) is sin(x) = x -H + E-E+...

    Problem 5 xx The Taylor series expansion for sin(x) is sin(x) = x -H + E-E+ = o E- (-1) . 2n +1 no (2n+1)! !57 where x is in radians. Write a MATLAB program that determines sin(x) using the Taylor series expansion. The program asks the user to type a value for an angle in degrees. Then the program uses a while loop for adding the terms of the Taylor series. If an n is the nth term in...

  • Write the Taylor series expansion for the following function up to the second order terms about...

    Write the Taylor series expansion for the following function up to the second order terms about point (1,1). Then, compare approximate and exact values of the function at (1.2,0.8). f(x1, x2) = 10x1 – 20x{x2 + 10xż + x– 2x1 + 5

  • Convince yourself that the Maclaurin Series for cos(x) is: A. Write a function script called cos...

    Convince yourself that the Maclaurin Series for cos(x) is: A. Write a function script called cos_series that takes that takes as its inputs, x and N and has output given by the sum in the N-term Maclaurin Series approximation for Cos(x). Hint: try a “for loop” and set “format long” in your code. You may use the MATLAB built-in function factorial() B. Check your code by finding the 2-terms, 3-terms, 4-terms, 5-terms and 6-terms Maclaurin Series approximations every 30 degrees...

  • The following function computes by summing the Taylor series expansion to n terms. Write a program...

    The following function computes by summing the Taylor series expansion to n terms. Write a program to print a table of using both this function and the exp() function from the math library, for x = 0 to 1 in steps of 0.1. The program should ask the user what value of n to use. (PLEASE WRITE IN PYTHON) def taylor(x, n): sum = 1 term = 1 for i in range(1, n): term = term * x / i...

  • 3. Cosine Approximation Write a function m-file to calculate the approximated cosine function which can be...

    3. Cosine Approximation Write a function m-file to calculate the approximated cosine function which can be evaluated by the following infinite series - equation (2): 1. x² x4 26 (-1)(n+1). 22(n-1) cos x = 1 - 3 + - 6! + + (2(n − 1))! Here are requirements for the function m-file. - Parameter lists (inputs): 2 value (in radian), and desired error percentagel. - Return value (outputs): approximated value, real error percentage, and number of iterations - Function name:...

  • 2. Using the MATLAB "integral" command, numerically determine the Fourier Cosine series of the following function....

    2. Using the MATLAB "integral" command, numerically determine the Fourier Cosine series of the following function. Assume each case has an even extension (b,-0) Last Name N-Z: f= 2xcos (Vx+4), 0<x<3 (Hint: after extension L-3) Have your code plot both the analytical function (as a red line) and the numerical Fourier series (in blue circles -spaced appropriately). Use the Legend command to identify the two items. It is suggested to use a series with 15 terms.

  • Problem 1 MATLAB A Taylor series is a series expansion of a function f()about a given point a. For one-dimensional real...

    Problem 1 MATLAB A Taylor series is a series expansion of a function f()about a given point a. For one-dimensional real-valued functions, the general formula for a Taylor series is given as ia) (a) (z- a) (z- a)2 + £(a (r- a) + + -a + f(x)(a) (1) A special case of the Taylor series (known as the Maclaurin series) exists when a- 0. The Maclaurin series expansions for four commonly used functions in science and engineering are: sin(x) (-1)"...

  • Please help me write a Matlab function that sums up a specified number of terms from...

    Please help me write a Matlab function that sums up a specified number of terms from the Taylor series of log(x) with a = 1 Make sure it runs!

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