Clear Filters
Clear Filters

Contour plot of R_0 against one parameter and two parameters.

4 views (last 30 days)
Hello everyone,
I am trying to plot contour of R_0 against one and two parameters, I checked the code few times but been unable to detect where actually the error is. Can anyone please help me with plotting R_0 against one and two parameters?
clear, clc, close
% Define the variables
b = 0.19408;
d = 0.957163;
d1 = 0.078622871;
alpha = 0:0.1:1;
gamma = 0:0.1:1;
% The basic reproduction number R_0
R_0 = alpha.*b./ (d^2 + d * d1 + d * gamma);
% Plotting the contour of R_0 against alpha
levels = 0:0.1:1;
contourf(alpha, R_0, levels)
Error using contourf (line 57)
Z must be at least a 2x2 matrix.
colorbar('vert');
xlabel('\alpha');
ylabel('\gamma');
title('Contour Plot of R_0 vs. \alpha');
% Ploting the contour of R_0 against alpha and gamma
contourf(alpha, gamma, R_0, levels)
colorbar('vert');
xlabel('\alpha');
ylabel('\gamma');
title('Contour Plot of R_0 vs. \alpha and \gamma');

Answers (1)

Manikanta Aditya
Manikanta Aditya on 3 Jun 2024
The error you’re encountering is because the contourf function in MATLAB expects a 2D matrix for its second argument, but you’re providing a 1D array. The contourf function is used to create a filled contour plot containing the isolines of matrix Z, where Z contains height values on the x-y plane.
In your case, you’re trying to plot R_0 which is a function of alpha and gamma. Therefore, R_0 should be a 2D matrix where each element R_0(i, j) corresponds to the value of the function at alpha(i) and gamma(j).
clear, clc, close
% Define the variables
b = 0.19408;
d = 0.957163;
d1 = 0.078622871;
alpha = 0:0.1:1;
gamma = 0:0.1:1;
[Alpha, Gamma] = meshgrid(alpha, gamma); % Create a grid of alpha and gamma values
% The basic reproduction number R_0
R_0 = Alpha.*b./ (d^2 + d * d1 + d * Gamma); % Compute R_0 for each pair of alpha and gamma
% Plotting the contour of R_0 against alpha and gamma
levels = 0:0.1:1;
contourf(Alpha, Gamma, R_0, levels)
colorbar('vert');
xlabel('\alpha');
ylabel('\gamma');
title('Contour Plot of R_0 vs. \alpha and \gamma');
This should help!
  2 Comments
Manikanta Aditya
Manikanta Aditya on 3 Jun 2024
clear, clc, close
% Define the variables
b = 0.19408;
d = 0.957163;
d1 = 0.078622871;
alpha = 0:0.1:1;
gamma = 0:0.1:1;
% The basic reproduction number R_0
R_0_alpha = alpha.*b./ (d^2 + d * d1 + d * gamma);
% Plotting the contour of R_0 against alpha
figure; % Create a new figure
plot(alpha, R_0_alpha);
xlabel('\alpha');
ylabel('R_0');
title('Plot of R_0 vs. \alpha');

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!