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;

 Akzeptierte Antwort

madhan ravi
madhan ravi am 15 Mär. 2019
Bearbeitet: madhan ravi am 16 Mär. 2019

1 Stimme

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 Kommentare

cubehz94
cubehz94 am 15 Mär. 2019
thanks so much, still don't understadn why mine doesnt work though
madhan ravi
madhan ravi am 16 Mär. 2019
Bearbeitet: madhan ravi am 16 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Rik
Rik am 15 Mär. 2019

2 Stimmen

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 Kommentare

cubehz94
cubehz94 am 15 Mär. 2019
thanks so much,still dont understand why mine doesnt work though
Rik
Rik am 15 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 15 Mär. 2019

Bearbeitet:

am 16 Mär. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by