Category: Matlab
Category Archives: Matlab
Ask user to adjust color threshold and use those setting for subsequent live image acquisition/analysis
I’m Attempting to write a target tracking script. So far it is working great. I am tracking the centroid of an orange object in live video from my computers Webcam. But I Realized that my color thresholding script (which I made and exported from the color threshold app) doesn’t always work because the lighting in the room that I am capturing images changes. I would like to be able to ask the user every time the script starts running If the threshold parameters look OK, and if not, be able to adjust the parameters, and automatically export and use the new thresholding code. Right now i have to open the thresholding app, change settings, save a new script, and add that new script to my code manually if i want to utilize my new thresholding settings. My question is, once I Prompt the user to Check if a capture image looks OK, how do I Let the user change the thresholding settings and store those settings for all subsequent captured live images?I’m Attempting to write a target tracking script. So far it is working great. I am tracking the centroid of an orange object in live video from my computers Webcam. But I Realized that my color thresholding script (which I made and exported from the color threshold app) doesn’t always work because the lighting in the room that I am capturing images changes. I would like to be able to ask the user every time the script starts running If the threshold parameters look OK, and if not, be able to adjust the parameters, and automatically export and use the new thresholding code. Right now i have to open the thresholding app, change settings, save a new script, and add that new script to my code manually if i want to utilize my new thresholding settings. My question is, once I Prompt the user to Check if a capture image looks OK, how do I Let the user change the thresholding settings and store those settings for all subsequent captured live images? I’m Attempting to write a target tracking script. So far it is working great. I am tracking the centroid of an orange object in live video from my computers Webcam. But I Realized that my color thresholding script (which I made and exported from the color threshold app) doesn’t always work because the lighting in the room that I am capturing images changes. I would like to be able to ask the user every time the script starts running If the threshold parameters look OK, and if not, be able to adjust the parameters, and automatically export and use the new thresholding code. Right now i have to open the thresholding app, change settings, save a new script, and add that new script to my code manually if i want to utilize my new thresholding settings. My question is, once I Prompt the user to Check if a capture image looks OK, how do I Let the user change the thresholding settings and store those settings for all subsequent captured live images? color thresholding, live image acquisition MATLAB Answers — New Questions
Intersections between two discretised functions
Hi Everybody, I have the following problem:
I have two vectors:
v1 = list of values representing a discretised non monotonic function
v2 = list of values representing a constant and horizontal line
v1 has no analytical model. It’s just a vector containing the values a function assumes in a given interval.
To make everything clearer, let’s consider the following case:
v1 = list of 100 values representing a parabola: y= x^2, with x=linspace(-5, +5, 100)
v2 = ones(1,100)
f = @(x) x.^2;
values = linspace(-5, 5, 100);
v1 = f(values);
v2 = ones(1,100);
Clearly, the intersection points are two, for x1=-1 and x2=+1.
Now let’s forget about the fact that we had the analytical expression for v1 and let’s just consider the vector itself.
How can I implement this on Matalb for a generical vector v1 which is not monotonic and is supposed to have two intersection points with v2? I tried doing something with interp1 but couldn’t figure it out.
Thanks a lot to whoever will help me!Hi Everybody, I have the following problem:
I have two vectors:
v1 = list of values representing a discretised non monotonic function
v2 = list of values representing a constant and horizontal line
v1 has no analytical model. It’s just a vector containing the values a function assumes in a given interval.
To make everything clearer, let’s consider the following case:
v1 = list of 100 values representing a parabola: y= x^2, with x=linspace(-5, +5, 100)
v2 = ones(1,100)
f = @(x) x.^2;
values = linspace(-5, 5, 100);
v1 = f(values);
v2 = ones(1,100);
Clearly, the intersection points are two, for x1=-1 and x2=+1.
Now let’s forget about the fact that we had the analytical expression for v1 and let’s just consider the vector itself.
How can I implement this on Matalb for a generical vector v1 which is not monotonic and is supposed to have two intersection points with v2? I tried doing something with interp1 but couldn’t figure it out.
Thanks a lot to whoever will help me! Hi Everybody, I have the following problem:
I have two vectors:
v1 = list of values representing a discretised non monotonic function
v2 = list of values representing a constant and horizontal line
v1 has no analytical model. It’s just a vector containing the values a function assumes in a given interval.
To make everything clearer, let’s consider the following case:
v1 = list of 100 values representing a parabola: y= x^2, with x=linspace(-5, +5, 100)
v2 = ones(1,100)
f = @(x) x.^2;
values = linspace(-5, 5, 100);
v1 = f(values);
v2 = ones(1,100);
Clearly, the intersection points are two, for x1=-1 and x2=+1.
Now let’s forget about the fact that we had the analytical expression for v1 and let’s just consider the vector itself.
How can I implement this on Matalb for a generical vector v1 which is not monotonic and is supposed to have two intersection points with v2? I tried doing something with interp1 but couldn’t figure it out.
Thanks a lot to whoever will help me! interpolation, intersection, function, numerical MATLAB Answers — New Questions
Intersections between two discretised functions
Hi Everybody, I have the following problem:
I have two vectors:
v1 = list of values representing a discretised non monotonic function
v2 = list of values representing a constant and horizontal line
v1 has no analytical model. It’s just a vector containing the values a function assumes in a given interval.
To make everything clearer, let’s consider the following case:
v1 = list of 100 values representing a parabola: y= x^2, with x=linspace(-5, +5, 100)
v2 = ones(1,100)
f = @(x) x.^2;
values = linspace(-5, 5, 100);
v1 = f(values);
v2 = ones(1,100);
Clearly, the intersection points are two, for x1=-1 and x2=+1.
Now let’s forget about the fact that we had the analytical expression for v1 and let’s just consider the vector itself.
How can I implement this on Matalb for a generical vector v1 which is not monotonic and is supposed to have two intersection points with v2? I tried doing something with interp1 but couldn’t figure it out.
Thanks a lot to whoever will help me!Hi Everybody, I have the following problem:
I have two vectors:
v1 = list of values representing a discretised non monotonic function
v2 = list of values representing a constant and horizontal line
v1 has no analytical model. It’s just a vector containing the values a function assumes in a given interval.
To make everything clearer, let’s consider the following case:
v1 = list of 100 values representing a parabola: y= x^2, with x=linspace(-5, +5, 100)
v2 = ones(1,100)
f = @(x) x.^2;
values = linspace(-5, 5, 100);
v1 = f(values);
v2 = ones(1,100);
Clearly, the intersection points are two, for x1=-1 and x2=+1.
Now let’s forget about the fact that we had the analytical expression for v1 and let’s just consider the vector itself.
How can I implement this on Matalb for a generical vector v1 which is not monotonic and is supposed to have two intersection points with v2? I tried doing something with interp1 but couldn’t figure it out.
Thanks a lot to whoever will help me! Hi Everybody, I have the following problem:
I have two vectors:
v1 = list of values representing a discretised non monotonic function
v2 = list of values representing a constant and horizontal line
v1 has no analytical model. It’s just a vector containing the values a function assumes in a given interval.
To make everything clearer, let’s consider the following case:
v1 = list of 100 values representing a parabola: y= x^2, with x=linspace(-5, +5, 100)
v2 = ones(1,100)
f = @(x) x.^2;
values = linspace(-5, 5, 100);
v1 = f(values);
v2 = ones(1,100);
Clearly, the intersection points are two, for x1=-1 and x2=+1.
Now let’s forget about the fact that we had the analytical expression for v1 and let’s just consider the vector itself.
How can I implement this on Matalb for a generical vector v1 which is not monotonic and is supposed to have two intersection points with v2? I tried doing something with interp1 but couldn’t figure it out.
Thanks a lot to whoever will help me! interpolation, intersection, function, numerical MATLAB Answers — New Questions
Intersections between two discretised functions
Hi Everybody, I have the following problem:
I have two vectors:
v1 = list of values representing a discretised non monotonic function
v2 = list of values representing a constant and horizontal line
v1 has no analytical model. It’s just a vector containing the values a function assumes in a given interval.
To make everything clearer, let’s consider the following case:
v1 = list of 100 values representing a parabola: y= x^2, with x=linspace(-5, +5, 100)
v2 = ones(1,100)
f = @(x) x.^2;
values = linspace(-5, 5, 100);
v1 = f(values);
v2 = ones(1,100);
Clearly, the intersection points are two, for x1=-1 and x2=+1.
Now let’s forget about the fact that we had the analytical expression for v1 and let’s just consider the vector itself.
How can I implement this on Matalb for a generical vector v1 which is not monotonic and is supposed to have two intersection points with v2? I tried doing something with interp1 but couldn’t figure it out.
Thanks a lot to whoever will help me!Hi Everybody, I have the following problem:
I have two vectors:
v1 = list of values representing a discretised non monotonic function
v2 = list of values representing a constant and horizontal line
v1 has no analytical model. It’s just a vector containing the values a function assumes in a given interval.
To make everything clearer, let’s consider the following case:
v1 = list of 100 values representing a parabola: y= x^2, with x=linspace(-5, +5, 100)
v2 = ones(1,100)
f = @(x) x.^2;
values = linspace(-5, 5, 100);
v1 = f(values);
v2 = ones(1,100);
Clearly, the intersection points are two, for x1=-1 and x2=+1.
Now let’s forget about the fact that we had the analytical expression for v1 and let’s just consider the vector itself.
How can I implement this on Matalb for a generical vector v1 which is not monotonic and is supposed to have two intersection points with v2? I tried doing something with interp1 but couldn’t figure it out.
Thanks a lot to whoever will help me! Hi Everybody, I have the following problem:
I have two vectors:
v1 = list of values representing a discretised non monotonic function
v2 = list of values representing a constant and horizontal line
v1 has no analytical model. It’s just a vector containing the values a function assumes in a given interval.
To make everything clearer, let’s consider the following case:
v1 = list of 100 values representing a parabola: y= x^2, with x=linspace(-5, +5, 100)
v2 = ones(1,100)
f = @(x) x.^2;
values = linspace(-5, 5, 100);
v1 = f(values);
v2 = ones(1,100);
Clearly, the intersection points are two, for x1=-1 and x2=+1.
Now let’s forget about the fact that we had the analytical expression for v1 and let’s just consider the vector itself.
How can I implement this on Matalb for a generical vector v1 which is not monotonic and is supposed to have two intersection points with v2? I tried doing something with interp1 but couldn’t figure it out.
Thanks a lot to whoever will help me! interpolation, intersection, function, numerical MATLAB Answers — New Questions
Why is MATLAB Runtime so large?
The monolithic MATLAB Runtime installation size seems to get bigger and bigger from release to release. Can I have a smaller sized runtime with just the components required by my application?The monolithic MATLAB Runtime installation size seems to get bigger and bigger from release to release. Can I have a smaller sized runtime with just the components required by my application? The monolithic MATLAB Runtime installation size seems to get bigger and bigger from release to release. Can I have a smaller sized runtime with just the components required by my application? large, mcr, mgl, fat, size, big, huge, behemoth, slow, unacceptable, enormous, gargantuan, goliath, leviathan MATLAB Answers — New Questions
How can I extract the region of interest form multiple images as matrices?
The following is a part of images (video) that taken in an experiment
How can I only take into consideration the marker that keeps moving and discard the rest of the image?
I mean I want only to extract the region of interest (marker) and its features?
I have multiple experiments (around 60000) and I want to do the same for all of them
Note: When you run it as a video you will notice the marker motion, it is in the right down of the image
for i = 1:301
imshow(DATA(:,:,:,i),[]);
pause(0.001)
endThe following is a part of images (video) that taken in an experiment
How can I only take into consideration the marker that keeps moving and discard the rest of the image?
I mean I want only to extract the region of interest (marker) and its features?
I have multiple experiments (around 60000) and I want to do the same for all of them
Note: When you run it as a video you will notice the marker motion, it is in the right down of the image
for i = 1:301
imshow(DATA(:,:,:,i),[]);
pause(0.001)
end The following is a part of images (video) that taken in an experiment
How can I only take into consideration the marker that keeps moving and discard the rest of the image?
I mean I want only to extract the region of interest (marker) and its features?
I have multiple experiments (around 60000) and I want to do the same for all of them
Note: When you run it as a video you will notice the marker motion, it is in the right down of the image
for i = 1:301
imshow(DATA(:,:,:,i),[]);
pause(0.001)
end image, image analysis, image segmentation, image processing, digital image processing MATLAB Answers — New Questions
Incorrect Calculation when using symunit in Symbolic Math Toolbox
clc,clear
syms x a
u = symunit;
a = 1.01325*u.bar
b = -0.049576382*u.bar*u.m^3/u.mol
c = .000003657553277*u.bar*u.m^6/(u.mol)^2
d = -.00000000015676624*u.bar*u.m^9/u.mol^3
eqn = a*x^3+b*x^2+c*x+b == 0
solve(eqn,x,"MaxDegree",3)
vpa(solve(eqn,x,’MaxDegr
Output: 0.016
clc,clear
syms x
u = symunit;
a = 1.01325
b = -0.049576382
c = 3.657553277*10^-6
d = -1.5676624*10^-10
eqn = a*x^3+b*x^2+c*x+b == 0
solve(eqn,x,"MaxDegree",3)
vpa(solve(eqn,x,’MaxDegree’,3),10)
Output: 0.382
a = 1.01325
b = -0.049576382
c = 3.657553277*10^-6
d = -1.5676624*10^-10
x = [a b c d];
vpa(roots(x),10)
Output: 0.048
u = symunit;
a = 1.01325*u.bar
b = -0.049576382*u.bar*u.m^3/u.mol
c = .000003657553277*u.bar*u.m^6/(u.mol)^2
d = -.00000000015676624*u.bar*u.m^9/u.mol^3
x = [a b c d];
vpa(roots(x),10)
Output: root(z^3 – ((sym("223272175501577375")*z^2)/sym("4563272322433155072"))*(symunit("m")^3/symunit("mol")) + ((sym("539759593894213625")*z)/sym("149529307461489625399296"))*(symunit("m")^6/symunit("mol")^2) – sym("252691673573204875/1633258782299364015028043776")*(symunit("m")^9/symunit("mol")^3), z, 1)
Wolfram Alpha Answer: 0.048clc,clear
syms x a
u = symunit;
a = 1.01325*u.bar
b = -0.049576382*u.bar*u.m^3/u.mol
c = .000003657553277*u.bar*u.m^6/(u.mol)^2
d = -.00000000015676624*u.bar*u.m^9/u.mol^3
eqn = a*x^3+b*x^2+c*x+b == 0
solve(eqn,x,"MaxDegree",3)
vpa(solve(eqn,x,’MaxDegr
Output: 0.016
clc,clear
syms x
u = symunit;
a = 1.01325
b = -0.049576382
c = 3.657553277*10^-6
d = -1.5676624*10^-10
eqn = a*x^3+b*x^2+c*x+b == 0
solve(eqn,x,"MaxDegree",3)
vpa(solve(eqn,x,’MaxDegree’,3),10)
Output: 0.382
a = 1.01325
b = -0.049576382
c = 3.657553277*10^-6
d = -1.5676624*10^-10
x = [a b c d];
vpa(roots(x),10)
Output: 0.048
u = symunit;
a = 1.01325*u.bar
b = -0.049576382*u.bar*u.m^3/u.mol
c = .000003657553277*u.bar*u.m^6/(u.mol)^2
d = -.00000000015676624*u.bar*u.m^9/u.mol^3
x = [a b c d];
vpa(roots(x),10)
Output: root(z^3 – ((sym("223272175501577375")*z^2)/sym("4563272322433155072"))*(symunit("m")^3/symunit("mol")) + ((sym("539759593894213625")*z)/sym("149529307461489625399296"))*(symunit("m")^6/symunit("mol")^2) – sym("252691673573204875/1633258782299364015028043776")*(symunit("m")^9/symunit("mol")^3), z, 1)
Wolfram Alpha Answer: 0.048 clc,clear
syms x a
u = symunit;
a = 1.01325*u.bar
b = -0.049576382*u.bar*u.m^3/u.mol
c = .000003657553277*u.bar*u.m^6/(u.mol)^2
d = -.00000000015676624*u.bar*u.m^9/u.mol^3
eqn = a*x^3+b*x^2+c*x+b == 0
solve(eqn,x,"MaxDegree",3)
vpa(solve(eqn,x,’MaxDegr
Output: 0.016
clc,clear
syms x
u = symunit;
a = 1.01325
b = -0.049576382
c = 3.657553277*10^-6
d = -1.5676624*10^-10
eqn = a*x^3+b*x^2+c*x+b == 0
solve(eqn,x,"MaxDegree",3)
vpa(solve(eqn,x,’MaxDegree’,3),10)
Output: 0.382
a = 1.01325
b = -0.049576382
c = 3.657553277*10^-6
d = -1.5676624*10^-10
x = [a b c d];
vpa(roots(x),10)
Output: 0.048
u = symunit;
a = 1.01325*u.bar
b = -0.049576382*u.bar*u.m^3/u.mol
c = .000003657553277*u.bar*u.m^6/(u.mol)^2
d = -.00000000015676624*u.bar*u.m^9/u.mol^3
x = [a b c d];
vpa(roots(x),10)
Output: root(z^3 – ((sym("223272175501577375")*z^2)/sym("4563272322433155072"))*(symunit("m")^3/symunit("mol")) + ((sym("539759593894213625")*z)/sym("149529307461489625399296"))*(symunit("m")^6/symunit("mol")^2) – sym("252691673573204875/1633258782299364015028043776")*(symunit("m")^9/symunit("mol")^3), z, 1)
Wolfram Alpha Answer: 0.048 symunit, symbolic, cubic equation, units MATLAB Answers — New Questions
How to plot contour of an xy plot with a constant value?
I have several data sets (I have selected two lines in this case. I want to plot them as contour lines with different z values. For example, I want to plot the xy-data with z values as 40 and 30. As the lines extend beyond my area of interest, the current are of interest is 0 to 120 in x and 0 to 220 in y axis. I tried using meshgrid which I have used for plotting contour plots but I cannot convert these known values of lines into contours.Your help will be much appreciated.
xy = readtable(‘xy_data.xlsx’);
x1 = xy.x40;
y1 = xy.y40;
x2 = xy.x30;
y2 = xy.y30;
figure
rectangle(‘Position’,[0 0 120 220]) % the contour line can be only drawn in this box
axis equal
hold on
plot(x1,y1)
plot(x2,y2)I have several data sets (I have selected two lines in this case. I want to plot them as contour lines with different z values. For example, I want to plot the xy-data with z values as 40 and 30. As the lines extend beyond my area of interest, the current are of interest is 0 to 120 in x and 0 to 220 in y axis. I tried using meshgrid which I have used for plotting contour plots but I cannot convert these known values of lines into contours.Your help will be much appreciated.
xy = readtable(‘xy_data.xlsx’);
x1 = xy.x40;
y1 = xy.y40;
x2 = xy.x30;
y2 = xy.y30;
figure
rectangle(‘Position’,[0 0 120 220]) % the contour line can be only drawn in this box
axis equal
hold on
plot(x1,y1)
plot(x2,y2) I have several data sets (I have selected two lines in this case. I want to plot them as contour lines with different z values. For example, I want to plot the xy-data with z values as 40 and 30. As the lines extend beyond my area of interest, the current are of interest is 0 to 120 in x and 0 to 220 in y axis. I tried using meshgrid which I have used for plotting contour plots but I cannot convert these known values of lines into contours.Your help will be much appreciated.
xy = readtable(‘xy_data.xlsx’);
x1 = xy.x40;
y1 = xy.y40;
x2 = xy.x30;
y2 = xy.y30;
figure
rectangle(‘Position’,[0 0 120 220]) % the contour line can be only drawn in this box
axis equal
hold on
plot(x1,y1)
plot(x2,y2) contourf, contour xy, meshgrid, xy data, contour MATLAB Answers — New Questions
VideoWriter generating unexpected results
I believe the VideoWriter in R2023b has a bug. It can be reproduced with this piece of code:
v = VideoWriter(‘test.mp4’, ‘MPEG-4’); % use h264 encoding
v.FrameRate = 30;
v.Quality = 100;
open(v)
vidFrame = ones([1920, 1080, 3]);
writeVideo(v, vidFrame);
close(v)
There are black lines appearing in the output video:I believe the VideoWriter in R2023b has a bug. It can be reproduced with this piece of code:
v = VideoWriter(‘test.mp4’, ‘MPEG-4’); % use h264 encoding
v.FrameRate = 30;
v.Quality = 100;
open(v)
vidFrame = ones([1920, 1080, 3]);
writeVideo(v, vidFrame);
close(v)
There are black lines appearing in the output video: I believe the VideoWriter in R2023b has a bug. It can be reproduced with this piece of code:
v = VideoWriter(‘test.mp4’, ‘MPEG-4’); % use h264 encoding
v.FrameRate = 30;
v.Quality = 100;
open(v)
vidFrame = ones([1920, 1080, 3]);
writeVideo(v, vidFrame);
close(v)
There are black lines appearing in the output video: videowriter, mp4, writevideo MATLAB Answers — New Questions
Are the variables(fminimax) independent of each other?
in this code i have 68 variable half of them between [0,1] and next half [-90,90]
the first half change reasonable but others dont change (almost 0.3) and dont go to minus area
are they related to each other?
% close all
clear all
elementNumb=34;
%%
%%with W
% random_numbersw = rand(1,elementNumb); % Random number from 0 to 1
random_numbersw = ones(1,elementNumb);
ubb=90;
lbb=-90;
% random_numberst = lbb + (ubb + ubb) * rand(1, elementNumb); % Random number from -90 to 90
random_numberst =12* (ones(1 , elementNumb));
x0 = [ random_numbersw,random_numberst ] ;
ub = zeros(1, elementNumb*2); % Initialize a matrix of zeros with 40 elements
ub(1:elementNumb) = 1; % Set values from 1 to 21 to 0
ub(elementNumb+1:elementNumb*2) = ubb; % Set values from 22 to 40 to 90
lb = zeros(1, elementNumb*2); % Initialize a matrix of zeros with 40 elements
lb(1:elementNumb) = 0.000001; % Set values from 1 to 21 to 0
lb(elementNumb+1:elementNumb*2) = lbb; % Set values from 22 to 40 to 90
%%
%%Fminimax
% options = optimoptions(@fminimax,’disp’,’iter’,’Diagnostics’,…
% ‘on’, ‘FiniteDifferenceStepSize’,1e-3,…
% ‘FunctionTolerance’,1e-9,’MaxFunctionEvaluations’,20000,’FunValCheck’,’on’,’ConstraintTolerance’,0);
% options = optimoptions(@fminimax,’disp’,’iter’,’Diagnostics’,…
% ‘on’,…
% ‘FunctionTolerance’,1e-9,’MaxFunctionEvaluations’,2000,’FunValCheck’,’on’);
% % options=optimset(‘disp’,’iter’,’LargeScale’,’off’,’Diagnostics’,…
% ‘on’,’TolFun’,.00000000000001,’MaxIter’,10000,’MaxFunEvals’,1000000);
[x,fval,maxfval,exitflag,output,lambda] = fminimax(@costy,x0,[],[],[],[],lb,ub,[]);
%%function
function y = costy(x)
freq = 9*10^9; %Freq
j = sqrt(-1); %Define Imaginary
l =(3*10^8)/freq; %Lambda
k = (2*pi)/l; %Constant
d = 0.5*l; %Distant of each element
elementNumb = 34;
step = 0.1;
teta1 = ((-80) :step: (-50));
tetat = teta1;
g = zeros(elementNumb,length(tetat));
for h = 1:elementNumb
for aa = 1:length(teta1)
g(h,aa) = g(h,aa)+(x(h) * ( exp(-j*(h-1) * (k*d*sind(teta1(aa)+x(h+elementNumb)))))); %w W
end
end
y = abs(sum(g,1));
end
and can they change with certain step for example first half 0.01 and second 5in this code i have 68 variable half of them between [0,1] and next half [-90,90]
the first half change reasonable but others dont change (almost 0.3) and dont go to minus area
are they related to each other?
% close all
clear all
elementNumb=34;
%%
%%with W
% random_numbersw = rand(1,elementNumb); % Random number from 0 to 1
random_numbersw = ones(1,elementNumb);
ubb=90;
lbb=-90;
% random_numberst = lbb + (ubb + ubb) * rand(1, elementNumb); % Random number from -90 to 90
random_numberst =12* (ones(1 , elementNumb));
x0 = [ random_numbersw,random_numberst ] ;
ub = zeros(1, elementNumb*2); % Initialize a matrix of zeros with 40 elements
ub(1:elementNumb) = 1; % Set values from 1 to 21 to 0
ub(elementNumb+1:elementNumb*2) = ubb; % Set values from 22 to 40 to 90
lb = zeros(1, elementNumb*2); % Initialize a matrix of zeros with 40 elements
lb(1:elementNumb) = 0.000001; % Set values from 1 to 21 to 0
lb(elementNumb+1:elementNumb*2) = lbb; % Set values from 22 to 40 to 90
%%
%%Fminimax
% options = optimoptions(@fminimax,’disp’,’iter’,’Diagnostics’,…
% ‘on’, ‘FiniteDifferenceStepSize’,1e-3,…
% ‘FunctionTolerance’,1e-9,’MaxFunctionEvaluations’,20000,’FunValCheck’,’on’,’ConstraintTolerance’,0);
% options = optimoptions(@fminimax,’disp’,’iter’,’Diagnostics’,…
% ‘on’,…
% ‘FunctionTolerance’,1e-9,’MaxFunctionEvaluations’,2000,’FunValCheck’,’on’);
% % options=optimset(‘disp’,’iter’,’LargeScale’,’off’,’Diagnostics’,…
% ‘on’,’TolFun’,.00000000000001,’MaxIter’,10000,’MaxFunEvals’,1000000);
[x,fval,maxfval,exitflag,output,lambda] = fminimax(@costy,x0,[],[],[],[],lb,ub,[]);
%%function
function y = costy(x)
freq = 9*10^9; %Freq
j = sqrt(-1); %Define Imaginary
l =(3*10^8)/freq; %Lambda
k = (2*pi)/l; %Constant
d = 0.5*l; %Distant of each element
elementNumb = 34;
step = 0.1;
teta1 = ((-80) :step: (-50));
tetat = teta1;
g = zeros(elementNumb,length(tetat));
for h = 1:elementNumb
for aa = 1:length(teta1)
g(h,aa) = g(h,aa)+(x(h) * ( exp(-j*(h-1) * (k*d*sind(teta1(aa)+x(h+elementNumb)))))); %w W
end
end
y = abs(sum(g,1));
end
and can they change with certain step for example first half 0.01 and second 5 in this code i have 68 variable half of them between [0,1] and next half [-90,90]
the first half change reasonable but others dont change (almost 0.3) and dont go to minus area
are they related to each other?
% close all
clear all
elementNumb=34;
%%
%%with W
% random_numbersw = rand(1,elementNumb); % Random number from 0 to 1
random_numbersw = ones(1,elementNumb);
ubb=90;
lbb=-90;
% random_numberst = lbb + (ubb + ubb) * rand(1, elementNumb); % Random number from -90 to 90
random_numberst =12* (ones(1 , elementNumb));
x0 = [ random_numbersw,random_numberst ] ;
ub = zeros(1, elementNumb*2); % Initialize a matrix of zeros with 40 elements
ub(1:elementNumb) = 1; % Set values from 1 to 21 to 0
ub(elementNumb+1:elementNumb*2) = ubb; % Set values from 22 to 40 to 90
lb = zeros(1, elementNumb*2); % Initialize a matrix of zeros with 40 elements
lb(1:elementNumb) = 0.000001; % Set values from 1 to 21 to 0
lb(elementNumb+1:elementNumb*2) = lbb; % Set values from 22 to 40 to 90
%%
%%Fminimax
% options = optimoptions(@fminimax,’disp’,’iter’,’Diagnostics’,…
% ‘on’, ‘FiniteDifferenceStepSize’,1e-3,…
% ‘FunctionTolerance’,1e-9,’MaxFunctionEvaluations’,20000,’FunValCheck’,’on’,’ConstraintTolerance’,0);
% options = optimoptions(@fminimax,’disp’,’iter’,’Diagnostics’,…
% ‘on’,…
% ‘FunctionTolerance’,1e-9,’MaxFunctionEvaluations’,2000,’FunValCheck’,’on’);
% % options=optimset(‘disp’,’iter’,’LargeScale’,’off’,’Diagnostics’,…
% ‘on’,’TolFun’,.00000000000001,’MaxIter’,10000,’MaxFunEvals’,1000000);
[x,fval,maxfval,exitflag,output,lambda] = fminimax(@costy,x0,[],[],[],[],lb,ub,[]);
%%function
function y = costy(x)
freq = 9*10^9; %Freq
j = sqrt(-1); %Define Imaginary
l =(3*10^8)/freq; %Lambda
k = (2*pi)/l; %Constant
d = 0.5*l; %Distant of each element
elementNumb = 34;
step = 0.1;
teta1 = ((-80) :step: (-50));
tetat = teta1;
g = zeros(elementNumb,length(tetat));
for h = 1:elementNumb
for aa = 1:length(teta1)
g(h,aa) = g(h,aa)+(x(h) * ( exp(-j*(h-1) * (k*d*sind(teta1(aa)+x(h+elementNumb)))))); %w W
end
end
y = abs(sum(g,1));
end
and can they change with certain step for example first half 0.01 and second 5 optimization, variables MATLAB Answers — New Questions
How to get system date and time into simulink in EXTERNAL MODE?
Is there a way to get the system date and time into simulink in *EXTERNAL MODE* ?
below code in embedded matlab function block doesn’t work in EXTERNAL MODE.
function [Y, M, D, H, MN, S] = sysClock()
coder.extrinsic(‘clock’);
Y = 0;
M = 0;
D = 0;
H = 0;
MN = 0;
S = 0;
[Y, M, D, H, MN, S] = clock;
endIs there a way to get the system date and time into simulink in *EXTERNAL MODE* ?
below code in embedded matlab function block doesn’t work in EXTERNAL MODE.
function [Y, M, D, H, MN, S] = sysClock()
coder.extrinsic(‘clock’);
Y = 0;
M = 0;
D = 0;
H = 0;
MN = 0;
S = 0;
[Y, M, D, H, MN, S] = clock;
end Is there a way to get the system date and time into simulink in *EXTERNAL MODE* ?
below code in embedded matlab function block doesn’t work in EXTERNAL MODE.
function [Y, M, D, H, MN, S] = sysClock()
coder.extrinsic(‘clock’);
Y = 0;
M = 0;
D = 0;
H = 0;
MN = 0;
S = 0;
[Y, M, D, H, MN, S] = clock;
end simulink, external mode MATLAB Answers — New Questions
How do I express my numerator in the transfer function block (SIMULINK) if the s variable is in the exponent?
The numerator of my transfer function is 104e^-2.6s and I don’t know how to express this in the transfer function block in Simulink. Need help for a school project. Thanks.The numerator of my transfer function is 104e^-2.6s and I don’t know how to express this in the transfer function block in Simulink. Need help for a school project. Thanks. The numerator of my transfer function is 104e^-2.6s and I don’t know how to express this in the transfer function block in Simulink. Need help for a school project. Thanks. simulink, transfer function MATLAB Answers — New Questions
Solving the system of ODEs and algebraic Equation
I am trying to solve the coupled ODE with dependent algebraic equation using RK4 method. I tried with my code below. I want to plot v,w,p,R,u_s and h_r v/s t. But I am getting error "Too many input arguments". I tried to figure out the problem for daya but could not able to do. Can anyone point my mistake ?? ny help will be much aappreciated.
clear
close all
a = deg2rad(45); m = 0.683; We = 277; Re = 75;
b = (-0.13*m.^3+0.263*m.^2 +0.039*m +0.330)/tan(a);
h = 0.001;
t = 0:h:pi ;
y = zeros(4,length(t));
qs = [];
y(:,1) = [0.0424; 0.0042; pi/2; 1.7440];
for i = 1:1
t = t(i,1);
syms q
x = b*cos(t)*sin(a).^2; x01 = 4*((1-cos(t).^2*cos(a)^2)); x02 = (b*sin(t)*sin(a)).^2;
q_j = -((x -(x01-x02).^0.5)./(x01/4));
d = (b.^2*sin(a).^2 + q.^2*(1-cos(t).^2*cos(a).^2) + 2*b*q*cos(t)*sin(a)^2).^0.5;
u_j = 2*(m-1)*(4*d.^2-1) + m;
F = int(q*u_j, q, 0, q_j);
G = int(q*u_j^3,q,0,q_j);
% F = matlabFunction(F1);
% G = matlabFunction(G);
syms u_s
eqs = (y(4,i)-q_j).*F.*sin(a).*u_s.^3 +…
(y(4,i).^2+2*y(4,i)*((pi*(y(1,i).^2./y(2,i))).^0.5)+(16/(sin(a)*3*q_j.^3*y(4,i)*Re))*((y(4,i)-q_j)*F*sin(a)).^2)*u_s.^2 -…
((y(4,i)-q_j)*F*sin(a)-((F.^3*sin(a).^3)/(9))*((1/q_j.^3)-(1/y(4,i).^3))+((y(4,i)-q_j)*F*sin(a))*(4/(q_j*sin(a))))*u_s-…
((y(4,i)-q_j)*F*sin(a))*((4*sin(a).^2*F.^3*(y(4,i).^5-q_j.^5))/(15*q_j.^7*y(4,i).^5*Re));
eqs = matlabFunction(eqs, ‘Vars’, u_s);
u_s = fsolve(eqs,0.50);
k1 = odesym(t(i,1),y(:,i),a,We,Re,F,u_s);
k2 = odesym(t(i,1)+h/2,y(:,i)+h*k1/2,a,We);
k3 = odesym(t(i,1)+h/2,y(:,i)+h*k2/2,a,We);
k4 = odesym(t(i,1)+h,y(:,i)+h*k3,a,We);
y(:,i+1) = y(:,i) + (1/6)*(k1+2*k2+2*k3+k4)*h;
end
function dydt = odesym(t,y,We,Re,F,u_s)
v = y(1); w = y(2); p = y(3); R = y(4);
% u_b = w./v; s_b = (v.^2)./w;
h_r = ((F(t).^(3./2))./(G(t).^0.5))./R;
F_v = ((((w./v)-u_s.*cos(p))./(((v.^2./w)./pi).^0.5))*((h_r.*R)./(sin(p).*Re)));
dvdt = u_s.*h_r.*R;
dwdt = u_s.^2.*h_r.*R.*cos(p) + F_v;
dpdt = (((2.*R./We)-(u_s.^2.*h_r.*R.*sin(p).^2))/(w.*sin(p)))-1;
drdt = R./tan(p);
dydt = [dvdt; dwdt; dpdt; drdt];
endI am trying to solve the coupled ODE with dependent algebraic equation using RK4 method. I tried with my code below. I want to plot v,w,p,R,u_s and h_r v/s t. But I am getting error "Too many input arguments". I tried to figure out the problem for daya but could not able to do. Can anyone point my mistake ?? ny help will be much aappreciated.
clear
close all
a = deg2rad(45); m = 0.683; We = 277; Re = 75;
b = (-0.13*m.^3+0.263*m.^2 +0.039*m +0.330)/tan(a);
h = 0.001;
t = 0:h:pi ;
y = zeros(4,length(t));
qs = [];
y(:,1) = [0.0424; 0.0042; pi/2; 1.7440];
for i = 1:1
t = t(i,1);
syms q
x = b*cos(t)*sin(a).^2; x01 = 4*((1-cos(t).^2*cos(a)^2)); x02 = (b*sin(t)*sin(a)).^2;
q_j = -((x -(x01-x02).^0.5)./(x01/4));
d = (b.^2*sin(a).^2 + q.^2*(1-cos(t).^2*cos(a).^2) + 2*b*q*cos(t)*sin(a)^2).^0.5;
u_j = 2*(m-1)*(4*d.^2-1) + m;
F = int(q*u_j, q, 0, q_j);
G = int(q*u_j^3,q,0,q_j);
% F = matlabFunction(F1);
% G = matlabFunction(G);
syms u_s
eqs = (y(4,i)-q_j).*F.*sin(a).*u_s.^3 +…
(y(4,i).^2+2*y(4,i)*((pi*(y(1,i).^2./y(2,i))).^0.5)+(16/(sin(a)*3*q_j.^3*y(4,i)*Re))*((y(4,i)-q_j)*F*sin(a)).^2)*u_s.^2 -…
((y(4,i)-q_j)*F*sin(a)-((F.^3*sin(a).^3)/(9))*((1/q_j.^3)-(1/y(4,i).^3))+((y(4,i)-q_j)*F*sin(a))*(4/(q_j*sin(a))))*u_s-…
((y(4,i)-q_j)*F*sin(a))*((4*sin(a).^2*F.^3*(y(4,i).^5-q_j.^5))/(15*q_j.^7*y(4,i).^5*Re));
eqs = matlabFunction(eqs, ‘Vars’, u_s);
u_s = fsolve(eqs,0.50);
k1 = odesym(t(i,1),y(:,i),a,We,Re,F,u_s);
k2 = odesym(t(i,1)+h/2,y(:,i)+h*k1/2,a,We);
k3 = odesym(t(i,1)+h/2,y(:,i)+h*k2/2,a,We);
k4 = odesym(t(i,1)+h,y(:,i)+h*k3,a,We);
y(:,i+1) = y(:,i) + (1/6)*(k1+2*k2+2*k3+k4)*h;
end
function dydt = odesym(t,y,We,Re,F,u_s)
v = y(1); w = y(2); p = y(3); R = y(4);
% u_b = w./v; s_b = (v.^2)./w;
h_r = ((F(t).^(3./2))./(G(t).^0.5))./R;
F_v = ((((w./v)-u_s.*cos(p))./(((v.^2./w)./pi).^0.5))*((h_r.*R)./(sin(p).*Re)));
dvdt = u_s.*h_r.*R;
dwdt = u_s.^2.*h_r.*R.*cos(p) + F_v;
dpdt = (((2.*R./We)-(u_s.^2.*h_r.*R.*sin(p).^2))/(w.*sin(p)))-1;
drdt = R./tan(p);
dydt = [dvdt; dwdt; dpdt; drdt];
end I am trying to solve the coupled ODE with dependent algebraic equation using RK4 method. I tried with my code below. I want to plot v,w,p,R,u_s and h_r v/s t. But I am getting error "Too many input arguments". I tried to figure out the problem for daya but could not able to do. Can anyone point my mistake ?? ny help will be much aappreciated.
clear
close all
a = deg2rad(45); m = 0.683; We = 277; Re = 75;
b = (-0.13*m.^3+0.263*m.^2 +0.039*m +0.330)/tan(a);
h = 0.001;
t = 0:h:pi ;
y = zeros(4,length(t));
qs = [];
y(:,1) = [0.0424; 0.0042; pi/2; 1.7440];
for i = 1:1
t = t(i,1);
syms q
x = b*cos(t)*sin(a).^2; x01 = 4*((1-cos(t).^2*cos(a)^2)); x02 = (b*sin(t)*sin(a)).^2;
q_j = -((x -(x01-x02).^0.5)./(x01/4));
d = (b.^2*sin(a).^2 + q.^2*(1-cos(t).^2*cos(a).^2) + 2*b*q*cos(t)*sin(a)^2).^0.5;
u_j = 2*(m-1)*(4*d.^2-1) + m;
F = int(q*u_j, q, 0, q_j);
G = int(q*u_j^3,q,0,q_j);
% F = matlabFunction(F1);
% G = matlabFunction(G);
syms u_s
eqs = (y(4,i)-q_j).*F.*sin(a).*u_s.^3 +…
(y(4,i).^2+2*y(4,i)*((pi*(y(1,i).^2./y(2,i))).^0.5)+(16/(sin(a)*3*q_j.^3*y(4,i)*Re))*((y(4,i)-q_j)*F*sin(a)).^2)*u_s.^2 -…
((y(4,i)-q_j)*F*sin(a)-((F.^3*sin(a).^3)/(9))*((1/q_j.^3)-(1/y(4,i).^3))+((y(4,i)-q_j)*F*sin(a))*(4/(q_j*sin(a))))*u_s-…
((y(4,i)-q_j)*F*sin(a))*((4*sin(a).^2*F.^3*(y(4,i).^5-q_j.^5))/(15*q_j.^7*y(4,i).^5*Re));
eqs = matlabFunction(eqs, ‘Vars’, u_s);
u_s = fsolve(eqs,0.50);
k1 = odesym(t(i,1),y(:,i),a,We,Re,F,u_s);
k2 = odesym(t(i,1)+h/2,y(:,i)+h*k1/2,a,We);
k3 = odesym(t(i,1)+h/2,y(:,i)+h*k2/2,a,We);
k4 = odesym(t(i,1)+h,y(:,i)+h*k3,a,We);
y(:,i+1) = y(:,i) + (1/6)*(k1+2*k2+2*k3+k4)*h;
end
function dydt = odesym(t,y,We,Re,F,u_s)
v = y(1); w = y(2); p = y(3); R = y(4);
% u_b = w./v; s_b = (v.^2)./w;
h_r = ((F(t).^(3./2))./(G(t).^0.5))./R;
F_v = ((((w./v)-u_s.*cos(p))./(((v.^2./w)./pi).^0.5))*((h_r.*R)./(sin(p).*Re)));
dvdt = u_s.*h_r.*R;
dwdt = u_s.^2.*h_r.*R.*cos(p) + F_v;
dpdt = (((2.*R./We)-(u_s.^2.*h_r.*R.*sin(p).^2))/(w.*sin(p)))-1;
drdt = R./tan(p);
dydt = [dvdt; dwdt; dpdt; drdt];
end ode MATLAB Answers — New Questions
How to just plot the data inside the minimum and maximum values in a figure?
Hello,
I have 2 figures, From the frst figure I calculated the minimum and maximum y values from each x bin and then plotted these minimum and maximum values as dashed black line over second figure. My code:
y1 = y;
y1(isnan(z))= nan;
x1 = x(1,:);
minvalues = zeros(1,16);
maxvalues = zeros(1,16);
for k = 1:16
x2 = x1(:,k);
minvalues(k) = min(y1(:,k));
maxvalues(k) = max(y1(:,k));
end
plot(ax,x1,minvalues, ‘–k’, ‘Linewidth’, 2)
plot(ax,x1,maxvalues, ‘–k’, ‘Linewidth’, 2)
My second figure looks like this, I just want to plot data which is inside this boundary (minimum and maximum values). Outside area should be white. How can I do this? Thank youHello,
I have 2 figures, From the frst figure I calculated the minimum and maximum y values from each x bin and then plotted these minimum and maximum values as dashed black line over second figure. My code:
y1 = y;
y1(isnan(z))= nan;
x1 = x(1,:);
minvalues = zeros(1,16);
maxvalues = zeros(1,16);
for k = 1:16
x2 = x1(:,k);
minvalues(k) = min(y1(:,k));
maxvalues(k) = max(y1(:,k));
end
plot(ax,x1,minvalues, ‘–k’, ‘Linewidth’, 2)
plot(ax,x1,maxvalues, ‘–k’, ‘Linewidth’, 2)
My second figure looks like this, I just want to plot data which is inside this boundary (minimum and maximum values). Outside area should be white. How can I do this? Thank you Hello,
I have 2 figures, From the frst figure I calculated the minimum and maximum y values from each x bin and then plotted these minimum and maximum values as dashed black line over second figure. My code:
y1 = y;
y1(isnan(z))= nan;
x1 = x(1,:);
minvalues = zeros(1,16);
maxvalues = zeros(1,16);
for k = 1:16
x2 = x1(:,k);
minvalues(k) = min(y1(:,k));
maxvalues(k) = max(y1(:,k));
end
plot(ax,x1,minvalues, ‘–k’, ‘Linewidth’, 2)
plot(ax,x1,maxvalues, ‘–k’, ‘Linewidth’, 2)
My second figure looks like this, I just want to plot data which is inside this boundary (minimum and maximum values). Outside area should be white. How can I do this? Thank you plot, figure MATLAB Answers — New Questions
what does A/b mean when solving matrix Ax=b
A/bA/b A/b matrix, matlab MATLAB Answers — New Questions
Matlab Solve and simplify functions not working
I’m using the ‘solve’ function to solve a system of equations. It is solving them but not simplifying. That’s when I have tried to use the ‘simplify’ function. Simplifying is not working. It stays the same as before instead of being solved. If I copy/paste the expression, it solves it though. Any suggestions for getting it to simplify to one number without having to manually copy/paste for each one? For example, f21_sol(1) = -(760*(348475767795489804396578940311480912*3^(1/2) + 168032379110159424438402050627659277))/(3*(3858879662023561551604431872957445612*3^(1/2) – 3892977404894321714789280647602584263)), which is absolutely solvable for a single number.I’m using the ‘solve’ function to solve a system of equations. It is solving them but not simplifying. That’s when I have tried to use the ‘simplify’ function. Simplifying is not working. It stays the same as before instead of being solved. If I copy/paste the expression, it solves it though. Any suggestions for getting it to simplify to one number without having to manually copy/paste for each one? For example, f21_sol(1) = -(760*(348475767795489804396578940311480912*3^(1/2) + 168032379110159424438402050627659277))/(3*(3858879662023561551604431872957445612*3^(1/2) – 3892977404894321714789280647602584263)), which is absolutely solvable for a single number. I’m using the ‘solve’ function to solve a system of equations. It is solving them but not simplifying. That’s when I have tried to use the ‘simplify’ function. Simplifying is not working. It stays the same as before instead of being solved. If I copy/paste the expression, it solves it though. Any suggestions for getting it to simplify to one number without having to manually copy/paste for each one? For example, f21_sol(1) = -(760*(348475767795489804396578940311480912*3^(1/2) + 168032379110159424438402050627659277))/(3*(3858879662023561551604431872957445612*3^(1/2) – 3892977404894321714789280647602584263)), which is absolutely solvable for a single number. solve, simplify, system of equations MATLAB Answers — New Questions
Index exceeds the number of array elements, must not exceed 1, in a nested for loop
In my code, I am analyzing a heat concentric heat exchanger and am solving for h, the local convection coefficients at points along the length of the of tube. In my nested for loop, indexed by j, I am attempting to update the length 0, dDh, for each iteration of the Nsselt number Nud. However, I keep getting the error that the index exceeds the number of array elements. there are 10 distance steps and 10 mass flow rates to analyze, so I am confused on why the sizes are a probelm. Any help is appreciated.
“`clear
clc
% givens for the air
ma = 15; %kg/s
Tai = 1200; % deg-C
cpa = 1.207; % kJ/kg-K @ 1200C
ODa = 0.05; % m
IDa = 0.04; % m
% givens for the feedwater
mw = 10; %kg/s
Twi = 250; % deg-C
Two = 500; % deg-C
cpw = 4.860; % kJ/kg-K @ 250C
ODw = 0.01; %m
IDw = 0.008; %m
L = 5; %m, length of HEX bundle
Cc = mw*cpw;
Ch = ma*cpa;
A = [Cc, Ch];
Cmin = min(A);
Cmax = max(A);
Cr = Cmin/Cmax;
Tao = Tai – ((Cmax/Cmin)*(Two-Twi)); %air outlet temperature in deg-C
dT1 = Tai-Two;
dT2 = Tao – Twi;
LMTD = (dT2 – dT1)/log(dT2/dT1); %log mean temperature difference for the HEX
e = (Cc*(Two-Twi))/(Cmin*(Tai-Twi)); %HEX effectiveness
NTU = (1/(Cr-1))*log((e-1)/((e*Cr)-1)); % Net Transfer Units
Aht = pi*ODw*L; % heat transfer area (ft^2), or the outside surface area of the feedwater tube
U = (NTU*Cmin)/Aht; % overall heat transfer coefficient
%%% PART B
mw2 = [0.01:0.01:0.1]; %water mass flow rate in kg/s
rho_w = 798.6; %lg/m^3
Ac = 0.25*pi*IDw^2; %cross-sectional area of the inside of the feedwater tube
Dh = (4*Ac)/(4*pi*IDw); % hydraulic diameter for the feedwater tube
muw = 0.0001063; % dynamic visocsity of the water @ 250C (N*s/m^2)
Uw = zeros(1,length(mw2));
Red = zeros(1,length(mw2));
Nud = zeros(1,length(mw2));
k = 8; %W/m-K. thermal conductivity of water @250-C, guess beased on lower figures
step = Dh/9;
dDh = [0:step:Dh]; %size of the hydraulic diameter
h = zeros(length(mw2),length(dDh));
for i = 1:10
Uw(i) = mw2(i)/(rho_w*Ac); %velocity of the feedwater for the varying flow rates
Red(i) = (rho_w*Uw(i)*Dh)/muw;
Pr = (muw*cpw)/k;
if Red <= 5000
Nud = 4.36;
elseif Red > 5000
Nud(i) = 0.023*(Red(i)^0.8)*(Pr^0.4);
end
for j = 1:10
h(i,j) = (Nud(i)*k)/dDh(j);
end
end“`In my code, I am analyzing a heat concentric heat exchanger and am solving for h, the local convection coefficients at points along the length of the of tube. In my nested for loop, indexed by j, I am attempting to update the length 0, dDh, for each iteration of the Nsselt number Nud. However, I keep getting the error that the index exceeds the number of array elements. there are 10 distance steps and 10 mass flow rates to analyze, so I am confused on why the sizes are a probelm. Any help is appreciated.
“`clear
clc
% givens for the air
ma = 15; %kg/s
Tai = 1200; % deg-C
cpa = 1.207; % kJ/kg-K @ 1200C
ODa = 0.05; % m
IDa = 0.04; % m
% givens for the feedwater
mw = 10; %kg/s
Twi = 250; % deg-C
Two = 500; % deg-C
cpw = 4.860; % kJ/kg-K @ 250C
ODw = 0.01; %m
IDw = 0.008; %m
L = 5; %m, length of HEX bundle
Cc = mw*cpw;
Ch = ma*cpa;
A = [Cc, Ch];
Cmin = min(A);
Cmax = max(A);
Cr = Cmin/Cmax;
Tao = Tai – ((Cmax/Cmin)*(Two-Twi)); %air outlet temperature in deg-C
dT1 = Tai-Two;
dT2 = Tao – Twi;
LMTD = (dT2 – dT1)/log(dT2/dT1); %log mean temperature difference for the HEX
e = (Cc*(Two-Twi))/(Cmin*(Tai-Twi)); %HEX effectiveness
NTU = (1/(Cr-1))*log((e-1)/((e*Cr)-1)); % Net Transfer Units
Aht = pi*ODw*L; % heat transfer area (ft^2), or the outside surface area of the feedwater tube
U = (NTU*Cmin)/Aht; % overall heat transfer coefficient
%%% PART B
mw2 = [0.01:0.01:0.1]; %water mass flow rate in kg/s
rho_w = 798.6; %lg/m^3
Ac = 0.25*pi*IDw^2; %cross-sectional area of the inside of the feedwater tube
Dh = (4*Ac)/(4*pi*IDw); % hydraulic diameter for the feedwater tube
muw = 0.0001063; % dynamic visocsity of the water @ 250C (N*s/m^2)
Uw = zeros(1,length(mw2));
Red = zeros(1,length(mw2));
Nud = zeros(1,length(mw2));
k = 8; %W/m-K. thermal conductivity of water @250-C, guess beased on lower figures
step = Dh/9;
dDh = [0:step:Dh]; %size of the hydraulic diameter
h = zeros(length(mw2),length(dDh));
for i = 1:10
Uw(i) = mw2(i)/(rho_w*Ac); %velocity of the feedwater for the varying flow rates
Red(i) = (rho_w*Uw(i)*Dh)/muw;
Pr = (muw*cpw)/k;
if Red <= 5000
Nud = 4.36;
elseif Red > 5000
Nud(i) = 0.023*(Red(i)^0.8)*(Pr^0.4);
end
for j = 1:10
h(i,j) = (Nud(i)*k)/dDh(j);
end
end“` In my code, I am analyzing a heat concentric heat exchanger and am solving for h, the local convection coefficients at points along the length of the of tube. In my nested for loop, indexed by j, I am attempting to update the length 0, dDh, for each iteration of the Nsselt number Nud. However, I keep getting the error that the index exceeds the number of array elements. there are 10 distance steps and 10 mass flow rates to analyze, so I am confused on why the sizes are a probelm. Any help is appreciated.
“`clear
clc
% givens for the air
ma = 15; %kg/s
Tai = 1200; % deg-C
cpa = 1.207; % kJ/kg-K @ 1200C
ODa = 0.05; % m
IDa = 0.04; % m
% givens for the feedwater
mw = 10; %kg/s
Twi = 250; % deg-C
Two = 500; % deg-C
cpw = 4.860; % kJ/kg-K @ 250C
ODw = 0.01; %m
IDw = 0.008; %m
L = 5; %m, length of HEX bundle
Cc = mw*cpw;
Ch = ma*cpa;
A = [Cc, Ch];
Cmin = min(A);
Cmax = max(A);
Cr = Cmin/Cmax;
Tao = Tai – ((Cmax/Cmin)*(Two-Twi)); %air outlet temperature in deg-C
dT1 = Tai-Two;
dT2 = Tao – Twi;
LMTD = (dT2 – dT1)/log(dT2/dT1); %log mean temperature difference for the HEX
e = (Cc*(Two-Twi))/(Cmin*(Tai-Twi)); %HEX effectiveness
NTU = (1/(Cr-1))*log((e-1)/((e*Cr)-1)); % Net Transfer Units
Aht = pi*ODw*L; % heat transfer area (ft^2), or the outside surface area of the feedwater tube
U = (NTU*Cmin)/Aht; % overall heat transfer coefficient
%%% PART B
mw2 = [0.01:0.01:0.1]; %water mass flow rate in kg/s
rho_w = 798.6; %lg/m^3
Ac = 0.25*pi*IDw^2; %cross-sectional area of the inside of the feedwater tube
Dh = (4*Ac)/(4*pi*IDw); % hydraulic diameter for the feedwater tube
muw = 0.0001063; % dynamic visocsity of the water @ 250C (N*s/m^2)
Uw = zeros(1,length(mw2));
Red = zeros(1,length(mw2));
Nud = zeros(1,length(mw2));
k = 8; %W/m-K. thermal conductivity of water @250-C, guess beased on lower figures
step = Dh/9;
dDh = [0:step:Dh]; %size of the hydraulic diameter
h = zeros(length(mw2),length(dDh));
for i = 1:10
Uw(i) = mw2(i)/(rho_w*Ac); %velocity of the feedwater for the varying flow rates
Red(i) = (rho_w*Uw(i)*Dh)/muw;
Pr = (muw*cpw)/k;
if Red <= 5000
Nud = 4.36;
elseif Red > 5000
Nud(i) = 0.023*(Red(i)^0.8)*(Pr^0.4);
end
for j = 1:10
h(i,j) = (Nud(i)*k)/dDh(j);
end
end“` for loop, for, loop, nested, nusselt, coefficient, convection, local MATLAB Answers — New Questions
Index exceeds the number of array elements, must not exceed 1, in a nested for loop
In my code, I am analyzing a heat concentric heat exchanger and am solving for h, the local convection coefficients at points along the length of the of tube. In my nested for loop, indexed by j, I am attempting to update the length 0, dDh, for each iteration of the Nsselt number Nud. However, I keep getting the error that the index exceeds the number of array elements. there are 10 distance steps and 10 mass flow rates to analyze, so I am confused on why the sizes are a probelm. Any help is appreciated.
“`clear
clc
% givens for the air
ma = 15; %kg/s
Tai = 1200; % deg-C
cpa = 1.207; % kJ/kg-K @ 1200C
ODa = 0.05; % m
IDa = 0.04; % m
% givens for the feedwater
mw = 10; %kg/s
Twi = 250; % deg-C
Two = 500; % deg-C
cpw = 4.860; % kJ/kg-K @ 250C
ODw = 0.01; %m
IDw = 0.008; %m
L = 5; %m, length of HEX bundle
Cc = mw*cpw;
Ch = ma*cpa;
A = [Cc, Ch];
Cmin = min(A);
Cmax = max(A);
Cr = Cmin/Cmax;
Tao = Tai – ((Cmax/Cmin)*(Two-Twi)); %air outlet temperature in deg-C
dT1 = Tai-Two;
dT2 = Tao – Twi;
LMTD = (dT2 – dT1)/log(dT2/dT1); %log mean temperature difference for the HEX
e = (Cc*(Two-Twi))/(Cmin*(Tai-Twi)); %HEX effectiveness
NTU = (1/(Cr-1))*log((e-1)/((e*Cr)-1)); % Net Transfer Units
Aht = pi*ODw*L; % heat transfer area (ft^2), or the outside surface area of the feedwater tube
U = (NTU*Cmin)/Aht; % overall heat transfer coefficient
%%% PART B
mw2 = [0.01:0.01:0.1]; %water mass flow rate in kg/s
rho_w = 798.6; %lg/m^3
Ac = 0.25*pi*IDw^2; %cross-sectional area of the inside of the feedwater tube
Dh = (4*Ac)/(4*pi*IDw); % hydraulic diameter for the feedwater tube
muw = 0.0001063; % dynamic visocsity of the water @ 250C (N*s/m^2)
Uw = zeros(1,length(mw2));
Red = zeros(1,length(mw2));
Nud = zeros(1,length(mw2));
k = 8; %W/m-K. thermal conductivity of water @250-C, guess beased on lower figures
step = Dh/9;
dDh = [0:step:Dh]; %size of the hydraulic diameter
h = zeros(length(mw2),length(dDh));
for i = 1:10
Uw(i) = mw2(i)/(rho_w*Ac); %velocity of the feedwater for the varying flow rates
Red(i) = (rho_w*Uw(i)*Dh)/muw;
Pr = (muw*cpw)/k;
if Red <= 5000
Nud = 4.36;
elseif Red > 5000
Nud(i) = 0.023*(Red(i)^0.8)*(Pr^0.4);
end
for j = 1:10
h(i,j) = (Nud(i)*k)/dDh(j);
end
end“`In my code, I am analyzing a heat concentric heat exchanger and am solving for h, the local convection coefficients at points along the length of the of tube. In my nested for loop, indexed by j, I am attempting to update the length 0, dDh, for each iteration of the Nsselt number Nud. However, I keep getting the error that the index exceeds the number of array elements. there are 10 distance steps and 10 mass flow rates to analyze, so I am confused on why the sizes are a probelm. Any help is appreciated.
“`clear
clc
% givens for the air
ma = 15; %kg/s
Tai = 1200; % deg-C
cpa = 1.207; % kJ/kg-K @ 1200C
ODa = 0.05; % m
IDa = 0.04; % m
% givens for the feedwater
mw = 10; %kg/s
Twi = 250; % deg-C
Two = 500; % deg-C
cpw = 4.860; % kJ/kg-K @ 250C
ODw = 0.01; %m
IDw = 0.008; %m
L = 5; %m, length of HEX bundle
Cc = mw*cpw;
Ch = ma*cpa;
A = [Cc, Ch];
Cmin = min(A);
Cmax = max(A);
Cr = Cmin/Cmax;
Tao = Tai – ((Cmax/Cmin)*(Two-Twi)); %air outlet temperature in deg-C
dT1 = Tai-Two;
dT2 = Tao – Twi;
LMTD = (dT2 – dT1)/log(dT2/dT1); %log mean temperature difference for the HEX
e = (Cc*(Two-Twi))/(Cmin*(Tai-Twi)); %HEX effectiveness
NTU = (1/(Cr-1))*log((e-1)/((e*Cr)-1)); % Net Transfer Units
Aht = pi*ODw*L; % heat transfer area (ft^2), or the outside surface area of the feedwater tube
U = (NTU*Cmin)/Aht; % overall heat transfer coefficient
%%% PART B
mw2 = [0.01:0.01:0.1]; %water mass flow rate in kg/s
rho_w = 798.6; %lg/m^3
Ac = 0.25*pi*IDw^2; %cross-sectional area of the inside of the feedwater tube
Dh = (4*Ac)/(4*pi*IDw); % hydraulic diameter for the feedwater tube
muw = 0.0001063; % dynamic visocsity of the water @ 250C (N*s/m^2)
Uw = zeros(1,length(mw2));
Red = zeros(1,length(mw2));
Nud = zeros(1,length(mw2));
k = 8; %W/m-K. thermal conductivity of water @250-C, guess beased on lower figures
step = Dh/9;
dDh = [0:step:Dh]; %size of the hydraulic diameter
h = zeros(length(mw2),length(dDh));
for i = 1:10
Uw(i) = mw2(i)/(rho_w*Ac); %velocity of the feedwater for the varying flow rates
Red(i) = (rho_w*Uw(i)*Dh)/muw;
Pr = (muw*cpw)/k;
if Red <= 5000
Nud = 4.36;
elseif Red > 5000
Nud(i) = 0.023*(Red(i)^0.8)*(Pr^0.4);
end
for j = 1:10
h(i,j) = (Nud(i)*k)/dDh(j);
end
end“` In my code, I am analyzing a heat concentric heat exchanger and am solving for h, the local convection coefficients at points along the length of the of tube. In my nested for loop, indexed by j, I am attempting to update the length 0, dDh, for each iteration of the Nsselt number Nud. However, I keep getting the error that the index exceeds the number of array elements. there are 10 distance steps and 10 mass flow rates to analyze, so I am confused on why the sizes are a probelm. Any help is appreciated.
“`clear
clc
% givens for the air
ma = 15; %kg/s
Tai = 1200; % deg-C
cpa = 1.207; % kJ/kg-K @ 1200C
ODa = 0.05; % m
IDa = 0.04; % m
% givens for the feedwater
mw = 10; %kg/s
Twi = 250; % deg-C
Two = 500; % deg-C
cpw = 4.860; % kJ/kg-K @ 250C
ODw = 0.01; %m
IDw = 0.008; %m
L = 5; %m, length of HEX bundle
Cc = mw*cpw;
Ch = ma*cpa;
A = [Cc, Ch];
Cmin = min(A);
Cmax = max(A);
Cr = Cmin/Cmax;
Tao = Tai – ((Cmax/Cmin)*(Two-Twi)); %air outlet temperature in deg-C
dT1 = Tai-Two;
dT2 = Tao – Twi;
LMTD = (dT2 – dT1)/log(dT2/dT1); %log mean temperature difference for the HEX
e = (Cc*(Two-Twi))/(Cmin*(Tai-Twi)); %HEX effectiveness
NTU = (1/(Cr-1))*log((e-1)/((e*Cr)-1)); % Net Transfer Units
Aht = pi*ODw*L; % heat transfer area (ft^2), or the outside surface area of the feedwater tube
U = (NTU*Cmin)/Aht; % overall heat transfer coefficient
%%% PART B
mw2 = [0.01:0.01:0.1]; %water mass flow rate in kg/s
rho_w = 798.6; %lg/m^3
Ac = 0.25*pi*IDw^2; %cross-sectional area of the inside of the feedwater tube
Dh = (4*Ac)/(4*pi*IDw); % hydraulic diameter for the feedwater tube
muw = 0.0001063; % dynamic visocsity of the water @ 250C (N*s/m^2)
Uw = zeros(1,length(mw2));
Red = zeros(1,length(mw2));
Nud = zeros(1,length(mw2));
k = 8; %W/m-K. thermal conductivity of water @250-C, guess beased on lower figures
step = Dh/9;
dDh = [0:step:Dh]; %size of the hydraulic diameter
h = zeros(length(mw2),length(dDh));
for i = 1:10
Uw(i) = mw2(i)/(rho_w*Ac); %velocity of the feedwater for the varying flow rates
Red(i) = (rho_w*Uw(i)*Dh)/muw;
Pr = (muw*cpw)/k;
if Red <= 5000
Nud = 4.36;
elseif Red > 5000
Nud(i) = 0.023*(Red(i)^0.8)*(Pr^0.4);
end
for j = 1:10
h(i,j) = (Nud(i)*k)/dDh(j);
end
end“` for loop, for, loop, nested, nusselt, coefficient, convection, local MATLAB Answers — New Questions
Index exceeds the number of array elements, must not exceed 1, in a nested for loop
In my code, I am analyzing a heat concentric heat exchanger and am solving for h, the local convection coefficients at points along the length of the of tube. In my nested for loop, indexed by j, I am attempting to update the length 0, dDh, for each iteration of the Nsselt number Nud. However, I keep getting the error that the index exceeds the number of array elements. there are 10 distance steps and 10 mass flow rates to analyze, so I am confused on why the sizes are a probelm. Any help is appreciated.
“`clear
clc
% givens for the air
ma = 15; %kg/s
Tai = 1200; % deg-C
cpa = 1.207; % kJ/kg-K @ 1200C
ODa = 0.05; % m
IDa = 0.04; % m
% givens for the feedwater
mw = 10; %kg/s
Twi = 250; % deg-C
Two = 500; % deg-C
cpw = 4.860; % kJ/kg-K @ 250C
ODw = 0.01; %m
IDw = 0.008; %m
L = 5; %m, length of HEX bundle
Cc = mw*cpw;
Ch = ma*cpa;
A = [Cc, Ch];
Cmin = min(A);
Cmax = max(A);
Cr = Cmin/Cmax;
Tao = Tai – ((Cmax/Cmin)*(Two-Twi)); %air outlet temperature in deg-C
dT1 = Tai-Two;
dT2 = Tao – Twi;
LMTD = (dT2 – dT1)/log(dT2/dT1); %log mean temperature difference for the HEX
e = (Cc*(Two-Twi))/(Cmin*(Tai-Twi)); %HEX effectiveness
NTU = (1/(Cr-1))*log((e-1)/((e*Cr)-1)); % Net Transfer Units
Aht = pi*ODw*L; % heat transfer area (ft^2), or the outside surface area of the feedwater tube
U = (NTU*Cmin)/Aht; % overall heat transfer coefficient
%%% PART B
mw2 = [0.01:0.01:0.1]; %water mass flow rate in kg/s
rho_w = 798.6; %lg/m^3
Ac = 0.25*pi*IDw^2; %cross-sectional area of the inside of the feedwater tube
Dh = (4*Ac)/(4*pi*IDw); % hydraulic diameter for the feedwater tube
muw = 0.0001063; % dynamic visocsity of the water @ 250C (N*s/m^2)
Uw = zeros(1,length(mw2));
Red = zeros(1,length(mw2));
Nud = zeros(1,length(mw2));
k = 8; %W/m-K. thermal conductivity of water @250-C, guess beased on lower figures
step = Dh/9;
dDh = [0:step:Dh]; %size of the hydraulic diameter
h = zeros(length(mw2),length(dDh));
for i = 1:10
Uw(i) = mw2(i)/(rho_w*Ac); %velocity of the feedwater for the varying flow rates
Red(i) = (rho_w*Uw(i)*Dh)/muw;
Pr = (muw*cpw)/k;
if Red <= 5000
Nud = 4.36;
elseif Red > 5000
Nud(i) = 0.023*(Red(i)^0.8)*(Pr^0.4);
end
for j = 1:10
h(i,j) = (Nud(i)*k)/dDh(j);
end
end“`In my code, I am analyzing a heat concentric heat exchanger and am solving for h, the local convection coefficients at points along the length of the of tube. In my nested for loop, indexed by j, I am attempting to update the length 0, dDh, for each iteration of the Nsselt number Nud. However, I keep getting the error that the index exceeds the number of array elements. there are 10 distance steps and 10 mass flow rates to analyze, so I am confused on why the sizes are a probelm. Any help is appreciated.
“`clear
clc
% givens for the air
ma = 15; %kg/s
Tai = 1200; % deg-C
cpa = 1.207; % kJ/kg-K @ 1200C
ODa = 0.05; % m
IDa = 0.04; % m
% givens for the feedwater
mw = 10; %kg/s
Twi = 250; % deg-C
Two = 500; % deg-C
cpw = 4.860; % kJ/kg-K @ 250C
ODw = 0.01; %m
IDw = 0.008; %m
L = 5; %m, length of HEX bundle
Cc = mw*cpw;
Ch = ma*cpa;
A = [Cc, Ch];
Cmin = min(A);
Cmax = max(A);
Cr = Cmin/Cmax;
Tao = Tai – ((Cmax/Cmin)*(Two-Twi)); %air outlet temperature in deg-C
dT1 = Tai-Two;
dT2 = Tao – Twi;
LMTD = (dT2 – dT1)/log(dT2/dT1); %log mean temperature difference for the HEX
e = (Cc*(Two-Twi))/(Cmin*(Tai-Twi)); %HEX effectiveness
NTU = (1/(Cr-1))*log((e-1)/((e*Cr)-1)); % Net Transfer Units
Aht = pi*ODw*L; % heat transfer area (ft^2), or the outside surface area of the feedwater tube
U = (NTU*Cmin)/Aht; % overall heat transfer coefficient
%%% PART B
mw2 = [0.01:0.01:0.1]; %water mass flow rate in kg/s
rho_w = 798.6; %lg/m^3
Ac = 0.25*pi*IDw^2; %cross-sectional area of the inside of the feedwater tube
Dh = (4*Ac)/(4*pi*IDw); % hydraulic diameter for the feedwater tube
muw = 0.0001063; % dynamic visocsity of the water @ 250C (N*s/m^2)
Uw = zeros(1,length(mw2));
Red = zeros(1,length(mw2));
Nud = zeros(1,length(mw2));
k = 8; %W/m-K. thermal conductivity of water @250-C, guess beased on lower figures
step = Dh/9;
dDh = [0:step:Dh]; %size of the hydraulic diameter
h = zeros(length(mw2),length(dDh));
for i = 1:10
Uw(i) = mw2(i)/(rho_w*Ac); %velocity of the feedwater for the varying flow rates
Red(i) = (rho_w*Uw(i)*Dh)/muw;
Pr = (muw*cpw)/k;
if Red <= 5000
Nud = 4.36;
elseif Red > 5000
Nud(i) = 0.023*(Red(i)^0.8)*(Pr^0.4);
end
for j = 1:10
h(i,j) = (Nud(i)*k)/dDh(j);
end
end“` In my code, I am analyzing a heat concentric heat exchanger and am solving for h, the local convection coefficients at points along the length of the of tube. In my nested for loop, indexed by j, I am attempting to update the length 0, dDh, for each iteration of the Nsselt number Nud. However, I keep getting the error that the index exceeds the number of array elements. there are 10 distance steps and 10 mass flow rates to analyze, so I am confused on why the sizes are a probelm. Any help is appreciated.
“`clear
clc
% givens for the air
ma = 15; %kg/s
Tai = 1200; % deg-C
cpa = 1.207; % kJ/kg-K @ 1200C
ODa = 0.05; % m
IDa = 0.04; % m
% givens for the feedwater
mw = 10; %kg/s
Twi = 250; % deg-C
Two = 500; % deg-C
cpw = 4.860; % kJ/kg-K @ 250C
ODw = 0.01; %m
IDw = 0.008; %m
L = 5; %m, length of HEX bundle
Cc = mw*cpw;
Ch = ma*cpa;
A = [Cc, Ch];
Cmin = min(A);
Cmax = max(A);
Cr = Cmin/Cmax;
Tao = Tai – ((Cmax/Cmin)*(Two-Twi)); %air outlet temperature in deg-C
dT1 = Tai-Two;
dT2 = Tao – Twi;
LMTD = (dT2 – dT1)/log(dT2/dT1); %log mean temperature difference for the HEX
e = (Cc*(Two-Twi))/(Cmin*(Tai-Twi)); %HEX effectiveness
NTU = (1/(Cr-1))*log((e-1)/((e*Cr)-1)); % Net Transfer Units
Aht = pi*ODw*L; % heat transfer area (ft^2), or the outside surface area of the feedwater tube
U = (NTU*Cmin)/Aht; % overall heat transfer coefficient
%%% PART B
mw2 = [0.01:0.01:0.1]; %water mass flow rate in kg/s
rho_w = 798.6; %lg/m^3
Ac = 0.25*pi*IDw^2; %cross-sectional area of the inside of the feedwater tube
Dh = (4*Ac)/(4*pi*IDw); % hydraulic diameter for the feedwater tube
muw = 0.0001063; % dynamic visocsity of the water @ 250C (N*s/m^2)
Uw = zeros(1,length(mw2));
Red = zeros(1,length(mw2));
Nud = zeros(1,length(mw2));
k = 8; %W/m-K. thermal conductivity of water @250-C, guess beased on lower figures
step = Dh/9;
dDh = [0:step:Dh]; %size of the hydraulic diameter
h = zeros(length(mw2),length(dDh));
for i = 1:10
Uw(i) = mw2(i)/(rho_w*Ac); %velocity of the feedwater for the varying flow rates
Red(i) = (rho_w*Uw(i)*Dh)/muw;
Pr = (muw*cpw)/k;
if Red <= 5000
Nud = 4.36;
elseif Red > 5000
Nud(i) = 0.023*(Red(i)^0.8)*(Pr^0.4);
end
for j = 1:10
h(i,j) = (Nud(i)*k)/dDh(j);
end
end“` for loop, for, loop, nested, nusselt, coefficient, convection, local MATLAB Answers — New Questions
Significant percent difference in calculated answer and code answer.
The code for a project I am doing looks like this:
% Convert flow rate to cubic feet per second
Q = Q / 448.831;
% Initial guess for Darcy friction factor f
f_guess = 0.02;
% Iterative solution for f using the Colebrook equation
maxIterations = 100000;
tolerance = 1e-6;
f_convergence = zeros(1, maxIterations);
for i = 1:maxIterations
f_next = 1 / (2 * log10(abs((epsilon/12) / (3.7) + (2.51 / (Re(Q, f_guess, mu))) / sqrt(f_guess))));
% Ensure that the friction factor is positive
f_next = max(f_next, 1e-6); % Use a small positive value if necessary
% Check for convergence
if abs(f_next – f_guess) < tolerance
f = f_next;
break;
end
f_guess = f_next;
f_convergence(i) = f_guess;
end
% Iterative solution for the diameter of the pipe using the Darcy-Weisbach equation
D_guess = 1;
D_convergence = zeros(1, maxIterations);
for i = 1:maxIterations
D_next = ((4 * f * L * Q^2) / (pi^2 * g * D_guess^5) + h_loss)^(1/5);
% Ensure that the diameter is positive
D_next = max(D_next, 1e-6); % Use a small positive value if necessary
% Check for convergence
if abs(D_next – D_guess) < tolerance
D = D_next;
break;
end
D_guess = D_next;
D_convergence(i) = D_guess;
end
This is not all of the code but this is where I am certain the error is occuring, my professors calculated result is 1.33 ft, however on I am getting an astoundingly large number of 2.36. I was wondering if there is any obvious mistakes I made in the iteration that are resulting in this.
The variables are:
Given data
Q = 4000; % Volumetric flow rate in gpm
L = 10000; % Length of the pipe in ft
h_loss = 75; % Head loss in ft
epsilon = 0.00015; % Roughness of the pipe in ft
g = 32.2; % Acceleration due to gravity in ft/s^2
mu = 0.00001; % Dynamic viscosity in ft^2/sThe code for a project I am doing looks like this:
% Convert flow rate to cubic feet per second
Q = Q / 448.831;
% Initial guess for Darcy friction factor f
f_guess = 0.02;
% Iterative solution for f using the Colebrook equation
maxIterations = 100000;
tolerance = 1e-6;
f_convergence = zeros(1, maxIterations);
for i = 1:maxIterations
f_next = 1 / (2 * log10(abs((epsilon/12) / (3.7) + (2.51 / (Re(Q, f_guess, mu))) / sqrt(f_guess))));
% Ensure that the friction factor is positive
f_next = max(f_next, 1e-6); % Use a small positive value if necessary
% Check for convergence
if abs(f_next – f_guess) < tolerance
f = f_next;
break;
end
f_guess = f_next;
f_convergence(i) = f_guess;
end
% Iterative solution for the diameter of the pipe using the Darcy-Weisbach equation
D_guess = 1;
D_convergence = zeros(1, maxIterations);
for i = 1:maxIterations
D_next = ((4 * f * L * Q^2) / (pi^2 * g * D_guess^5) + h_loss)^(1/5);
% Ensure that the diameter is positive
D_next = max(D_next, 1e-6); % Use a small positive value if necessary
% Check for convergence
if abs(D_next – D_guess) < tolerance
D = D_next;
break;
end
D_guess = D_next;
D_convergence(i) = D_guess;
end
This is not all of the code but this is where I am certain the error is occuring, my professors calculated result is 1.33 ft, however on I am getting an astoundingly large number of 2.36. I was wondering if there is any obvious mistakes I made in the iteration that are resulting in this.
The variables are:
Given data
Q = 4000; % Volumetric flow rate in gpm
L = 10000; % Length of the pipe in ft
h_loss = 75; % Head loss in ft
epsilon = 0.00015; % Roughness of the pipe in ft
g = 32.2; % Acceleration due to gravity in ft/s^2
mu = 0.00001; % Dynamic viscosity in ft^2/s The code for a project I am doing looks like this:
% Convert flow rate to cubic feet per second
Q = Q / 448.831;
% Initial guess for Darcy friction factor f
f_guess = 0.02;
% Iterative solution for f using the Colebrook equation
maxIterations = 100000;
tolerance = 1e-6;
f_convergence = zeros(1, maxIterations);
for i = 1:maxIterations
f_next = 1 / (2 * log10(abs((epsilon/12) / (3.7) + (2.51 / (Re(Q, f_guess, mu))) / sqrt(f_guess))));
% Ensure that the friction factor is positive
f_next = max(f_next, 1e-6); % Use a small positive value if necessary
% Check for convergence
if abs(f_next – f_guess) < tolerance
f = f_next;
break;
end
f_guess = f_next;
f_convergence(i) = f_guess;
end
% Iterative solution for the diameter of the pipe using the Darcy-Weisbach equation
D_guess = 1;
D_convergence = zeros(1, maxIterations);
for i = 1:maxIterations
D_next = ((4 * f * L * Q^2) / (pi^2 * g * D_guess^5) + h_loss)^(1/5);
% Ensure that the diameter is positive
D_next = max(D_next, 1e-6); % Use a small positive value if necessary
% Check for convergence
if abs(D_next – D_guess) < tolerance
D = D_next;
break;
end
D_guess = D_next;
D_convergence(i) = D_guess;
end
This is not all of the code but this is where I am certain the error is occuring, my professors calculated result is 1.33 ft, however on I am getting an astoundingly large number of 2.36. I was wondering if there is any obvious mistakes I made in the iteration that are resulting in this.
The variables are:
Given data
Q = 4000; % Volumetric flow rate in gpm
L = 10000; % Length of the pipe in ft
h_loss = 75; % Head loss in ft
epsilon = 0.00015; % Roughness of the pipe in ft
g = 32.2; % Acceleration due to gravity in ft/s^2
mu = 0.00001; % Dynamic viscosity in ft^2/s newtons method MATLAB Answers — New Questions