Rewriting in columns of Excel sheet

조회 수: 2 (최근 30일)
MINATI PATRA
MINATI PATRA 2024년 4월 29일
댓글: MINATI PATRA 2024년 5월 20일
%% After running the code for p1 = 0.01; p2 = 0.0; p3 = 0.0;
%Matlab writes the calculations of 'Cf' and 'Nu' in an excel sheet of Columns 'I' and 'J' respectively.
%% But I want to run the code 03 times with different values (i) p1 = 0.01; p2 = 0.0; p3 = 0.0;
% (ii) p1 = 0.01; p2 = 0.01; p3 = 0.0;(iii)p1 = 0.01;p2 = 0.01;p3 = 0.01;
% (other values are fixed as in Excel sheet)
%% Now I want Matlab to write the values of 'Cf' in the Columns " I, J, K " and the values of 'Nu' in the Columns " L, M, N" respectively. (SAME EXCEL SHEET)
%% Here is my try, please modify
status = mkdir('D:\PK79'); cd D:\PK79
filePath = 'D:\PK79\ADM3A.xlsx'; filename = 'ADM3A.xlsx';
d = readtable(filename);
T = fillmissing(d,"previous");
p1 = 0.01; p2 = 0.0; p3 = 0.0;
K = T.K; M = T.M; Pr = T.Pr; Ec = T.Ec; Q = T.Qe; D = T.D; b = T.b; Bi = T.Bi;
Cp = 1;rf = 7;kf = 0.6;sf = 5.5; C1 = 7;rhos1 = 1;k1 = 4;s1 = 1;
C2 = 5;r2 = 2;k2 = .5;s2 = 2.7; C3 = 6.2;r3 = 2;k3 = .9;s3 = 6.2;
H1 = ((1-p1)*(1-p2)*(1-p3))^-2.5; H2 = (1-p3)*( (1-p2)*( 1-p1 + p1*rhos1/rf ) + p2*r2/rf ) + p3*r3/rf;
H3 = (1-p3)*( (1-p2)*(1-p1 + p1*rhos1*C1/(rf*Cp)) + p2*r2*C2/(rf*Cp) ) + p3*r3*C3/(rf*Cp);
C2 = ( (s1+2*sf-2*p1*(sf-s1))/(s1+2*sf+p1*(sf-s1)));
C3 = ( (s2+2*C2-2*p2*(C2-s2))/(s2+2*C2+p2*(C2-s2)) );
A3 = ( (s3+2*C3-2*p3*(C3-s3))/(s3+2*C3+p3*(C3-s3)) );
B1 = ( (k1+2*kf-2*p1*(kf-k1))/(k1+2*kf+p1*(kf-k1)) );
B2 = ( (k2+2*B1-2*p2*(B1-k2))/(k2+2*B1+p2*(B1-k2)) );
H4 = ( (k3+2*B2-2*p3*(B2-k3))/(k3+2*B2+p3*(B2-k3)) );
N = size(T,1); Cf = zeros(N,1); Nu = zeros(N,1);
for k = 1:N
ODE = @(x,y)[y(2); y(3); y(4);
M(k)*(x+K(k)).^2*(A3/H1).*(y(2) + (x+K(k)).*y(3)) - 2*y(4)./(x+K(k)) + y(3)./(x+K(k)).^2 - y(2)./(x+K(k)).^3 - (H2/H1)*K(k)*((x+K(k)).^2.*(y(1)*y(4) - y(2)*y(3))) - y(1)*y(2) + (x+K(k)).*(y(1)*y(3)-y(2)^2);
y(6); - (Pr(k)/H4)*( Q(k)*(y(5) + exp(-D(k)*x)) + H3*K(k)*y(1)*y(6) + M(k)*Ec(k)*A3*y(2)^2 ) - y(6) ];
BC = @(ya,yb)[ya(1); ya(2)-1-b(k)*(ya(3)-ya(2)/K(k)); ya(6)-Bi(k)*(ya(5)-1); yb([2;3;5])];
xa = 0; xb = 6;
x = linspace(xa,xb,101);
solinit = bvpinit(x,[0 1 0 1 0 1]);
sol = bvp5c(ODE,BC,solinit);
S = deval(sol,x);
Cf(k) = H1*( S(3,1) - S(2,1)/K(k));
Nu(k) = -H4*S(6,1);
end
T.Cf = Cf; T.Nu = Nu;
vars = T.Properties.VariableNames;
T = removevars(T,vars(startsWith(vars,'Var')));
writetable(T,filename,'WriteMode','overwritesheet')
T = readtable(filename) % check the result:
  댓글 수: 2
