Setting up a while loop

12 Ansichten (letzte 30 Tage)
Zain Malik
Zain Malik am 23 Jul. 2018
Kommentiert: Geoff Hayes am 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 Kommentar
Geoff Hayes
Geoff Hayes am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by