Problem overview Modify the original codes that we developed for sin(x), cos(x)
ID: 3819125 • Letter: P
Question
Problem overview Modify the original codes that we developed for sin(x), cos(x) and exp(x) (you can also refer to the "Additional Information") according the following design criteria: 1. (10 points) Make the calculations in the form of a callable function. Assume the argumentxis in radians 2. (10 points) Include both help document (one line) and general help in your function. 3. (90 points) For sin() and cos(x): a. Reduce the argument x, which originally can take the range of (-oo,+co) to (0, 2n) using the modulo operator (b mod (a,m) b. Further reduce the argument from part (a) to [0, r/2) using relationships for sin(x) and cos(x) in the different quadrants of the circle. c. Call a sub-function to compute the value of the new, reduced argument from (a) and (b) above. The minimum argument value will be 0 and maximum will be n/2. example if the function is called as exp(3.123), separate the 3 from the fractional component 0.123. Tip: you can use x-floor to do this. Recognizing that exp(x+y) exp(x) exp(y), compute the integer portion of the argument using a simple loop that performs the multiplication and calculate the fractional component using the series expansion. 5. (30 points) when calculating the infinite series (see Hw #3 and lecture notes of Chapter 5 for the definitions of sin, cos, and exp), continue to calculate terms until the following accuracy condition is met: a. Save the current value of the series before another term is added. Call this, for example, Soh. b. Now add the new term and calculate the new value of the summation, s w. c. Determine the absolute value of the difference between the old and new terms as d lsnew Soldl/sold. d. Stop adding terms when the absolute value of the difference between Sad and s less than a prescribed value, i.e., d E Use E 10 for all work in this assignment. built in sin, cos and exp functions in matlab.Explanation / Answer
function [s] = mySin(x)
r = mod(x,2*pi); %to change value within range of [0-2pi]
s = 0.0; %initially sum of series for sin is 0
din = 1.0; %din is denominator
val = r/din; %val is the value of nth term of the expansion series
count = 1; %count is the value indicating power and factorial
sign = 1; %sign associated with the term
while val > 1e-15 %accuracy level
s = s + sign*val; %modify sum
sign = sign*-1; %change sign value
din = din * (count+1) * (count+2); %calculate factorial
count = count + 2; %update count value
val = r^count/din; %calculate the n+1th term
end %end of while loop
end
function [c] = myCos(x)
r = mod(x,2*pi);
c = 0.0; %initially sum of series for cos is 0
din = 1.0;
val = 1; %for cos the first value is 1
count = 0; %and cout is 0, since in cos power and factorial are calculated on even numbers
sign = 1;
while val > 1e-15
c = c + sign*val;
sign = sign*-1;
din = din * (count+1) * (count+2);
count = count + 2;
val = r^count/din;
end
end
Here you go champ. You now have two functions mySin and myCos. Both functions take scalar parameter in radian and returns a scalar value. I have commented every line of the code to make your life easy. Incase you still have doubt, please feel free to comment below. I shall be glad to help you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.