Write a program in ML Consider the Taylor expansion of the sin function sin (x)
ID: 3027954 • Letter: W
Question
Write a program in ML
Consider the Taylor expansion of the sin function sin (x) = x - x^3/3! + x^5/5! - x^7/7! +... As you can see, it is also a sum of terms with a couple of twists. First, the signs alternate, second and the terms for even numbers are missing. The formula in sigma notation is sin (x) = sigma_i=0^n (-1)^i/(2 middot i + 1)! x^2 i + 1 where the order-n summation contains the first n + 1 terms of the expansion. Write a linear time ML function sinappx that, given a value n, computes in linear time in n, the Taylor expansion up to order n. As you surely realized, computing (for instance) factorials from scratch for each term would not deliver the desired complexity.Explanation / Answer
Matlab code
function f=sinapp(x,n)
for i=1:n+1
f(i)=sum(((-1)^(i-1)/(factorial(2*(i-1)+1)))*x^(2*(i-1)+1));
end
f
end
example
syms x
n=3
then call function
f=sinapp(x,n)
f =
[ x, -1/6*x^3, 1/120*x^5, -1/5040*x^7]
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.