How can I solve the error for plot?
15 次查看(过去 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!