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

1 回表示 (過去 30 日間)
cubehz94
cubehz94 2019 年 3 月 15 日
編集済み: madhan ravi 2019 年 3 月 16 日
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 日
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 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by