0

I have written two function in Matlab; one solves for the factorial of the input and the other solves for the nth term of the Fibonacci sequence. I'm now trying to make a script for each function - one to calculate Euler's number to 10 decimals and the other to calculate the reciprocal Fibonacci number to 10 decimals.

Image here

I have created the following two functions for the factorial and Fibonacci sequence, respectively.

function [ nfactorial ] = get_fact( n )
%input a non-negative integer
%output is factorial of that integer
for i=0:n
    nfactorial=prod(1:n);
end

and

function [ F ] = get_fib( k )

if k < 0
    fprintf('positive integer please')
end

k = k + 1;
F(1) = 0;    % 0th Fibonacci term
F(2) = 1;    % 1st Fibonacci term
F(3) = 1;    % 2nd Fibonacci term
i = 4;

while k >= i
    F(i) = F(i-1) + F(i-2);
    i = i + 1;
end
   F = F(k)
end

How would I go about putting these in scripts?

I tried:

for i = 0:n
    e = prod(1 / get_fact( i ))
end

But this returned errors and obviously has no tolerance set so if it did work, it would be infinite. I have never put a function into a script so any help would be appreciated.

wolfcastle
  • 5,850
  • 3
  • 33
  • 46
rubisco_
  • 63
  • 1
  • 7
  • Did you attend the course which gave you this matlab homework? There are some serious semantic issues with your code. – Andras Deak -- Слава Україні Oct 09 '15 at 22:09
  • Yes I am there every class. An example of a function or script was never shown in class... yet this was assigned. – rubisco_ Oct 09 '15 at 22:29
  • Well, *syntactically* your functions would work fine, so would your script call. The problem is that *what those functions are doing is wrong on multiple levels*. So: if you're there on every class, do they ever teach you something? And if they teach you something, do you ever learn something? **Hint**: if your loop over `i` doesn't contain any `i`, then you're probably doing something wrong. – Andras Deak -- Слава Україні Oct 09 '15 at 22:34
  • Like I said, I was never taught how to do this. So obviously it will have flaws which is why I am asking for advice. No need to be condescending. – rubisco_ Oct 09 '15 at 23:02
  • I'm sorry, I didn't mean to be condescending. My grumpyness is mostly directed at your teacher, as you should not make such mistakes if you were taught properly. Really, I didn't mean to offend you, and sorry for coming across this way. – Andras Deak -- Слава Україні Oct 09 '15 at 23:11
  • 1
    Technical note: I see you've made use of [the answers to your other question](http://stackoverflow.com/questions/33046312/while-loop-help-please). On behalf of the others: please consider marking one of those answers as accepted, if they were the ones solving your problem. Also: "this returned errors" is never enough, you should always include the error messages to help us debug your code. – Andras Deak -- Слава Україні Oct 09 '15 at 23:12

1 Answers1

0

The syntax of your functions, and calling your functions is fine in each case. Your functions, on the other hand, have some semantic problems.

In get_fact:

function [ nfactorial ] = get_fact( n )
%input a non-negative integer
%output is factorial of that integer
for i=0:n
    nfactorial=prod(1:n);
end

prod(1:n) will multiply the numbers from 1 to n, see help : and help prod. So it exactly calculates the factorial by itself! No need to have a loop over i: you'll just compute the same thing over and over again. On a more serious note: get_fact(0) will give you 0, which it shouldn't. Prepare your function for this case!

In get_fib:

function [ F ] = get_fib( k )

if k < 0
    fprintf('positive integer please')
end

k = k + 1;
F(1) = 0;    % 0th Fibonacci term
F(2) = 1;    % 1st Fibonacci term
F(3) = 1;    % 2nd Fibonacci term
i = 4;

while k >= i
    F(i) = F(i-1) + F(i-2);
    i = i + 1;
end
   F = F(k)
end

you print a message if k<0, but you don't do anything extra afterwards. This should be an error message of sorts, so you should either use error instead of fprintf, or write F=NaN; return after fprintf to prematurely return from the function (without an official error). Otherwise this function seems fine to me.

Now, if you save get_fact into get_fact.m and get_fib into get_fib.m somewhere on your MATLAB path (which always includes the current directory), you can call them as get_fact(3) and similar for get_fib() as you tried.

But your call is also problematic:

for i = 0:n
    e = prod(1 / get_fact( i ))
end

here you would call again e = prod(1 / get_fact( i )) for each i, in each step overwriting e. You obviously don't want this. You should either define a vector in place of e and sum its contents in the end, or keep incrementing the value of your scalar e in each iteration (but then you have to initialize it to 0). And prod(1/get_fact(i)) will just give you 1/get_fact(i) since this quantity is a scalar. You would need a sum in any way, and you would have to do it in a different way (see a few sentences earlier).

The tolerance your teacher speaks about is related to the cut-off of the loop where you increment the value of e, i.e. the approximation to the infinite sum. First you can just take a given maximum order of the approximation, and if it works as it should then you can work on implementing the tolerance.