필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
YuChe Kao
YuChe Kao 2018년 2월 22일
편집: YuChe Kao 2018년 2월 23일
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!!!!!

답변 (1개)

KL
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;
  댓글 수: 1
YuChe Kao
YuChe Kao 2018년 2월 23일
편집: YuChe Kao 2018년 2월 23일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by