Setting up a while loop

12 views (last 30 days)
Zain Malik
Zain Malik on 23 Jul 2018
Commented: Geoff Hayes on 23 Jul 2018
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 Comment
Geoff Hayes
Geoff Hayes on 23 Jul 2018
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.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!