Using sample code but returning a blank plot
4 views (last 30 days)
Show older comments
Hello,
I've copied the following code verbatim from a resource I am using. I've included it below. I am able to return a plot, but it is blank. I assume the issue is with lines 24/25, however since it is not my code, I'm having a hard time figuring out how to change it. My assumption is that I need to define r, x and t. This is apparently a population modeling function where r is the Growthrate and t is time. I've attempted to define r = GrowthRate, however this throws an error. I'm wondering how to define my variables to actually get something on my plot. For reference, I would define my skill level as an aspiring novice. I'm using MATLab 2013a.
Thank you in advance for your help
%LogisticMap(N0,StepSize,DiscardRuns,Num2Plot)
%plots long term solutions of the discrete time logistic model
%N0: initial population density [0 1]
%SteSize: smaller step size for more detailed output (0,3]
%Discard Runs: number of loops of model to run before plotting
%Num2Plot: number of iterations to plot
%Example code:
%LogisticMap(0.5,0.01,100,20)
function LogisticMap(N0,StepSize, DiscardRuns, Num2Plot)
clf
hold on
NLoops= DiscardRuns+Num2Plot;
for GrowthRate=1:StepSize:4
N=N0;
for loop=1:DiscardRuns
NNew=LogisticEq(GrowthRate,N);
N=NNew;
end
end
LabelPlot()
%NO,StepSize,DiscardRuns,Num2Plot)
end
function xtp=LogisticEq(r,xt)
xtp=r.*xt.*(1-xt);
end
function LabelPlot
xlabel('x')
title('x_{t+1}= r x_{t} (1-x_{t})')
end
0 Comments
Answers (1)
Daniel Pollard
on 20 Jan 2021
Edited: Daniel Pollard
on 20 Jan 2021
You create a label and a title for a plot, but at no point do you feed it actual data to plot. Why would it do anything but produce a blank figure?
Edit To add some more detail.
These are Matlab functions; in the code you provided there are three of them. The first one, LogisticMap, has no output as such- it goes round a loop, performing the same calculation each time, saving a variable called N as LogisticEq(GrowthRate, N). Each time it loops around, it overwrites the old N, not saving any of them. The function alse produces no output so even if it did something useful, you couldn't use it in its current form.
LogisticEq takes inputs r and xt and outputs xtp = r.*xt.*(1-xt). The output will be a vector with the same number of elements as r and xt; if r and xt have different numbers of elements, it'll return an error.
LabelPlot does what it says it does - it labels a plot. It puts a label "x" on the x-axis, and titles the plot 'x_{x+1}=r x_{t}(1-x_{t})'.
This code on its own does nothing. If you call the functions, you'll produce an output of xtp and a blank figure with a title and an x-axis labelled "x".
If you're aspiring to be a Matlab novice as you say you are, I recommend checking out the Matlab onramp course:
It'll get you up to speed on all the basics of Matlab, and is the starting point for anyone starting out. It's also free!
0 Comments
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!