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;

 채택된 답변

madhan ravi
madhan ravi 2019년 3월 15일
편집: madhan ravi 2019년 3월 16일

1 개 추천

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

cubehz94
cubehz94 2019년 3월 15일
thanks so much, still don't understadn why mine doesnt work though
madhan ravi
madhan ravi 2019년 3월 16일
편집: madhan ravi 2019년 3월 16일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Rik
Rik 2019년 3월 15일

2 개 추천

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

cubehz94
cubehz94 2019년 3월 15일
thanks so much,still dont understand why mine doesnt work though
Rik
Rik 2019년 3월 15일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2019년 3월 15일

편집:

2019년 3월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by