Setting up a while loop
조회 수: 12 (최근 30일)
이전 댓글 표시
For an introduction to MATLAB, Ive been working on the following problem, however i keep getting the anwser for a_n and b_n as 0
my code is as follows;
n = 0
fprintf('a_n | b_n\n')
a_n = (6/sqrt(3))*((-1)^n/(3^n(2*n+1)));
b_n = 16*((-1)^n/5^(2*n+1)*(2*n+1))-4*((-1)^n/239^(2^n+1)*(2*n+1));
a_n_Error = abs(a_n - pi);
b_n_Error = abs(b_n - pi);
tol = 1e-6;
while a_n_Error >= tol && b_n_Error >= tol
n = n+1;
a_n = (6/sqrt(3))*((-1^n)/(3^n)*(2*n+1));
b_n = 16*((-1^n)/(5^(2*n+1))*(2*n+1)) - 4*((-1^n)/(239^(2^n+1))*(2*n+1));
a_n_Error = abs(a_n - pi);
b_n_Error = abs(b_n - pi);
fprintf('%10.6f | %10.6f\n', a_n, b_n)
end
댓글 수: 1
Geoff Hayes
2018년 7월 23일
Zain - the equations in the attached pdf indicate that you should be adding/summing the a_n from the current iteration with the a_n from the previous iteration
a_n = a_n + (6/sqrt(3))*((-1^n)/(3^n)*(2*n+1));
Same for b_n. You will also need to be careful with the order of operations and your placement of the brackets. Please confirm if the above is actually is how described in the pdf.
답변 (0개)
참고 항목
카테고리
Help Center 및 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!