How to display the nth term where the nth term is found by adding x number previous term?

3 views (last 30 days)
I am trying to write code that will ask the user to input n and x. The sequence is similar to the fibonacci sequence, but instead of adding the previous 2 terms, I want to find the next term by adding the previous x number of terms. I then want it to display the nth term of this sequence, also inputted by the user.

Answers (1)

John D'Errico
John D'Errico on 19 Aug 2021
Admit it. This is homework, since nobody would ask you to do this if it were not. And since it is homework, NOBODY should write the code for you. It is YOUR homework.
But I'll give you some hints.
  1. Can you create a vector of length x? I hope so.
  2. Can you sum all the elements in a vector? Again, if you cannot, then did you fall asleep in class that month?
  3. Can you now create a NEW vector, that contains elements 2:x of the vector, and then append the sum you formed in step 2?
  4. Can you iterate steps 2:3 until you are done? Hint, what does a loop do?

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!