How can I solve the error for plot?

11 Ansichten (letzte 30 Tage)
Deck Zhan Sim
Deck Zhan Sim am 5 Jan. 2022
Beantwortet: Walter Roberson am 5 Jan. 2022
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')

Antworten (2)

KSSV
KSSV am 5 Jan. 2022
Bearbeitet: KSSV am 5 Jan. 2022
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

Walter Roberson
Walter Roberson am 5 Jan. 2022
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.

Kategorien

Mehr zu Neuroimaging finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by