I need help writing a Matlab function that takes a positive integer n as its argument, and returns the sum of all odd numbers between 1 and n.

1 Kommentar

Jan
Jan am 15 Jan. 2017
Please clarify, if this is your homework. If so posting a solution might be counter-productive, because you would cheat if you deliver it. Even if it is a homework question, posting what you have tried so far and asking a specific question would be useful. Because asking for explanations is not a cheating instead of letting others solve your work.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

John BG
John BG am 13 Jan. 2017

0 Stimmen

Hi Mateo
what about this
function sn=sum_odds(n)
narginchk(1,1);
if(n<0) || (n==0)
error(message('input error n<=0'));
end
if abs(n-floor(n))>0
error(message('input not +int'));
end
sn=sum([1:2:n])
% if rem(n,2)==0
% sn=sum([1:2:n-1])
% end
%
% if rem(n,2)>0
% sn=sum([1:2:n])
% end
end
if you find these lines useful would you please mark my answer as Accepted Answer
To any other reader, if you find this answer of any help please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG

5 Kommentare

Walter Roberson
Walter Roberson am 13 Jan. 2017
You just gave complete code for a homework question :(
Image Analyst
Image Analyst am 14 Jan. 2017
That assumes it was a homework question. Mateo did not say it was a homework question, either in the body of the message or via a tag. It may well be, but should we assume that? Maybe it's not. Or maybe he's self-learning and this is not a question to be turned in and graded. Every time someone posts something should they say "This is not a homework question" if it is not? A given question might be assumed to be a homework question by some people and not a homework question by other people. I guess it's up to each individual's judgment.
Jan
Jan am 15 Jan. 2017
Bearbeitet: Jan am 15 Jan. 2017
@Image Analyst: You are right. I've removed my comment, which mentioned http://www.mathworks.com/matlabcentral/answers/8626-how-do-i-get-help-on-homework-questions-on-matlab-answers.
For me the question sounds like a homework or Cody problem. Even if this does not concern the OP, this thread is found when others pupils search in the net. Beside this, the OP does not show any own effort like posting code or asking for a specific detail. Therefore I judge that this is a "do-it-for-me" question, which I do not like to answer with code also.
Jan
Jan am 15 Jan. 2017
Bearbeitet: Jan am 15 Jan. 2017
@John BG: The message() command "is intended for MathWorks use only" according to the documentation. In addition it requires a qualified message-ID, therefore your error handling causes another error.
Walter Roberson
Walter Roberson am 15 Jan. 2017
Should we assume that it is a homework question: Yes. Nothing is lost by assuming that for something that looks like homework. The person who posted the question can also provide clarification afterwards that might potentially lead to someone posting a more complete solution, but an overcomplete solution cannot be withdawn.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Jan
Jan am 13 Jan. 2017

3 Stimmen

Do you remember how Gauss calculated the sum over all elements from 1 to n? You can modify the method to use only the pairs of odd numbers. This reduces the compuations drastically and you should never process the dull sum.

3 Kommentare

Stephen23
Stephen23 am 13 Jan. 2017
Bearbeitet: Stephen23 am 14 Jan. 2017
@John BG: note that square brackets are not required: sum(1:1:100).
The mlint messages is ""Use of brackets [] is unnecessary." This might also be of interest:
Jan
Jan am 15 Jan. 2017
Bearbeitet: Jan am 15 Jan. 2017
I do not post the solution, because I assume, that this is a homework question. I even did not mention, that asking an internet search engine for the terms "sum of odd numbers" reveals the main part of the solution already: The sum of N odd numbers is N^2.
@Stephen: The step size 1 is not required in vectors, so this is enough: sum(1:100). This does not influence the computing time but is nicer . The leaner the code, the less chances for a typo... :-)
Assuming N odd,
1 + 3 + 5 +... N
N + (N-2) + (N-4) +... 1
Each pair adds to N+1. Count the pairs and multiply by that. Then divide by 2 because each pair is being counted twice.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Function Creation finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 13 Jan. 2017

Bearbeitet:

Jan
am 15 Jan. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by