Solving equation with two for loops
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2022-11-18
I suggest you switch to numeric solutions, and to providing solution hints. Some of the tests I did hinted there might be multiple solutions.
The bumps along the yellow ridge appear to be inherent somehow -- if you zoom in you will see "ribs" on the plot.
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, 50);
numH = length(H);
numr = length(r);
S = NaN(numr, numH);
guess = 1.5;
opt = optimoptions('fsolve','Display','off');
for i = 1:numH
for j = 1:numr
S1 = @(gf) ( 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 = fsolve(S1, guess, opt );
if ~isempty(SS) && imag(SS) == 0
S(j,i) = double(SS);
guess = SS;
end
end
end
surf(H, r, S, 'edgecolor', 'none')
xlabel('H'); ylabel('r'); zlabel('S')
view([57,39])
0 个评论
更多回答(2 个)
Voss
2022-11-15
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 个评论
Walter Roberson
2022-11-15
The implication is that there is a combination of values that vpasolve is not able to find a solution for.
Walter Roberson
2022-11-15
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 个评论
Torsten
2022-11-15
编辑:Torsten
2022-11-15
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Number Theory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!