Modify the original codes that we developed for sin(x), cos(x) and exp(x) (you c
ID: 3109453 • Letter: M
Question
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:
(90 points) For sin(x) and cos(x):
a. Reduce the argument x, which originally can take the range of (-,+) to [0, 2) using the modulo operator (b = mod(a,m)).
b. Further reduce the argument from part (a) to [0, /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 /2.
Additional Information:
You may use the following code fragments for the sin(x), cos(x), and exp(x) functions. Note, however, that these routines calculate a fixed number of terms, based on the value of N. you will have to modify the routines to continue to calculate terms until the accuracy criterion has been established. Tips: do NOT try to write the complete, fully-functional routine all at once! Better is to work on the individual pieces, test them independently, then assemble together for the final functional version.
% calculate exp(x), given x and N (number of terms)
s= 1; % this is the sum of terms and our final answer
xt= 1; % first term is 1, so let's just put it in to start
fact= 1; % factorial of first term is also 1, so put it in here
for i=1:N % now lets add N terms, as requested by user
xt= xt*x; % calculate numerator of next term (x^i)
fact= fact*i; % calculate denominator of next term (i!)
s=s + xt/fact; % add term to total sum, and go back for more
end
% calculate sin(x), given x and N (number of terms)
s=x; % 1st term in series is x, so put it here
xt=x; % used to compute next term
fact=1; % factorial of first terms is 1 also, so put here
sgn=1; % here is the sign. We start with +1.
for i=3:2:N
sgn= -sgn; % cheap way to alternate sign on each successive term
xt= xt*x*x; % numerator: x^i, given x^(i-2) from previous time
fact= fact*i*(i-1); % calculate denominator: factorial, skipping by 2
s= s + sgn*xt/fact; % add term to total sum
end
% calculate cos(x), given x and N (number of terms)
s= 1;
xt= 1;
fact= 1;
sgn= 1;
for i=2:2:N
sgn= -sgn;
xt=xt*x*x;
fact=fact*i*(i-1);
s=s + sgn*xt/fact;
end
Explanation / Answer
clc;
clear all;
close all;
p=input(' Enter the number Angle in radians ');
m=p;
% % % Calculating Exponetnial power
% % % let accuracy is within 0.0001
le=0.0001;
sum(1)=0;
for n=1:100;
sum(n+1)=sum(n)+p^(n-1)/factorial(n-1);
if (abs(sum(n+1)-sum(n)) < le)
break;
end
end
% % % % Calculating sin and cosine series
a=[1,1]; %%% to indicate whether sin and cos are positive or negative
if (p > 2*pi)
p=mod(p,2*pi);
end
if ( p < pi/2 )
a=[1,1];
else if (pi/2 < p && p< pi )
p= pi-p;
a=[1,-1];
else if ( pi < p && p < 3*pi/2 )
p=p-pi;
a=[-1,-1];
else
p=2*pi-p;
a=[-1,1];
end
end
end
% % % sine series
sum1(1)=0;
x=a(1)*p;
for k=1:100
sum1(k+1)=sum1(k)+(-1)^(k+1)*x^(2*k-1)/factorial(2*k-1);
if (abs(sum1(k+1)-sum1(k)) < le)
break;
end
end
% % % cosine series
sum2(1)=0;
x=a(2)*p;
for h=1:100
sum2(h+1)=sum2(h)+(-1)^(h+1)*p^(2*(h-1))/factorial(2*(h-1));
if (abs(sum2(h+1)-sum2(h)) < le)
break;
end
end
fprintf('exp(%f ) = %f ',m,sum(end));
fprintf('sin(%f) = %f ',m,sum1(end));
fprintf('cos(%f) = %f ',m,sum2(end));
output :
Enter the number Angle in radians 7*pi
exp(21.991149 ) = 3553321280.847006
sin(21.991149) = 0.000001
cos(21.991149) = -1.000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.