fprintf no values in DAT file
Show older comments
This code is not generating values in the temperatureparallel.dat file. I believe it has to do with %10.6f. It is graphing properly. Any help is greatly appreciated.
clc
close all
fid=fopen('.\temperatureparallel.dat','w');
fprintf(fid,'variables = "t","tfire","ts1","ts2","ti","ta"\r\n');
fprintf(fid,'zone\r\n');
g=9.81;
h=1.0; % m height of wall
%set properties
%carbon steel 1
cps1=502.48;
ks1=16.27;
rhos1=8030.0;
As1=0.0025; %Area of steel exposed to tf
%carbon steel 2
cps2=502.48;
ks2=16.27;
rhos2=8030.0;
%insulation
cpi=711.756; %(J/kg-K)
ki=0.033;
rhoi=32.04;
Ai=0.9975; %Area of insulation exposed to tf
%cpi=473.0d0
%ki=43.0d0
%rhoi=7801.0d0
%air
cpa=1006.43;
ka=2.42E-2;
rhoa=1.2041;
rmua=1.7894E-5;
rnua=rmua/rhoa;
alpha=ka/(rhoa*cpa);
pra=rnua/alpha;
Va=1.0; %m^3
%set widths of each section
li=104.0/1000.0; %m
ls1=108.0/1000.0; %m
ls2=4.0/1000.0; %m
la=1.0; %m
%evaluate average of insulation and steel conductivity using harmonic average
kbar=ki*ks1/(ki+ks1);
%set initial conditions
t=0.0;
to=293.15;
tf=0.0;
ts1=to;
ts2=to;
ti=to;
ta=to;
%set time parameters and initial conditions
tfinal=3600.0;
nt=100.0;
dt=tfinal/nt;
outputs(1,1:6)=[t,tf,ts1,ts2,ti,ta];
beta=1.0d0/(ta+273.15);
%write initial conditions
fprintf(fid,'%10.6f %10.6f %10.6f %10.6f %10.6f %10.6f\r\n',t,tf,ts1,ts2,ti,ta);
%update each variable ts1,ts2,ti,ta to the new time step
for j=2:nt+1
ha=hac(h,rhoa,g,ta,ts2,rmua,ka,cpa);
tf=tfire(t,to);
k1s1=dt*rhss1(t,ts1,ta,tf,ks1,rhos1,cps1,li,ls2,ha);
k1s2=dt*rhss2(t,ts2,ti,ta,kbar,rhos2,cps2,ls2,li,ha);
k1i=dt*rhsi(t,ts2,ti,tf,ki,kbar,rhoi,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,cpa,Va,As1,Ai,ha);
ts1=ts1+1.0/6.0*k1s1+1.0d0/3.0d0*k2s1+1.0/3.0*k3s1+1.0/6.0*k4s1;
ts2=ts2+1.0/6.0*k2s2+1.0d0/3.0d0*k2s2+1.0/3.0*k3s2+1.0/6.0*k4s2;
ti=ti+1.0/6.0*k1i+1.0d0/3.0d0*k2i+1.0/3.0*k3i+1.0/6.0*k4i;
ta=ta+1.0/6.0*k1a+1.0d0/3.0d0*k2a+1.0/3.0*k3a+1.0/6.0*k4a;
t=t+dt;
fprintf(fid,'%10.6f %10.6f %10.6f %10.6 %10.6f %10.6f\r\n',t,tf,ts1,ts2,ti,ta);
outputs(j,:)=[t,tf,ts1,ts2,ti,ta];
end
figure(1),clf(1);
t=outputs(:,1);
plot(t,outputs(:,2:end));
%subfunctions start here.
function f1=hac(h,rhoa,g,ta,ts2,rmua,ka,cpa)
beta=1.0d0/(ta+273.15);
deltat=ts2-ta;
pr=rmua*cpa/ka;
gr=h^3*rhoa^2*g*deltat*beta/rmua^2;
ra=gr*pr;
nu=(0.825+0.387*ra^(1.0/6.0)/(1.0+(0.492/pr)^(9.0/16.0))...
^(8.0/27.0))^2;
f1=nu*ka/h;
end
function f2=tfire(t,to)
f2=345.0*log10(((8.00*t)/60)+1.0)+to;
end
function f3=rhsa(t,ts1,ts2,ta,rhoa,cpa,Va,As1,Ai,ha)
f3=((ha*As1)/(rhoa*cpa*Va))*(ts1-ta)+((ha*Ai)/(rhoa*cpa*Va))*(ts2-ta);
end
function f4=rhsi(t,ts2,ti,tf,ki,kbar,rhoi,cpi,ls2,li)
f4=(2*ki)/(rhoi*cpi*li^2)*(tf-ti)-kbar/(rhoi*cpi*li)*(ti-ts2)/(0.50*(ls2+li));
end
function f5=rhss1(t,ts1,ta,tf,ks1,rhos1,cps1,li,ls2,ha)
f5=(2*ks1)/(rhos1*cps1*(li+ls2)^2)*(tf-ts1)-ha/(rhos1*cps1*(li+ls2))*(ts1-ta);
end
function f6=rhss2(t,ts2,ti,ta,kbar,rhos2,cps2,ls2,li,ha)
f6=kbar/(rhos2*cps2*ls2)*(ti-ts2)/(0.50*(ls2+li))-ha/(rhos2*cps2*ls2)*(ts2-ta);
end
2 Comments
Stephen23
on 30 Jan 2019
@Carl Hendrickson: for such a large amount of code it is preferable to upload it by simply clicking the paperclip button.
Answers (1)
I guess, that the file is written correctly, but not in the location you expect:
fid=fopen('.\temperatureparallel.dat','w');
This create the file in the current directory, but maybe this is not the folder you are looking to. Use absolute file names in general:
File = fullile(tmepdir, 'temperatureparallel.dat');
[fid, msg] = fopen(Fiel, 'w');
if fid == -1
error('Cannot open file: %s, %s', File, msg);
end
1 Comment
Carl Hendrickson
on 30 Jan 2019
Categories
Find more on Application Deployment in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!