Error calculation and using for loop
35 views (last 30 days)
The cosine function can be evaluated by the following infinite series: cos(x)=1-x^2/2!+x^4/4!-x^6/6!+... Create an M-file to compute cos(1.18 rad) for up to and including number of terms terms, which is up to the term x^8/8!. Your program should compute and display the values of cos x as each term in the series is added. then compute and display true relative error as a percentage..
Abhishek Jain on 4 Oct 2016
You can use the following code:
It prints the value of cos(x) after each step.
elias GR on 4 Oct 2016
Edited: elias GR on 4 Oct 2016
- You need to put your code inside a function, in order x and n to be parameters.
- You can estimate the true value by just using MATLAB's cos function.
- Add figure commands in order to see both plots.
- Usually we take the absolute value of the relative error.
- You need to take care if true=0 (not to divide by zero) - this is not included in the code below
You can call the function like