Need help fixing a function error

2 ビュー (過去 30 日間)
Chris
Chris 2013 年 9 月 30 日
回答済み: Wayne King 2013 年 9 月 30 日
Alright so have to m files open but every time i try to call up my function it says that the function definition is not allowed in that context. What does the error mean? go slow This is the question (a) Write a function file that accepts the values of r, a and n as arguments and uses a for loop to return the sum of the first n terms of the geometric series. Test your function for a = 3, r = 1=2 and n = 10. (b) Write a function file that accepts the values of r, a and n as arguments and uses the built in command sum to find the sum of the first n terms of the geometric series. Test your function for a = 3, r = 1=2 and n = 10. Hint: Start by defining the vector e=0:n-1 and then evaluate the vector R = r.^e. It should be easy to figure out how to find the sum from there. and this is what i have
a=3;
r=1/2;
g=0;
for n = 0:10
fx=geometricfunction
end
and my function file
function g=geometricfunction(a,r)
fx=g+a.*r.^n
end
I am still lost right now and dont have any idea what to do

回答 (1 件)

Wayne King
Wayne King 2013 年 9 月 30 日
You don't want to call the function inside of a for loop, you want to write the loop inside of the function
Also, Matt J started to try and help you here:
So please do not make a new post when you have an active post for the same question.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by