"Allowed to use any platform" > Octave desktop app, Octave Online, MATLAB App, or MATLAB Online.
Create a script or a function to solve the following problem.
The irrational number π can be (indirectly) approximated by an infinite series, that is
π4=1-13+15-17+…
The script or function should feature the following:
the user can input the number of terms of the infinite series n ,
compute the sum of n terms of the infinite series,
compute the absolute difference (error) of the sum of n terms from
the actual Octave value of π ,
for the user input n>1 , plot the sum and the error against the
number of terms added, together with the reference line for π ,
and
display the sum of n terms and the absolute difference between the
sum of n terms and π. The sum of n terms must be displayed using
fixed point format with 16 significant figures, while the error be
displayed using fixed point format with 5 significant figures.
"Allowed to use any platform" > Octave desktop app, Octave Online, MATLAB App, or MATLAB Online.
Create a script or a function to solve the following problem.
The irrational number π can be (indirectly) approximated by an infinite series, that is
π4=1-13+15-17+…
The script or function should feature the following:
•The user can input the number of terms of the infinite series n ,
•compute the sum of n terms of the infinite series,
•compute the absolute difference (error) of the sum of n terms from the actual Octave value of π ,
•for the user input n>1 , plot the sum and the error against the number of terms added, together with the reference line for π , and
•display the sum of n terms and the absolute difference between the sum of n terms and π. The sum of n terms must be displayed using fixed point format with 16 significant figures, while the error be displayed using fixed point format with 5 significant figures.
n=input('Enter n: ');
N=0:(n-1);
series = (-1).^N./(2*N+1);
approx = 4*cumsum(series);
er=abs(approx-pi);
if n>1
plot(1:n,approx,1:n,er,[1,n],[pi,pi])
legend('Approximation','Error','\pi','location','best')
grid on
end
fprintf('Sum of n terms:
%.15f\n',round(approx(end),16,'significant'))
fprintf('Error after n terms:
%.5f\n',round(er(end),5,'significant'))
Get Answers For Free
Most questions answered within 1 hours.