Question

(a) Write a Matlab function called sine_Taylor that inputs a positive integer n, a number x, and outputs the nth Taylor polynomial of the sine function. (b) Compute the absolute and error for n-1 : 10 at 1, obtained by comparing the output of your function to that of Matlabs built in sine function. (c) Type on the Matlab console format long, then compute the previous error. Repeat the opera tion by typing format short. Comment on the result (quantify the difference) (d) Make three plots of the absolute error: loglog, semilogx, and semilogy. The axes should be properly labeled. If you make plots of the logarithms of the data you will not receive full credit. (e) Decide which of the three plots gives you a linear error decay. Then use the points n :8, and the polyfit function to obtain the form of the error (f) Write down the theoretical error as a function of n. Compare the result to your actual error (g) What happened to the points n - 9, 10 on your plots?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

% sineTaylor.n %part A 9 function sinx-sineTa if rem (x, pi) 0 sinx- 0; else if abs (x) > 2 * pi yl x- (floor (x/ (2 *pi))) * 2 * pi ; else y1 = x; sinx = 0; for i 0:n sinx = sinx + (-1)싯 * y1.^ (2 * İ+1) eEERE (1+1) = abs (sin(x) - sinx) ; / factorial (2*i+1); en en

copyable code:

% sineTaylor.m

%part A

function sinx=sineTaylor(n,x)

if rem(x, pi) == 0

sinx = 0;

  

else

if abs(x) > 2 * pi

y1 = x - (floor(x/(2*pi))) * 2 * pi;

else

y1 = x;

end

  

sinx = 0;

  

for i=0:n

sinx = sinx + (-1)^i * y1.^(2*i+1) / factorial(2*i+1);

error(i+1) = abs(sin(x) - sinx);

end

end

i = 0:n;

%part D

figure;

loglog(i,error,'o-');

title('log log plot');

xlabel('n');

ylabel('error');

figure

semilogx(i,error,'o-');

title('semi logx plot');

xlabel('n');

ylabel('error');

figure

semilogy(i,error,'o-');

title('semi logy plot');

xlabel('n');

ylabel('error');

%main.m

clear all

%part C

format long

%part B

%prompt = 'Input n : ';

n = 10;

%prompt = 'Input x : ';

x = 1;

% calling function sine_Taylor

sinx=sineTaylor(n,x);

fprintf('approximate value = %f \n', sinx);

%matlab built in function for sin

y = sin(1);

%Therotical error

%part F

error = y - sinx;

%part E

%d=polyfit(error)

fprintf('error is %f \n', error);

Add a comment
Know the answer?
Add Answer to:
Write a Matlab function called sine_Taylor that inputs a positive integer n, a number x, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Taylor polynomial approximation pn (r) for f(x) = sin(x) around x,-0 is given as follows: TL ...

    The Taylor polynomial approximation pn (r) for f(x) = sin(x) around x,-0 is given as follows: TL 2k 1)! Write a MATLAB function taylor sin.m to approximate the sine function. The function should have the following header: function [p] = taylor-sin(x, n) where x is the input vector, scalar n indicates the order of the Taylor polynomials, and output vector p has the values of the polynomial. Remember to give the function a description and call format. in your script,...

  • 1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector...

    1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector containing the values of the integral (A) for n= 1,2,3,..., n. The function must use the relation (B) and the value of y(1). Your function must preallocate the array that it returns. Use for loop when writing your code. b) Write MATLAB script that uses your function to calculate the values of the integral (A) using the recurrence relation (B), y(n) for n=1,2,... 19...

  • Task 2: (1 Mark) - Scripts Write a MATLAB script which plots the absolute error between...

    Task 2: (1 Mark) - Scripts Write a MATLAB script which plots the absolute error between your mySqrt() function and the built-in sqrt() function for a variety of N values with: N=1:20 • n=323158 err-le-3. Your plot should clearly show that error decreases until "saturating" somewhere below err. Because the algorithm "converges” (increased in accuracy) very quickly the true accuracy will be several orders of magnitude better than the err value. You should use semilogy () instead of plot() to...

  • MATLAB Write a function called next_prime that takes a scalar positive integer input n. Use a whi...

    MATLAB Write a function called next_prime that takes a scalar positive integer input n. Use a while-loop to find and return k, the smallest prime number that is greater than n. Feel free to use the built-in isprime function (do not use nextprime). MATLAB

  • in MATLAB 3. Write a function called gauss_seidel that inputs an n x n matrix, A,...

    in MATLAB 3. Write a function called gauss_seidel that inputs an n x n matrix, A, a column vector, b, an initial guess xo), an error tolerance e, and a maximum number of iterations, and output an approximate solution obtained using the Gauss-Seidel method, the error and the number of iterations. The header should look like (x, err, N] = gauss_seidel (A, b, x0, tol, Nmax). Use the method to find approximate solutions to the linear system -2 1 0...

  • Write a program in Python that accepts as input an integer N and a real number...

    Write a program in Python that accepts as input an integer N and a real number c, and outputs the coefficient of the Nth degree term of the Taylor series for the function f(x) = ex centered at c. This process should recur until the user enters a quit code. Erroneous input should be excepted as necessary, and should result in a new prompt.

  • Use matlab please. Exercise 2 Use the functions you coded in Exercise 1 to compute the...

    Use matlab please. Exercise 2 Use the functions you coded in Exercise 1 to compute the numerical approximation of the integral .1 cos e 30 To this end, write a Matlab/Octave function function [en,et , es] test-integration() = that returns the following items: em, et, es: row vectors with components the absolute values of the integration errors llref-Inl n=2.3, . . . . 100 obtained with the midpont (vector em), trapezoidal (vector et) and Simpson (vector es) rules. Here, f...

  • Can you please do this in matlab Consider the following function: 4 0 Using the following...

    Can you please do this in matlab Consider the following function: 4 0 Using the following parameters in your functions: func: the function/equation that you are required to integrate a, b: the integration limits n: the number of points to be used for the integration : Integral estimate A. Write a function capable of performing numerical integration of h(x) using the composite B. write a function capable of performing numerical integration of h(x) using the composite C. Calculate the absolute...

  • (Matlab) Suppose we have a function “hw5f.m” that takes as input x and outputs the value for a function f(x). Write a Matlab program that inputs: • interval [a, b]; • m, the number of data points wit...

    (Matlab) Suppose we have a function “hw5f.m” that takes as input x and outputs the value for a function f(x). Write a Matlab program that inputs: • interval [a, b]; • m, the number of data points with evenly spaced nodes from x1 = a to xm = b, and values from f(x); • location z satisfying x2 < z < xm−1, where h = (b − a)/(m − 1); and outputs the value of the interpolaton polynomial using only...

  • matlab The error function is a mathematical function that frequently arises in probability and statistics. It also c...

    matlab The error function is a mathematical function that frequently arises in probability and statistics. It also can show up in the solution to some partial differential equations, particularly those arising in heat and mass transfer applications. The error function is defined as 2 e-t dt picture attached This function is actually built-in to MATLAB as the command erf, and here we'll use that function to compute a "true value" with which we can compare results of two interpolation approaches....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT