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

Ex#2: (Functions) A. Write a function Sin() that calculates the sine of the supp

ID: 3633477 • Letter: E

Question


Ex#2: (Functions)

A. Write a function Sin() that calculates the sine of the supplied angle (given in degrees) using:

sin(x) = x- (x^3/3!)+(x^5/5!)-(x^7/7!)+(x^9/9!)-(x^11/11!) …

! indicates factorial. 5! = 5 x 4 x 3 x 2 x 1 = 120

Call to the function should be : Result=sin(32.5); means calculate the sine of 32.5 degrees
B. Write a function cos() that calculates the cosine of the supplied angle (given in degrees) using:

cos(x) = 1- (x^2/2!)+(x^4/4!)-(x^6/6!)+(x^8/8!)-(x^10/10!) …


Call to the function should be : Result=cos(32.5); means calculate the cosine of 32.5 degrees
• Test your program for A) and B).
• Ask the user for the number of terms to use in the series then output all the values from 0 to 180 degrees from your functions.
• Also, output the values from the library predefined functions sin( ) and cos( ) for comparison purpose. Use the same unit for comparison.

Check whether sin( ) and cos( ) functions require the angle to be passed in radians or in degrees.
degrees = radians x 180/
radians = degrees x /180


Explanation / Answer

A) function ret = my_sin(x,n) ret = 0; pow = 1; sign = 1; if nargin == 1, n = 100; y = x/180*pi; %convert degree to radians. for i = 1:n % process the n terms ret = ret + sign * y .^ pow / factorial(pow); pow = pow + 2; % increment to next odd num sign = sign * -1; % flip the sign end else y = x/180*pi; %convert degree to radians. for i = 1:n % process the n terms ret = ret + sign * y .^ pow / factorial(pow); pow = pow + 2; % increment to next odd num sign = sign * -1; % flip the sign end end B) function ret = my_cos(x,n) ret = 0; pow = 0; sign = 1; if nargin == 1, n = 100; y = x/180*pi; %convert degree to radians. for i = 1:n % process the n terms ret = ret + sign * y .^ pow / factorial(pow); pow = pow + 2; % increment to next odd num sign = sign * -1; % flip the sign end else y = x/180*pi; %convert degree to radians. for i = 1:n % process the n terms ret = ret + sign * y .^ pow / factorial(pow); pow = pow + 2; % increment to next odd num sign = sign * -1; % flip the sign end end Testing program: A = zeros(1,181); B = zeros(1,181); C = zeros(1,181); D = zeros(1,181); n= input('Number of Terms:'); for x = 0:180; A(x+1) = my_sin(x,n); B(x+1) = my_cos(x,n); C(x+1) = sind(x); D(x+1) = cosd(x); end Call out the respective verctors for comparison. As for pre-defined sin(x) and cos(x) functions requires angle to be in radians,

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote