Setting up a while loop

12 vues (au cours des 30 derniers jours)
Zain Malik
Zain Malik le 23 Juil 2018
Commenté : Geoff Hayes le 23 Juil 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 commentaire
Geoff Hayes
Geoff Hayes le 23 Juil 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.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by