Troubleshooting MATLAB Loops: Solving a Logmap Function Error

  • Thread starter Thread starter elliottmarter
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The discussion focuses on troubleshooting a MATLAB function for logistic mapping, specifically the implementation of a loop to iterate the calculation of population size. The user, ElliottMarter, initially struggled with using "for" and "while" loops in MATLAB to repeatedly apply the logistic mapping formula. A solution was provided by BerryBoy, demonstrating a "for" loop that successfully iterates the calculation 100 times, updating the population size each iteration. The final function effectively returns the population size after the specified number of iterations.

PREREQUISITES
  • Understanding of MATLAB programming language
  • Familiarity with logistic mapping concepts
  • Knowledge of iterative programming structures (loops)
  • Basic understanding of function definitions in MATLAB
NEXT STEPS
  • Explore advanced MATLAB functions for optimization
  • Learn about MATLAB's vectorization techniques to improve performance
  • Investigate error handling in MATLAB for robust coding
  • Study population dynamics models beyond logistic mapping
USEFUL FOR

Students and researchers in computational biology, MATLAB programmers, and anyone interested in modeling population dynamics using iterative functions.

elliottmarter
Messages
13
Reaction score
0
Hi guys, sorry if i didn't follow the template but its more of a quick query than proper laid out question, but it is part of my coursework...anyway here goes;

I'm having a bit of trouble with an m-file in matlab, i posted earlier but i appreciate that post was too vague for a response.

basically i have an m-file which goes like this

function [y]=logmap(lam, x)
%calculate population size, y, next year using:
%lam: effective growth rate of population (between 0-4)
x: current size of population
using the logistic mapping formula
y=lam*x*(1-x)

but what I really need is for it to loop, so that y goes back into it as the x value and then just keeps repeating it.
does anyone know the commands to make it do this, the "for" and "while" loops don't seem to have the right inputs for the situation, also i need to define a value, n, which is the amount of times i want it to loop.

can someone please give me a hand?
thanks
elliottmarter
 
Physics news on Phys.org
Code:
function [y]=logmap(lam, x)
%calculate population size, y, next year using:
%lam: effective growth rate of population (between 0-4)
x: current size of population
using the logistic mapping formula

% Number of loops
n = 100;% Loop n times
for ii = 1:n
    x = lam*x*(1-x)
end

y = x;
end

Let me know if this helps
 
Last edited:
this worked fine, but i managed a different solution before i saw your post, thanks all the same BerryBoy :)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
23K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K