Setting up a while loop

13 次查看(过去 30 天)
Zain Malik
Zain Malik 2018-7-23
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
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by