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

This needs to be solved in MATLAB language. A Taylor Series approximation for si

ID: 642466 • Letter: T

Question

This needs to be solved in MATLAB language.

A Taylor Series approximation for sin(x) is performed with the algorithm show below. a) Perform the above calculations for a given x and a given number of terms using a for loop. Your output should be exactly as follows but instead using 4.3 for x. Enter x value for sin(x) approx: 3.2 Enter how many terms you would like to use: 5 Your Approximation with 5 terms is -0.0499. The exact answer is -0.0584. b) Perform the above calculations for a given x using a while loop to continually add terms until you have less than a specified error. Your output should be exactly as follows but instead using 4.3 for x and 0.001 for error. Hint: Error abs((exact ? approx)/exact) Enter x value for sin(x) approx: 3.2 Enter the error your approximate should be less than: 0.1 Your approximated value for sin(x) is -0.0589. And it was achieved with 6 terms.

Explanation / Answer

******************************

***********demo.m

******************************

x= input('enter tor x value for sin(x) approx: ');
n= input('Up to how many terms would you like to use: ');
[sinx]=sinx_approx(n,x);
fprintf('your approximation with %i terms is %f ',n,sinx)
fprintf('The exact answer is %f ',sin(x))

*************************************************

**************** sinx_approx.m*****************

**************************************************

function [sinx, error] = sinx_approx(n,x)
% approximates the value of sin(x), the approximation is more accurate as
% the number of terms selected is increased.

sinx=0;
for i=1:1:n+1
sinx=(-1)^(i+1) * x.^(2*i-1)/ fact(2*i-1)+ sinx;
error=((sin(x)-sinx)/sin(x));

end

end

********************************************************

***********************fact.m ************************

******************************************************

function factorial = fact(i)
x=1;
for j=1:i
x=x*j;
end
factorial=x;
end

OUTPUT:

enter tor x value for sin(x) approx: 3.2

Up to how many terms would you like to use: 5

your approximation with 5 terms is -0.058939

The exact answer is -0.058374

2)

**************************************************

***************** demo.m *********************

***********************************************

x= input('enter tor x value for sin(x) approximation');
n=1;
error_expected = input('enter the error that your approximate should be less than');
[sinx, error]=sinx_approx(n,x);
while (error > error_expected)
[sinx, error]=sinx_approx(n,x);
n= n+1;
end
fprintf('your approximate value is %f ',sinx)
fprintf('And it was acheived with %i steps ',n)

*************************************************

**************** sinx_approx.m*****************

**************************************************

function [sinx, error] = sinx_approx(n,x)
% approximates the value of sin(x), the approximation is more accurate as
% the number of terms selected is increased.

sinx=0;
for i=1:1:n+1
sinx=(-1)^(i+1) * x.^(2*i-1)/ fact(2*i-1)+ sinx;
error=((sin(x)-sinx)/sin(x));

end

end

********************************************************

***********************fact.m ************************

******************************************************

function factorial = fact(i)
x=1;
for j=1:i
x=x*j;
end
factorial=x;
end

OUTPUT:

enter tor x value for sin(x) approximation3.2                                                                                                                                   

enter the error that your approximate should be less than0.1                                                                                                                    

your approximate value is -0.058939                                                                                                                                             

And it was acheived with 6 steps

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