How to Make a function accept vector inputs
이전 댓글 표시
function Ml = myseries(t)
n=0;
while (n<10)
Ml = sum(t.^n/(gamma(n + 1)));
n = n+1;
end
The above code runs well as long as the input variable is a scalar; for vectorized inputs, it fails. What might be wrong?
댓글 수: 4
Azzi Abdelmalek
2013년 12월 21일
What "It fails" means? is there any error message?
John D'Errico
2013년 12월 22일
It isn't a question of MAKING it accept vector input. ANY function can always accept vector inputs. The issue is your code, making it work with vectors.
Ben
2017년 3월 29일
sum(t.^n/(gamma(n + 1))) needs to have a . before the /
Walter Roberson
2017년 3월 29일
./ would only be needed if the right hand side, gamma(n+1) was a vector or matrix, but in this context it is a scalar.
채택된 답변
추가 답변 (2개)
Walter Roberson
2013년 12월 21일
Your current code overwrites all of Ml each time through the "while" loop, and so is unlikely to be doing what you want.
for n = 0 : 9
Ml(n,:) = sum(t.^n/(gamma(n + 1)));
end
댓글 수: 4
Chuzymatics Chuzymatics
2013년 12월 21일
Youssef Khmou
2013년 12월 21일
Just a small correction, @Walter, the index must start at 1.
Walter Roberson
2013년 12월 21일
Right, sorry,
for n = 0 : 9
Ml(n+1,:) = sum(t.^n/(gamma(n + 1)));
end
Chuzymatics Chuzymatics
2013년 12월 22일
Image Analyst
2013년 12월 21일
편집: Image Analyst
2013년 12월 21일
I think you can to what you want to do with one of these functions. Not sure what you want to do so pick one:
function theSum = myseries(t)
% Vectorized method.
n = 1:length(t);
theSum = sum(t.^n ./ (gamma(n + 1)));
function theSum = myseries(t)
% For loop method.
theSum = 0;
for n = 1 : length(t)
theSum = theSum + t(n)^n ./ gamma(n + 1);
end
This does something completely different than the other two answers but I think is what you were actually intending, which is trying to sum a series.
댓글 수: 5
Youssef Khmou
2013년 12월 21일
i think the output should be a vector , if it was not the case, then a simple "sum(.....)" should suffice .
Image Analyst
2013년 12월 22일
I'm not so sure. It looks like he's trying to sum a series, and that would give a single summed value. I think he wants the sum of the first 10 terms of t(n)^n/gamma(n+1). A 10 element array t is passed in and n goes from 1 to 10. At least that's how I interpret it. Why do you think he wants a vector?
Youssef Khmou
2013년 12월 23일
it is guess, like i explained, if it was not a vector, no need for a question, a simple "sum(quantity)" is fine .
Image Analyst
2013년 12월 23일
Well he accepted your interpretation so I guess you were right. The Ml(2) was the sum of terms 1 and 2, the Ml(3) was the sum of terms 1, 2, and 3, and the element Ml(N) was term1 + term2 + term3 + term4 +....+termN. I thought he wanted just one sum - the sum of terms 1 through term N - not N sums, but I guess I was wrong.
Youssef Khmou
2013년 12월 23일
ok
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!