Solving equation with two for loops
Show older comments
Hi I would like to find the values of gf for different r and H values. It is obtained by solving the equation S1 which includes gf on both left and right sides. Could you please help me in solving this?
clear all;
close all;
iiy = 1.8;
rho_s = 2160;
rho_w = 997;
M_s = 58.44 * 10^-3;
M_w = 18* 10^-3
o = 0.072;
H = linspace(0.1,1,100)
T = 22+273;
Rv = 8.314;
r = 1E-9:100E-9:200E-9;
syms gf
for i = 1:length(H)
for j =1:length(r)
S1 = ( gf -((rho_s/rho_w) *(1 + (M_w/M_s * iiy * H(i)./(( exp(2*o)./(rho_w*Rv*T.*gf*r(j))) - H(i)))))^(1/3));
S(j,i) = vpasolve(S1(j,i)==0,gf, [0 Inf] );
end
end
Accepted Answer
More Answers (2)
S1 is calculated for a given i and j, so it is not correct to index it with i and j on the next line.
That is, use S1==0 instead of S1(j,i) == 0
clear all;
close all;
iiy = 1.8;
rho_s = 2160;
rho_w = 997;
M_s = 58.44 * 10^-3;
M_w = 18* 10^-3
o = 0.072;
H = linspace(0.1,1,100)
T = 22+273;
Rv = 8.314;
r = 1E-9:100E-9:200E-9;
syms gf
for i = 1:length(H)
for j =1:length(r)
S1 = ( gf -((rho_s/rho_w) *(1 + (M_w/M_s * iiy * H(i)./(( exp(2*o)./(rho_w*Rv*T.*gf*r(j))) - H(i)))))^(1/3));
% S(j,i) = vpasolve(S1(j,i)==0,gf, [0 Inf] );
S(j,i) = vpasolve(S1==0,gf, [0 Inf] );
end
end
disp(S)
3 Comments
DS
on 15 Nov 2022
Walter Roberson
on 15 Nov 2022
The implication is that there is a combination of values that vpasolve is not able to find a solution for.
DS
on 15 Nov 2022
iiy = 1.8;
rho_s = 2160;
rho_w = 997;
M_s = 58.44 * 10^-3;
M_w = 18* 10^-3
o = 0.072;
H = linspace(0.1,1,100)
T = 22+273;
Rv = 8.314;
r = 1E-9:100E-9:200E-9;
syms gf
numH = length(H);
numr = length(r);
S = zeros(numr, numH);
for i = 1:numH
for j = 1:numr
S1 = ( gf -((rho_s/rho_w) *(1 + (M_w/M_s * iiy * H(i)./(( exp(2*o)./(rho_w*Rv*T.*gf*r(j))) - H(i)))))^(1/3));
S(j,i) = vpasolve(S1==0, gf, [0 Inf] );
end
end
format long g
S
3 Comments
DS
on 15 Nov 2022
Your equation might have no solution or vpasolve might fail to compute it ...
iiy = 1.8;
rho_s = 2160;
rho_w = 997;
M_s = 58.44 * 10^-3;
M_w = 18* 10^-3
M_w = 0.0180
o = 0.072;
H = linspace(0.1,1,100)
T = 22+273;
Rv = 8.314;
r = linspace(1E-9,1000E-9, 20);
syms gf
numH = length(H);
numr = length(r);
S = NaN(numr, numH);
for i = 1:numH
for j = 1:numr
S1 = ( gf -((rho_s/rho_w) *(1 + (M_w/M_s * iiy * H(i)./(( exp(2*o)./(rho_w*Rv*T.*gf*r(j))) - H(i)))))^(1/3));
SS = vpasolve(S1==0, gf, [0 Inf] );
if ~isempty(SS)
S(j,i) = double(SS);
end
end
end
format long g
S
Categories
Find more on Symbolic Math Toolbox 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!