How do i calculate a sum using a function?
古いコメントを表示
Problem description:
Using previous examples make a function with one parameter n, number of integers to sum, and one output, the sum of first n integers: 1+2+3+...+n.
How do i use a function to calculate the sum?
2 件のコメント
KL
2017 年 10 月 14 日
Did you read the documentation? Show us what have you tried, we'd be happy help you
Jonas Fuglsang Hansen
2017 年 10 月 14 日
採用された回答
その他の回答 (2 件)
jean claude
2017 年 10 月 14 日
編集済み: jean claude
2017 年 10 月 14 日
as i understand you want to sum 1+2+...n so you can use
function S = Ma_somme(n)
S = 0 ;
for i = 1 : n
S = S + i ;
end
end
3 件のコメント
John D'Errico
2017 年 10 月 14 日
Please don't give explicit code like this for obvious homework. That does not help the student. They need to learn, which means they need to make some effort.
Jonas Fuglsang Hansen
2017 年 10 月 14 日
編集済み: Jonas Fuglsang Hansen
2017 年 10 月 14 日
Jan
2017 年 10 月 14 日
To run a function, you cannot just hit the "Run" button, but you save it to a file and call it from Matlab's command window with providing an input:
Result = Ma_somme(19)
@jean claude: Please consider John's advice in the future. It is an advantage for the student and for the forum. Thanks.
Image Analyst
2017 年 10 月 14 日
Hints
function theSum = ComputeSum(n)
theSum = .......
and check this out, and learn from what you see:
vec = 1 : 10;
Also, are you allowed to use the built-in sum() function? And never use "sum" for the name of your variable or you'll destroy the built-in function.
6 件のコメント
Jonas Fuglsang Hansen
2017 年 10 月 14 日
KL
2017 年 10 月 14 日
So this link shows you how to create and save the function as an m-file. As Image Analyst shows, this function should accept an "input" (n).
vec = 1:10
creates a series of numbers from 1 to 10, that is just an example. You use this like
vec = 1:n
inside your function m-file.
Jonas Fuglsang Hansen
2017 年 10 月 14 日
because you're passing 'n' to the function so MATLAB knows what 'n' means. If you do the same thing outside, where you haven't defined 'n' yet, then it's not possible for your program to know what 'n' means.
On your command ĺine, try the following,
vec = 1:n
now you'll get an error since the workspace doesn't have a variable called 'n'. Now try
n = 10;
vec = 1:n;
No error, because you just defined 'n' in the previous line. Get it?
Jonas Fuglsang Hansen
2017 年 10 月 14 日
Image Analyst
2017 年 10 月 14 日
編集済み: Image Analyst
2017 年 10 月 14 日
Yes, that should do it.
Since you already got one working version, I'll give my version:
function theSum = ComputeSum(n)
theSum = sum(1:n);
That's it! It uses the built-in sum() function but you didn't specifically disallow it so I used it.
Hint: to fix up your indenting, in the MATLAB editor, type control-a (to select all the code) and then control-i (to fix/standardize the indenting of the lines).
Here's another snippet that might come in handy for you:
% Ask user for one integer number.
defaultValue = 45;
titleBar = 'Enter an integer value';
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Round to nearest integer in case they entered a floating point number.
integerValue = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nTry replacing the user.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end
If we're done, then you can click "Accept this answer" on the one best answer (it only works on one).
カテゴリ
ヘルプ センター および File Exchange で Data Type Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!