What's wrong with the folow function?

I made a function in order to calculate a system of differential equations. I'm using Euler's method.
i=0;
C= 1; % Reagent's initial concentration
T=15; % Initial temperature
dC= -exp(-10/T+273)*C;
dT= 1000*exp(-10/(T+273))*C-10*(T-20);
h = 4;
ti = 0; % time=0
tf = 8; % end time of the simulation
niteracoes = round((tf-ti)/h); % number of steps
vC= zeros (niteracoes);
vT= zeros (niteracoes);
for i = 1:niteracoes
C(i+1) = C(i) - h*dC(i,i); % Euler's method
T(i+1) = T(i) - h*dT(i,i); % Euler's method
ti = ti + h; % Increment time
vC(i)=C(i+1);
disp(vC)
vT(i)=T(i+1);
disp(vT)
i=i+1;
end
Error in euler (line 16) C(i+1) = C(i) - h*dC(i,i);

 採用された回答

Walter Roberson
Walter Roberson 2013 年 12 月 21 日

0 投票

You start dC as a single element, and you do not expand it in your "for" loop, so it does not have two elements by the time "i" becomes 2.

2 件のコメント

Mariana
Mariana 2013 年 12 月 21 日
So, what I have to do?
Image Analyst
Image Analyst 2013 年 12 月 21 日
Perhaps you could answer the question I asked in my Answer "What are you thinking that the two indexes of dC should correspond to?"

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2013 年 12 月 21 日

0 投票

When you first get to this line:
C(i+1) = C(i) - h*dC(i,i);
dC is just a single number - it's not a 2D array. What are you thinking that the two indexes of dC should correspond to?

カテゴリ

質問済み:

2013 年 12 月 21 日

編集済み:

2013 年 12 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by