On 25 Aug., 04:49, "Jac " <jac_dom...@hotmail.com> wrote:
> This question is in a previous one of my posts, however the issue has evolved so i thought i would start again highlighting the new issue. The question is as follows:
>
> You have been tasked with developing an Mfile that allows other engineers to
> quickly determine the maximum n needed to reduce the truncation error below an
> error threshold. The truncation error is the absolute error between the
> approximation and MATLABs sin() function.
>
> I find the maximum n value using Lagrange's remainder theorem, which works fine as far as i can tell. However when i try and find the truncation error for all values of x between pi and pi, i get a tiny number nowhere near that of the threshold as it's maximum.
>
> Here is my code:
>
> %The theory used is the Lagrange remainder theorem. This theory can
> %calculate the error of a Taylor series after n terms. By using this
> %theorem the maximum value of n can be found.
>
> %Firstly an error limit must be found, which is given by the user.
> Threshold = input('Please enter the error threshold:');
>
> %The Lagrange theorem can be simplified as the nth derivative is <= 1 for
> %all values of n. Hence this part can be taken as 1. x0 can be equated to 0
> %as sin(0) = 0
> %This leaves us with the equation (x^(n+1))/factorial(n+1)
> %Which is equivalent to (x/1)*(x/2)...(x/(n+1))
>
> %From this final equation it can be seen that the greatest error will occur
> %at pi and pi. Hence pi has been substituted into my equation.
> %Now in this final form a while loop can be used.
> estimate_remainder = 1; %Starting point is 1 as it is multiplying itself by itself
> n = 1;
>
> while estimate_remainder >= Threshold
> estimate_remainder = estimate_remainder*(pi/n)
> n = n+1; %This adds 1 to the value of n everytime the loop repeats
> end
> %clear all information from loops, as it will be printed later
> clc
>
> %Value of n has been calculated, now a approximation function must be
> %formed using this value of n, and then given range for x.
> x = pi:0.01:pi;
>
> %This has been done by updating the code from lab2 using loops.
> %sin approx must first be defined.
> sin_approx = x; %This equates to the summation formula when n = 0
>
> for i = 1:n
> sin_approx = sin_approx + (((1)^i) / factorial(2*i + 1)) *(x.^(2*i+1));
> end
> %Now we can find a vector for the truncation error
> trunc_error = abs(sin_approx  sin(x))
>
> Need help with this urgently! Would be ever so greatful if someone could point out my mistake, thanks.
Your Taylor series summation goes up to (2*n+1), not up to n.
You will have sum up the series up to the smallest integer greater or
equal n/21.
Best wishes
Torsten.
