illustrate a special function using scilab

倖福魔咒の 提交于 2019-12-11 18:31:06

问题


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));


来源:https://stackoverflow.com/questions/55131176/illustrate-a-special-function-using-scilab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!