Undefined function 'findpeak' for input arguments of type 'double'. Error in untitled4 (line 40) [peaks, t] = findpeak(pred);

1 view (last 30 days)
clc;
clear;
close all;
% Parameters
a = 1; % Attack rate
h = 0.5; % Handling time
b = 0.5; % Conversion efficiency
d = 0.4; % Predator death rate
epsilon = 0.01; % Timescale separation
K = 5; % Carrying capacity
r_values = linspace(0.1, 1, 200);
x0 = 1.0;
y0 = 0.5;
initCond = [x0; y0];
dt = 0.01;
tspan = 0:dt:5000;
tr = 6000/dt; % time to discard as transient
results = [];
rosenzweigMacArthur = @(t, Y, r) [
r * Y(1) * (1 - Y(1)/K) - (a * Y(1) * Y(2)) / (1 + h * Y(1));
epsilon * ((b * a * Y(1) * Y(2)) / (1 + h * Y(1)) - d * Y(2))
];
for i = 1:length(r_values)
r = r_values(i);
opts = odeset('AbsTol',1e-12,'RelTol',1e-10);
[t, Y] = ode45(@(t,Y) rosenzweigMacArthur(t,Y,r), tspan, initCond, opts);
pred = Y(tr:end, 2); % discard transient
[peaks, t] = findpeak(pred);
if isempty(peaks)
% no oscillation detected → steady state
results(end+1, :) = [r, pred(end)];
else
% oscillatory: store unique peaks
up = unique(round(peaks,5));
for k = 1:length(up)
results(end+1, :) = [r, up(k)];
end
end
end
% Plotting bifurcation diagram
figure('Color','w');
plot(results(:,1), results(:,2), 'k.', 'MarkerSize', 6);
xlabel('Prey Growth Rate (r)', 'FontSize', 12);
ylabel('Predator Population Peaks or Fixed Points', 'FontSize', 12);
title(sprintf('Bifurcation Diagram using findpeaks (K = %.1f)', K), ...
'FontWeight','bold','FontSize',13);
grid on;
% Save figure
saveas(gcf, 'bifurcation_diagram_peaks.png');

Answers (1)

Mathieu NOE
Mathieu NOE on 28 May 2025
Moved: Walter Roberson on 29 May 2025
it's findpeaks with an "s" at the end (missing in your code)

Tags

Community Treasure Hunt

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

Start Hunting!