Why can't i plot a graph for a against t?

k_n= 7*10^-11;
n= 2;
i=1;
t(i)=0; %in hours
a = sym('a')
while t<=2.5 %2.5hours
a= 1-exp(-k_n*(t(i))^n); %degree of hydration
dt=1/3600; %1sec interval
t(i+1)=t(i)+dt;
i=i+1;
end
figure(1);
plot(t,a,'r'),xlabel('t [h]'),ylabel('degree of hydration');
hold on;

 Respuesta aceptada

madhan ravi
madhan ravi el 15 de Mzo. de 2019
Editada: madhan ravi el 16 de Mzo. de 2019
k_n= 7*10^-11;
n= 2;
dt=1/3600; %1sec interval
t=0:dt:2.5;
a= 1-exp(-k_n*(t).^n); %degree of hydration
t=0:dt:2.5;
figure(1);
plot(t,a,'r'),
xlabel('t [h]')
ylabel('degree of hydration')

2 comentarios

cubehz94
cubehz94 el 15 de Mzo. de 2019
thanks so much, still don't understadn why mine doesnt work though
madhan ravi
madhan ravi el 16 de Mzo. de 2019
Editada: madhan ravi el 16 de Mzo. de 2019
Refer links:
Also do a course in Matlab website called MATLAB onramp which takes just few hours to complete and is free.
Ok see which was the mistake you made with a small example:
for k=1:5
a=k; % you kept overwriting the variable in each iteration
end
% how it should be done:
a=zeros(1,5); % pre-allocate
for k=1:5
a(k)=k;
% ^^^- this is how you save values in each iteration
end
Note: Your task can be done trivially without loop. Vectorization method is preferrable than a loop.

Iniciar sesión para comentar.

Más respuestas (1)

Rik
Rik el 15 de Mzo. de 2019
Here are some variations you could try:
k_n= 7*10^-11;
n= 2;
i=1;
dt=1/3600; %1sec interval
t=zeros(1,floor(2.5/dt)); %in hours
a=zeros(size(t));
while t(i)<=2.5 %2.5hours
a(i)= 1-exp(-k_n*(t(i))^n); %degree of hydration
t(i+1)=t(i)+dt;
i=i+1;
end
t(i:end)=[];a(i:end)=[];%remove unneeded parts of preallocated space
figure(1);
plot(t,a,'r'),xlabel('t [h]'),ylabel('degree of hydration');
%or simpler:
k_n= 7*10^-11;
n= 2;
fun=@(t) 1-exp(-k_n.*t.^n);
figure(2)
subplot(1,2,1)
fplot(fun,[0,2.5])
%or explicit:
subplot(1,2,2)
t=0:1/3600:2.5;
a=fun(t);
plot(t,a)

2 comentarios

cubehz94
cubehz94 el 15 de Mzo. de 2019
thanks so much,still dont understand why mine doesnt work though
Rik
Rik el 15 de Mzo. de 2019
You were overwriting a on every iteration instead of forming a vector as well. You can compare my code with yours to see the changes I've made.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 15 de Mzo. de 2019

Editada:

el 16 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by