How to collect a series of value in my code then arrange them into a new array?
1 次查看(过去 30 天)
显示 更早的评论
Hi there: My code is below (thanks the help from Birdman for modifying)
t=0:0.05:5; for i=1:numel(t) T(i)=170-22*t(i); if T(i)>=120 G(i)=(3.98*10^7)*exp(-6270/(8.314*(T(i)-30)))*exp(-2.55*10^5/((T(i)+273)*(200-T(i)))); else G(i)=(4.81*10^11)*exp(-6270/(8.314*(T(i)-30)))*exp(-5.51*10^5/((T(i)+273)*(200-T(i)))); end end plot(t,G); axis([0,5,0,5]); xlabel('Time'); ylabel('G')
I want to draw a plot, at the same time, I also want to extract all G(i) and arrange them into a new array, like x=[G(1),G(2),...G(101)], an 1*101 array. I tried to write the code (see below), but I failed. Dose anyone know which parts are incorrect in my code?
x=zeros(1,101); for j=1;101; t=0:0.05:5; for i=1:numel(t) T(i)=170-22*t(i); if T(i)>=120 G(i)=(3.98*10^7)*exp(-6270/(8.314*(T(i)-30)))*exp(-2.55*10^5/((T(i)+273)*(200-T(i)))); else G(i)=(4.81*10^11)*exp(-6270/(8.314*(T(i)-30)))*exp(-5.51*10^5/((T(i)+273)*(200-T(i)))); end end j=i x(j)=G(i) end
Thanks a lot!!!!!
0 个评论
回答(1 个)
KL
2018-2-23
Firstly, you could eliminate the for loop and if statements in your code by using simple indexing.
t=0:0.05:5;
T = 170-22*t;
G = zeros(size(TT));
idx = TT>=120;
G(idx) = (3.98E7).*exp(-6270./(8.314.*(T(idx)-30))).*exp(-2.55*10^5./((T(idx)+273).*(200-T(idx))));
G(~idx)=(4.81E11).*exp(-6270./(8.314.*(T(~idx)-30))).*exp(-5.51*10^5./((T(~idx)+273).*(200-T(~idx))));
plot(t,G);
axis([0,5,0,5]);
xlabel('Time');
ylabel('G')
Now G itself is a separate vector already. I don't understand why you want to save it as x. If you really want to save a copy of it, it's simply,
x = G;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!