illustrate a special function using scilab

2019-08-17 08:27发布

问题:

I have written a function to find Log(Fn) / n, where Fn is the sequence of Fibonacci numbers F_{n+1} = F_n + F_{n-1}:

function [g] = logf(n)
    u = 0;
    v = 1;
    f = v;
    for i = 2:n do
        f = u + v;
        u = v;
        v = f;
    end
    g = log(f) / n;
endfunction

What I need is to plot this function for 1< n < 200.

回答1:

First all please try to learn MarkDown language to post readable questions. Your first question had some information which was not shown due to the conflicts with StackOverflow's MarkDown rendering.

Secondly try to modularize your code as much as possible and use the convention I have used to have a more readable code. Define a Fibonacci using a for loop:

function y = fibonacci(N)
    select N
    case 0 then
        y = 0;
    case 1 then
        y = 1;
    else
        y0 = 0;
        y1 = 1;
        y = 1;
        for ii = 3:N do
            y0 = y1;
            y1 = y
            y = y1 + y0;
        end
    end
endfunction

Then you have two options:

A. define your function in a vectorized form:

function [g] = logf(n)
    for ii = n do
        g(ii) = log(fibonacci(ii)) / ii;
    end
endfunction

consider that Scilab does not broadcast matrices / lists over functions and you should do that your self.

Now you can plot with:

n = 1:200; 
plot(n, logf(n));

And the result is:

B. Alternatively you could map or broadcast your function over the sequence using feval. Define your function in scalar form:

function [g] = logf(n)
    g = log(fibonacci(n)) / n;
endfunction

and then plot with:

n = 1:200; 
plot(n, feval(n, logf));


标签: plot scilab