Infinite series for pi
이전 댓글 표시
Hi I was wondering how I would go about writing a program for this infinite series of pi, so that the script works and I am would be able to plot a graph form it? http://upload.wikimedia.org/math/3/7/0/3706d96d8b2b4281a22700f2b642a5ad.png
I am new to Matlab Student version and having a hard time with the series problems. than you all and have a great day
답변 (1개)
Youssef Khmou
2013년 2월 6일
편집: Youssef Khmou
2013년 2월 7일
hi, You can use loops to get the value pi=22/7, so it is an infinite serie, you specify in your software the format short to show 4 digits after the decimal point
example :
format short
f=2.6589
You implement the loop :
% Infinite series for PI
PI=0;
%tolerance=1e-3;
n=1;
N=6351;
v=zeros(N+1,1);
for n=0:N
PI=PI+4*(((-1)^n)/(2*n+1));
v(n+1)=PI;
%n=n+1;
end
PI=PI;
figure, plot(v)
figure, plot(v(1:40)), hold on, plot(0:0.1:40,pi,'r-')
댓글 수: 6
ChristianW
2013년 2월 7일
Or using vector instead of for-loop.
n = 0:10000000;
PI = 4*sum((-1).^n./(2*n+1));
But the loop is needed to plot the evolution of PI:
N = 100; % number of n-steps
nv = 0:N-1; % n vector
PI = ones(1,N); % preallocate PI
PI(1) = 4*1; % initiate: PI(1) = PI(n=0) = 4*1
for n = 1:N-1
PI(n+1) = PI(n)+4*(-1)^n/(2*n+1);
end
plot(nv,PI,'k'); xlabel('n')
Youssef Khmou
2013년 2월 7일
right, also loops must be used to evaluate the tolerance like "while the quadratic error is > than tolerance"
AngelsaAtWar
2013년 2월 7일
Youssef Khmou
2013년 2월 7일
i edited the code, it plots two graphs : first for 6351 iterations, second only for 40 iterations,
ChristianW
2013년 2월 7일
Your x-axis is not n , its n+1 , the v-indices. (v(1) = v(n=0))
Youssef Khmou
2013년 2월 7일
@Christian : improve the code !
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!