How to collect a series of value in my code then arrange them into a new array?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
KL
am 23 Feb. 2018
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;
1 Kommentar
Siehe auch
Kategorien
Mehr zu Parallel Computing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!