How can I plot this discrete recursive function?

16 ビュー (過去 30 日間)
Mike AA
Mike AA 2020 年 3 月 4 日
コメント済み: Mike AA 2020 年 3 月 4 日
I wrote a discrete recursive function to model logistic growth. The sole input argument to this function is time. If I pass a single value for time, it will plot it just fine, but if I pass an array of numbers, it will give the following error:
Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding
your available stack space can crash MATLAB and/or your computer.
Here's the function code:
function [ pop] = discrete_logistic( time )
r = 1;
K = 1000;
N0 = 0.1;
if time ==0
pop = N0;
else
pop = discrete_logistic(time-1) + r*discrete_logistic(time-1)*(1-discrete_logistic(time-1)/K);
end
And here's the script which passes time as input:
clear all;
close all;
clc;
t = [1:10];
w = discrete_logistic(t);
plot(t,w,'r*');
shg
I've tried different ways for passing time,

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 3 月 4 日
編集済み: KALYAN ACHARJYA 2020 年 3 月 4 日
function pop=discrete_logistic(time)
r = 1;
K = 1000;
N0 = 0.1;
if time==0
pop=N0;
else
pop=discrete_logistic(time-1)+r*discrete_logistic(time-1)*(1-discrete_logistic(time-1)/K);
end
Main:
t = [1:10];
for i=1:length(t)
w(i)= discrete_logistic(t(i));
end
plot(t,w,'r*');
shg
  3 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 3 月 4 日
It definitely accept the array of data, but what about pop output in the function file.
Mike AA
Mike AA 2020 年 3 月 4 日
If a function accepts an array, the output will be an array too, no? Or do I have to define the output seperately inside the function?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by