How can I solve the error for plot?
조회 수: 11 (최근 30일)
이전 댓글 표시
Hi all,
I having the issue of plotting graph. I would be grateful that those who can solve the problem. Thanks!
%% Initialize Input
K = 1*(10^-9); % Carrying Capacity of Brain Tumor
C0 = 40000; % Initial Brain Tumor Population Size
r = 4.31*(10^-3); % Growth Rate of Brain Tumor Cells
%% Model Data
t = 365; % Time in Days
C(1) = C0;
% Calculating Brain Tumor Population
for t=1:1:t-1
C(t+1) = (K*(C0*exp(r*t)))/(K-C0+C0*exp(r*t));
end
hold on
nvec = 1:1:t;
%% Graph Plotting
figure(1)
title( 'Brain Tumor Population Against Time' )
xlabel('Time (Days)')
ylabel('Brain Tumor Population (cells)')
plot(nvec,C,'--bo','LineWidth',0.5,'MarkerSize',5,'MarkerEdgeColor','black')
grid
hold on
Above here is the code and the error is stated as below
Error using plot
Vectors must be the same length.
Error in Untitled (line 24)
plot(nvec,C,'--bo','LineWidth',0.5,'MarkerSize',5,'MarkerEdgeColor','black')
댓글 수: 0
답변 (2개)
KSSV
2022년 1월 5일
편집: KSSV
2022년 1월 5일
clc; clear all ;
%% Initialize Input
K = 1*(10^-9); % Carrying Capacity of Brain Tumor
C0 = 40000; % Initial Brain Tumor Population Size
r = 4.31*(10^-3); % Growth Rate of Brain Tumor Cells
%% Model Data
t = 365; % Time in Days
C = zeros(t,1) ;
C(1) = C0;
% Calculating Brain Tumor Population
for i=2:1:t-1
C(i+1) = (K*(C0*exp(r*i)))/(K-C0+C0*exp(r*i));
end
hold on
nvec = 1:1:t;
%% Graph Plotting
figure(1)
title( 'Brain Tumor Population Against Time' )
xlabel('Time (Days)')
ylabel('Brain Tumor Population (cells)')
plot(nvec,C,'--bo','LineWidth',0.5,'MarkerSize',5,'MarkerEdgeColor','black')
grid
hold on
댓글 수: 0
Walter Roberson
2022년 1월 5일
t = 365; % Time in Days
You set a variable t to 365
for t=1:1:t-1
You set up a for loop. The upper limit is t-1 which is 365-1 so the upper limit is 364. The loop control variable is named t which will cause the t = 365 to be overwritten. Each iteration of the loop, t will be assigned a new scalar. On the last iteration t = 364 will be in effect.
C(t+1) = (K*(C0*exp(r*t)))/(K-C0+C0*exp(r*t));
C(t+1) is written to, and t is up to 364, so at the end, C(365) will be written to.
nvec = 1:1:t;
After the for loop, t has the last value it was assigned, so t = 364. So nvec is 1 to 364.
plot(nvec,C,'--bo','LineWidth',0.5,'MarkerSize',5,'MarkerEdgeColor','black')
nvec is 364 long, C is 365 long.
You would not have had this problem if you had not re-used t as a variable name for different purposes.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Neuroimaging에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!