MATLAB Answers

Nested loops to estimate cos(pi/4)

조회 수: 5(최근 30일)
I have written this code that is giving me an error:
true = cos(pi/4);
n = 2;
es = .5^(2-n);
x = pi/4;
ea = 1;
est = 1;
k = 1;
for ea >= es
est = 1+x^(k*2)/factorial(k*2)*(-1)^k
ea = abs(true-est)/true*100
k = k+1;
Clearly, I do not understand the relationship of for to while loops. Can anyone help me understand?
Thank you, Mary

  댓글 수: 0

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 9 Dec 2016
  1. You should avoid calling your variable true, as that already has a meaning in MATLAB.
  2. This line of code "for ea >= es" is not valid MATLAB syntax.
  3. Your while loop is an infinite loop; you have not specified a way to break out of it.
Most of your line of code "est = 1+x^(k*2)/factorial(k*2)*(-1)^k" matches the term in the series definition of the cosine function, but the "1+" part doesn't.
Basically you want your code to look something like this pseudocode:
1) initialize variables
2) while (the approximation is not close enough to the known value)
2a) add another term to your approximation
There's a while loop in step 2, but the condition is not 1. There is no for loop in the pseudocode. I can't say much more without simply giving you the code.
In general, if you're still unsure about how while loops work, I think one way you can understand them better is to walk through the first example (calculating factorial(10)) on its documentation page yourself, with you playing the role of MATLAB. Take a piece of paper and a pencil and "execute" each line yourself, recording the values of variables as they change on that paper. [When you reach the end statement, return to the while statement. If the while condition is not satisfied, go to the statement immediately after the end.]

  댓글 수: 1

Mary Jeppson
Mary Jeppson 10 Dec 2016
Thank you for your thorough and conceptual answer. I appreciate it!

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Image Analyst
Image Analyst 9 Dec 2016
You don't need both. You need just one of the other. the for loop is most straightforward in this case:
theta = pi;
trueValue = cos(theta);
numberOfTerms = 10; % Whatever.
theSum = 0;
for k = 0 : numberOfTerms-1
thisTerm = (-1)^k * theta ^ (2*k) / factorial(2*k);
theSum(k+2) = theSum(k+1) + thisTerm;
theError(k+1) = theSum(k+2) - trueValue;
% Make fancy plots.
subplot(2, 1, 1);
plot(theSum, 'b*-', 'LineWidth', 2);
ylabel('The Taylor Sum', 'FontSize', 20);
xlabel('Number of Terms', 'FontSize', 20);
grid on;
subplot(2, 1, 2);
plot(theError, 'b*-', 'LineWidth', 2);
ylabel('The Error from True Value', 'FontSize', 20);
xlabel('Number of Terms', 'FontSize', 20);
grid on;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

  댓글 수: 1

Mary Jeppson
Mary Jeppson 10 Dec 2016
Thank you for this! I am looking closely at it and learning from it. Thank you!

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by