Modify the original codes that we developed for sin(x), cos(x) and exp(x) (you c
ID: 3820236 • 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:
4. (50 points) For exp(x), separate the integer from the fractional component of the argument. For 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(x) 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.
Additional Information You may use the following code fragments for the sin(x), cos(x), and explx) 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 oncel Better is to work on the individual pieces, test them independently, then assemble together for the final functional version. t calculate exp (x), given x and N (number of terms) this is the sum of terms and our answer E first term is 1, so let's just put it in to start xt- t factorial of first term is also 1, so put it in here fact- 1: for i-1 :N now lets add N terms, as requested by user calculate numerator of next term (x i) xt fact fact i; 3 calculate denominator of next term (i s s xt/fact: add term to total sum and go back for more calculate sin (x), given x and N (number of terms) term in series is x, so put it here used to compute next term fact 1; factorial of first terms is 1 also, so put here sgn 1 s here is the sign. We start with +1 for i 3:2:N cheap way to alternate sign on each successive term 3 numerator: x i, given x (i-2) from previous time fact fact i* (i-1) calculate denominator: factorial, skipping by 2 s- s agn xt/fact: 3 add term to total sum end calculate cos (x), given x and N umber of terms xt- 1 fact 1: sgn- 1; for i-2 :2:N fact-fact i s s sgn xt/fact: Other Useful You do not necessarily need to use these abs(x) returns the absolute value of x returns the next lower integer of x, e.g., floor(1.23) 1 floor(x) mod(x,y) -returns modulo of x with y, e.g., mod(8,5) m3 (remainder of 8/5) returns 1 if 0, -1 if xo0 and 0 if x 0 sign(x)Explanation / Answer
# your code goes here
x=30.123;
s=1;
xt=1;
fact=1;
o=1;
i=1;
while 1
o=s;
xt=xt*x;
fact=fact*i;
s=s+xt/fact;
i=i+1;
if floor(s) == floor(o)
break
end
end
disp(s);
disp(exp(30.123));
# your code goes here
x=0.5;
s=x;
xt=x;
fact=1;
sgn=1;
o=x;
i=3;
while 1
o=s;
sgn=-sgn;
xt=xt*x*x;
fact=fact*i*(i-1);
s=s+ (sgn*xt/fact);
if abs(s)==abs(o)
break;
end
i=i+2;
end
disp(s);
disp(sin(0.5));
# your code goes here
x=0.6;
s=1;
xt=1;
fact=1;
sgn=1;
o=s;
i=2;
while 1
o=s;
sgn=-sgn;
xt=xt*x*x;
fact=fact*i*(i-1);
s=s+ (sgn*xt/fact);
if (abs(s)==abs(o))
break;
end
i=i+2;
end
disp(s);
disp(cos(0.6));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.