MINATI PATRA
MINATI PATRA 2024년 4월 29일
편집: MINATI PATRA 2024년 4월 29일
Again same problem arised as before.
I want the results of 'Cf' and 'Nu' in (i) 1st run in columns 'I' and 'L' (ii) 2nd run in columns 'J' and 'M' (iii) 3rd run in columns 'K' and 'N'
Dyuman Joshi
Dyuman Joshi 2024년 4월 29일
@MINATI PATRA, I didn't change your code, I just formatted it properly.

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

답변 (1개)

Joshua Levin Kurniawan
Joshua Levin Kurniawan 2024년 4월 29일
Hello, you can add this lines of code to create 3 loops by simply using for loop with defined matrix below.
% Parameter combinations
params = [0.01, 0.0, 0.0; % (i)
0.01, 0.01, 0.0; % (ii)
0.01, 0.01, 0.01];% (iii)
Then, modify your loop into the following code
% Loop of each combinations (3 in total)
for i = 1:size(params, 1)
p1 = params(i, 1);
p2 = params(i, 2);
p3 = params(i, 3);
H1 = ((1-p1)*(1-p2)*(1-p3))^-2.5;
H2 = (1-p3)*((1-p2)*(1-p1 + p1*rhos1/rf) + p2*r2/rf) + p3*r3/rf;
H3 = (1-p3)*((1-p2)*(1-p1 + p1*rhos1*C1/(rf*Cp)) + p2*r2*C2/(rf*Cp)) + p3*r3*C3/(rf*Cp);
C2 = ((s1+2*sf-2*p1*(sf-s1))/(s1+2*sf+p1*(sf-s1)));
C3 = ((s2+2*C2-2*p2*(C2-s2))/(s2+2*C2+p2*(C2-s2)));
A3 = ((s3+2*C3-2*p3*(C3-s3))/(s3+2*C3+p3*(C3-s3)));
B1 = ((k1+2*kf-2*p1*(kf-k1))/(k1+2*kf+p1*(kf-k1)));
B2 = ((k2+2*B1-2*p2*(B1-k2))/(k2+2*B1+p2*(B1-k2)));
H4 = ((k3+2*B2-2*p3*(B2-k3))/(k3+2*B2+p3*(B2-k3)));
N = size(T, 1);
Cf = zeros(N, 1);
Nu = zeros(N, 1);
% Solve ODEs and calculate Cf and Nu
for k = 1:N
ODE = @(x, y)[y(2); y(3); y(4);
M(k)*(x+K(k)).^2*(A3/H1).*(y(2) + (x+K(k)).*y(3)) - 2*y(4)./(x+K(k)) + y(3)./(x+K(k)).^2 - y(2)./(x+K(k)).^3 - (H2/H1)*K(k)*((x+K(k)).^2.*(y(1)*y(4) - y(2)*y(3))) - y(1)*y(2) + (x+K(k)).*(y(1)*y(3)-y(2)^2);
y(6); - (Pr(k)/H4)*(Q(k)*(y(5) + exp(-D(k)*x)) + H3*K(k)*y(1)*y(6) + M(k)*Ec(k)*A3*y(2)^2) - y(6)];
BC = @(ya, yb)[ya(1); ya(2)-1-b(k)*(ya(3)-ya(2)/K(k)); ya(6)-Bi(k)*(ya(5)-1); yb([2;3;5])];
xa = 0;
xb = 6;
x = linspace(xa, xb, 101);
solinit = bvpinit(x, [0 1 0 1 0 1]);
sol = bvp5c(ODE, BC, solinit);
S = deval(sol, x);
Cf(k) = H1*(S(3,1) - S(2,1)/K(k));
Nu(k) = -H4*S(6,1);
end
% Write the result
col_offset = (i - 1) * 3;
T{:, {'Cf', 'Nu'}} = [Cf, Nu];
writetable(T, filename, 'WriteMode', 'overwritesheet', 'Range', ['I1:N' num2str(N + 1)]);
end
  댓글 수: 6
MINATI PATRA
MINATI PATRA 2024년 5월 3일
편집: MINATI PATRA 2024년 5월 7일
I also saw late, thats why late response.
Anyway thanks for your involvement. Please see the excel sheet attachment (Previously I have attached without UPLOADING, meaningless).
The columns ' I, J, and K ' for Cf; ' L, M, and N' for NU; should be filled up for three runs (i), (ii) and (iii), mentioned eariler in your code.
Please have a look at the excel sheet and modify if anything needed according to you to make it success. We will then close the thread.
MINATI PATRA
MINATI PATRA 2024년 5월 20일
Any idea

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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by