I have a question about plot and for loop

Here is my code:
Ca0 = 2;
k = 0.5;
L = input('What is length of the reactor in ft? ');
for La=1:L+1
Ca(La) = Ca0*exp(-3*k*(La));
end
Ca1 = Ca0*exp(-3*k*L);
disp('The concentration of the reactor at this length in M')
disp(Ca1)
figure(1);
La = 0:L;
plot(La,Ca);
xlabel('L');
ylabel('Ca');
title('Plot of L vs Ca');
When I tried to run the command, matlab said that "Error using plot Vectors must be the same lengths.". How can I deal with that?

回答 (3 件)

Walter Roberson
Walter Roberson 2016 年 5 月 6 日

0 投票

At the end of the "for" loop over variable La, the variable La is left set to the last value it had, not to the entire list of values it had.
Image Analyst
Image Analyst 2016 年 5 月 6 日

0 投票

Your Ca was not calculated with an La of 0, so don't use it - it's deceptive. Use
La = 1 : (L + 1);
plot(La, Ca);
This will also ensure that La and Ca have the same number of elements.
John BG
John BG 2016 年 5 月 6 日
編集済み: John BG 2016 年 5 月 6 日

0 投票

I only get same error message when introducing negative lengths.
If I introduce 10ft or 100ft your script does not return any error.

この質問は閉じられています。

タグ

質問済み:

2016 年 5 月 6 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by