Matlab: how to write a function that takes a number n and returns P = 1*1.2*1.4*.... *(1+0.2*(n-1))

Asked

Viewed 37 times

0

How could you create a function called repeat_prod(n) that takes a number n and returns this function P = 1*1.2*1.4*....(1+0.2(n-1))

I’m having a hard time understanding how this function should be and I appreciate any direction.

I tried to:

function 1*1.2*1.4*....*(1+0.2*(n-1)) = repeat_prod(n)

for n:
n = 1*1.2*1.4*....*(1+0.2*(n-1));

end
  • what is this pile of dots "...." ??

1 answer

1

If I understand your question, it’s something you need:

function [P] = repeat_prod(n)
P = 1;
for i = 1:(n-1)
    P = P*(1+0.2*i);
end
end

In defining the function, what is between [] is what will be returned, what is between () are the inputs of the function. O is defined as follows::

for contador = inicio:passo:final

but you can omit the step, which then will be the default value 1. Both to terminate the for, and to terminate the entire function, you must write end. Pay attention to the last value you want to iterate, if it will be n or n-1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.