Displaying help
JDownward
Wales, United Kingdom
I have been trying to figure out where I am going wrong on two files I have being trying to write for an assignment the first being
and the second being
the second I managed to put together by browsing this forum and changing a code I found to do what my assignment asks I mention this so that I don't offend anyone
the problem I have with both of them is they get to the levels I want them to but then display the next step in the equation
for example the 1st code is not to display if the answer is greater than 2000 I have tried various ways of trying to make it stop at the 17th term the 18th taking the answer to 2109 but to no avail
and similarly the second task is meant to stop when the term is less than 10^-6
I may well be reading my assignment wrongly and the second code may be right
the exact terming of it is the series should end when the last term is less than 10^-6 which it does
if there is a way to not display the last step but one before it I would be most grateful if someone could point me in the right direction of something to read or explain to me the way to do it
Thanks
John
i=0; sum=0; while sum <= 2000; sum=sum+i^2 i=i+1 end; disp(i); if sum >=2000 end;
and the second being
x=input('Enter x: '); %the power of e to be calculated y=10^(-6);% how accurate it is to calculate it to n=-1; expo=x^(n+1)/factorial(n+1);% the algebraic equation e_ans=expo; while expo > y n=n+1; % add another term. expo=x^(n+1)/factorial(n+1); % evaluate the new term. e_ans=e_ans+expo; end; disp('solution of e^x:'); disp(e_ans); disp('Number of terms in:'); disp(n); disp('Accuracy achieved:'); disp(expo);
the second I managed to put together by browsing this forum and changing a code I found to do what my assignment asks I mention this so that I don't offend anyone
the problem I have with both of them is they get to the levels I want them to but then display the next step in the equation
for example the 1st code is not to display if the answer is greater than 2000 I have tried various ways of trying to make it stop at the 17th term the 18th taking the answer to 2109 but to no avail
and similarly the second task is meant to stop when the term is less than 10^-6
I may well be reading my assignment wrongly and the second code may be right
the exact terming of it is the series should end when the last term is less than 10^-6 which it does
if there is a way to not display the last step but one before it I would be most grateful if someone could point me in the right direction of something to read or explain to me the way to do it
Thanks
John
0
Comments
What you need to do is determine a looping condition and/or looping structure that satisfies your conditions; for instance "if the value after the loop operation exceeds some value, don't go into the loop."
-drasnor
I will go look at setting loop conditions
cheers
John