필터 지우기
필터 지우기

How can I use a for loop to find the summation of a set variable and set number of terms

조회 수: 1 (최근 30일)
So I was given the task of creating a UDF that has to do the following:(1) accepts input values for a variable, x, and a set number of terms N.(2) calculate the value of f(x) using N terms and (3) return output value of f(x). Here is what I have so far:
x = input('variable x');
n = input('number of terms, n');
for N = (0:n)
y = sum(x.^N)
end
When I enter in x=2 and n=6 for example, I know the answer is supposed to be 127. But I am not getting the answers summed up, but separate instead. How do I fix this? Thanks.

채택된 답변

Walter Roberson
Walter Roberson 2018년 3월 7일
total = 0;
...
total = total + y;
  댓글 수: 1
macabe banchero
macabe banchero 2018년 3월 7일
That did it. I tried that earlier but my mistake was having it after "end" instead of before. Thanks again!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by