Filter löschen
Filter löschen

How to collect a series of value in my code then arrange them into a new array?

1 Ansicht (letzte 30 Tage)
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!!!!!

Antworten (1)

KL
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
YuChe Kao
YuChe Kao am 23 Feb. 2018
Bearbeitet: YuChe Kao am 23 Feb. 2018
Hi KL: First, very thanks for your answer.
I want to draw a plot as a function of 't' and 'G', at the same time I want to collect each 'G' at each 'T'. Then put each 'G' into a new array. I modified my original code to this as below:
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
x(1:101)=G(1:101)
end
disp(x)
plot(t,G);
axis([0,5,0,5]);
xlabel('Time');
ylabel('G')
b=(((4*3.14/3)*1*0.05^3)/60).*x.^3
sum(b)
The output is very close to what I want to have Thank you

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Lighting, Transparency, and Shading 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!

Translated by