00:01
We have a sort of coding exercise.
00:05
We are going to take this sign function and consider the mclaren series for that function which goes like x minus x -cube over 3 factorials plus x to the power 5 over 5 factorials minus x to power 7 over 7 factorial and so on.
00:28
We will evaluate the right -hand this series at x equal to 0 .3 times pi and we want our result to be precise or accurate up to the 8 decimal point and we want to know how many terms we need to achieve this accuracy so it's a calling exercise and i have chosen to use this on matlab slash octave compiler because it is free and available to everyone and matlap or octave language is highly simple to work with so my strategy will be as follows first i will write down a function that gives me the end term of this mclaren series then i will write another function that will that will give me the sum of all the terms, all the first n terms actually.
01:44
Then i will consider the difference between the sum of first n plus terms and the first n terms.
01:55
And i will increase this n value until the difference is less than 10 to the power minus 8.
02:09
Because we want to be we want an accuracy of we want the accuracy to wait the eight decimal place okay this our strategy now let's write down our code first i want to see as many decimal places as possible within the machine precision so i will choose this long format then i will write down my function my function my first function that will give me the end term of this series and i will call it literally the end term which is my end term and x value so what is the end term in this mocklen series it is 1 over 2 and 1 this one factorial times there is alternating sign minus 1 to the power n times x to the power 2n minus 1 so let's code this guy over here 1 .0 divide y factorial to m minus 1 times minus 1 to the power n times x to the power and minus 1 that's it now i want another function that will give me the sum of first and terms and i will call this function summed for some reason so i will pick some arpterr variables some s which will indicate my sum and i will start adding all these end turn all the first end terms one by one using a four loop so for k from one to and i remember that mclaren series four signs starts from one not zero so here s equals s plus the k term of this series and i will use the n turn for that and it then and i want this function to return this sum value, so f equal to s.
04:40
Now let us define some difference variable, which i will initiate with a value of 1.
04:51
I want to consider again the difference between the sum of first n plus 1 terms and the sum of first n terms.
05:02
And as i increase the value of n, this difference will get smaller and smaller so it is wise to choose a large difference value because i want to use a while loop i know that this difference will increase as i this difference will decrease as i increase my number of terms in the series so the condition let's write okay not the condition tolerance i want this tolerance to be 10 to power minus 8 so six seven eight when this difference is greater than this tolerance value i want my while loop to end so that i will have determined the number of terms needed to achieve this accuracy or tolerance let's say okay now i need a counting index a dummy index so i will use this letter r for that purpose and it will start from one okay now let's write down the following i will have two terms two variables let's call them term two and turn one so term two will be cool to the sum of first n plus one terms so it will be the greater one at this value of x namely 0 .3 times pi and the first term i mean term one will be the some of first r terms like that.
06:57
Now i will update my difference value and i want to consider the absolute value of this difference because i don't really care about the direction, the sign of this difference.
07:16
All we need is the size as long as it is less than the tolerance.
07:21
We are okay.
07:23
Then i will increase my r value.
07:25
To get to the next step and i want to print all the r values at each step so that i will make sure that this code works...