Contour plot of R_0 against one parameter and two parameters.
조회 수: 1 (최근 30일)
이전 댓글 표시
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)
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');
댓글 수: 0
답변 (1개)
Manikanta Aditya
2024년 6월 3일
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).
Refer this doc: https://in.mathworks.com/help/matlab/ref/meshgrid.html
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
Manikanta Aditya
2024년 6월 3일
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');
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!