I am not really sure how to get started with this function.... What would be rea
ID: 3648295 • Letter: I
Question
I am not really sure how to get started with this function....
Explanation / Answer
In order to calculate to the some fixed degree of accuracy, ie – within , we must continue adding terms in the series until the approximation error is smaller than the desired accuracy. The following code illustrates this example: function y = sine_approx(x,error) format long; % This function approximates sin(x) using an infinite series expansion % up to some degree of accuracy. It is called (for example) as follows: % sine_approx(0.25*pi,0.0000000000001) y=x; n=1; remainder=0; real=sin(x); while(abs(y-real)>error) y = y +(-1)^n*(1/factorial(2*n+1))*x^(2*n+1); n_digits = abs(ceil(log10(abs(y-real)))); fprintf(['y = %16.',num2str(n_digits),'g '],y); n=n+1; end end To use this file, save it in MATLABs active directory, and call it command window (see below): >> sine_approx(0.25*pi,0.0000000000001) y = 0.7 y = 0.7071 y = 0.707106 y = 0.70710678 y = 0.70710678118 y = 0.7071067811866
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.