How to display values of f(1) to f(10)?

1 view (last 30 days)
NOOR YOUNES
NOOR YOUNES on 7 Nov 2018
Edited: madhan ravi on 7 Nov 2018
I have this function:
f(1) =2 f(2)=3
for i=3:1:10
f(i) = 3*(f(i-1)) + 4*(f(i-2))
disp(f(i))
end
And now i want to find the sum of the values of f(2) to f(10). I was thinking of doing this:
sum=0
for n= f(2):f(10)
sum = sum + n
end
But that doesnt work because i just want f(2) + f(3)....+ f(10) and not f(2) + f(2)+1 + f(2)+2...ect
What should i do to make this work? I hope this makes sense.
  1 Comment
NOOR YOUNES
NOOR YOUNES on 7 Nov 2018
I also seem to get 11 values of f instead of 10 which is what i put as i=3:1:10. How do i fix this? Thanks again

Sign in to comment.

Answers (1)

madhan ravi
madhan ravi on 7 Nov 2018
Edited: madhan ravi on 7 Nov 2018
f(1) =2;
f(2) = 3;
for i=3:1:10
f(i) = 3*(f(i-1)) + 4*(f(i-2));
end
fprintf('f = %g\n',f(1:10))
TOTAL_SUM=sum(f(1:10))
command window
>> COMMUNITY
f = 2
f = 3
f = 17
f = 63
f = 257
f = 1023
f = 4097
f = 16383
f = 65537
f = 262143
TOTAL_SUM =
349523
>> size(f)
ans =
1 10
>>
  6 Comments
NOOR YOUNES
NOOR YOUNES on 7 Nov 2018
f(1) =2;
f(2)=3;
for i=3:1:10
f(i) = 3*(f(i-1)) + 4*(f(i-2));
fprintf('f = %g\n',f)
end
TOTAL_SUM=sum(f(2:10))

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!