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

Using MATLAB, For exp(x), separate the integer from the fractional component of

ID: 2081877 • Letter: U

Question

Using MATLAB, 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.

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

Solution:

% To separate integer from the fractional component

number=1.23;

integ=floor(number);

fract=number-integ;

or

number = 1.23

integ = fix(number)

frac = mod(abs(number),1)

% Power Series

x = input('x = ');

n = input('Number of terms in the power series to find exp(x) = ');

for n = 1:n

e(n) = x^(n-1)/factorial(n-1);

ep = sum(e);

y = exp(x);

d = (y-ep)/y*100;

end

fprintf(' Value of approximate using %1.0f terms in the power series is %5.5f ',n,ep);

fprintf(' Correct exponential value is %5.5f ',y);

fprintf(' Percentage Difference = %4.2f ',d)

% To find the code fragments for the sin(x), cos(x), and exp(x)

p=input(' Enter the number angle in radians ');

m=p;

% Calculating Exponential power

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

% To perform sine and cosine series

a=[1,1];

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

  sum1(1)=0; % To find the sine series

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

sum2(1)=0; % To find the cosine series

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));

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