Question: i have these matlab codes :function [ P , A ] = horseshoeFlow ( d , h , n , s ) % Constants %

i have these matlab codes :function [P, A]= horseshoeFlow(d, h, n, s)
% Constants
% Calculate h1
h1=(d./2).*(1-(1+(7^(1/2))/4));
if h >=0 && h < h1
% Case 1: 0<= h < h1
A =(h - d).*(h .*(2* d - h)).^(1/2)+ d.^2.*((sin((h - d)./d)).^(-1)+ pi/2);
Wetp =2* d .*(cos(1- h./d)).^(-1);
elseif h1<= h && h <(d/2)
% Case 2: h1<= h < diameter/2
h2=(d /2)- h1;
C1=1-(1+7^(1/2))/2;
C2= C1/2*(1-(1-(C1^2)/4).^(1/2))- sin(C1/2)^(-1);
A1=(h - d/2).*(h .*(d - h)).^(1/2)+(d^2/4).* sin((2* h - d)/ d).^(-1);
A2= d^2.*(C2+(sin(2* h - d)/(2* d)).^(-1))-(h - d/2).*(d -(d^2-(h - d/2)^2).^(1/2))+ A1;
Wetp1=2* d .*((cos((d -2* h)/(2* d))).^(-1)-(cos(-C1/2)).^(-1));
Wetp2=2* d .*((cos((d -2* h)/(2* d))).^(-1)-(cos(-C1/2)).^(-1))+ Wetp1;
A = A2;
Wetp = Wetp2;
elseif (d/2)<= h && h < d
% Case 3: diameter/2<= h < diameter
h3= d /2;
C1=1-(1+7^(1/2))/2;
C2= C1/2*(1-(1-(C1^2)/4).^(1/2))- sin(C1/2)^(-1);
A2=(h - d/2).*(h .*(d - h)).^(1/2)+(d^2/4).* sin((2* h - d)/ d).^(-1);
A3=(h - d/2).*(h .*(d - h)).^(1/2)+(d^2/4).* sin((2* h - d)/ d).^(-1)+ A2;
Wetp2=2* d .*((cos((d -2* h)/(2* d))).^(-1)-(cos(-C2/2)).^(-1));
Wetp3= d .*((cos(1-(2* h)/ d)).^(-1)- pi/2)+ Wetp2;
A = A3;
Wetp = Wetp3;
else
error('Invalid input for flow depth h.');
end
% Calculate hydraulic radius, velocity, and flow rate
Rh = A / Wetp;
V =(1/ n)* Rh^(2/3)*(s)^(1/2);
Q = V * A;
% Output wetted perimeter and cross-sectional area
P = Wetp;
A = A;
end
function data = readData(filename)
filename='mix1.dat'
fid = fopen(filename,'r');
if fid ==-1
error(['Error opening file ' filename]);
end
data = struct();
while ~feof(fid)
line = fgetl(fid);
if isempty(line)
continue;
end
% Split the line into parameter and value
[parameter, value]= strtok(line,':');
% Remove the colon and whitespace from the value
value = strtrim(strrep(value,':',''));
% Remove comments (lines starting with '#')
value = strtok(value,'#');
% Convert the value to a number if it is not empty
if ~isempty(value)
numValue = str2double(value);
% Check if the conversion was successful
if ~isnan(numValue)
data.(strtrim(parameter))= numValue;
else
warning(['Could not convert value for parameter ' parameter ' to a number.']);
end
else
warning(['Empty value for parameter ' parameter '.']);
end
end
fclose(fid);
end
% Call readData function to get values from the .dat file
data = readData('mix1.dat');
% Extract values of d, h, and n from the structure
d = data.DIAMETER; % Assuming 'DIAMETER' is the parameter for diameter
h = data.DEPTH1; % Assuming 'DEPTH1' is the parameter for flow depth
n = data.MANNING;
s = data.SLOPE; % Assuming 'MANNING' is the parameter for Manning's n
% Call the horseshoeFlow function with the obtained values
[P, A]= horseshoeFlow(d, h, n, s);
% Display the results
fprintf('Wetted Perimeter (P): %.4f
', P);
fprintf('Cross-sectional Area (A): %.4f
', A);
which reads the mix1.dat files and gives me A nd P
How can I modify thse codes so that it can read multiple .dat files with almost the same structure and with the 'input' command I can write which .dat file I want it to take the data from and calculate A and P
for example I have mix1.dat:MANNING: 0.016
DIAMETER: 1.0 #m
SLOPE: 0.5 # per cent (actual slope is 0.005)
DEPTH1: 0.4 #m
DISCHARGE1: 0.6 #m3/sec and mix8.dat:#Petaloid section
DIAMETER: 1.0 #m
MANNING: 0.016
SLOPE: 0.5 # per cent (actual slope is 0.005)
#DEPTH1: 0.4 #m
DISCHARGE1: 0.6 #m3/sec
when I run the programm: enter file prefix:mix8.dat
and it gives me A and P for mix8.dat
thanks!MATLAB CODE ONLY!!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!