can someone help me pls with this code?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
function main
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
x0 = 80;
eps = 0.4;
c_s = 5.67*10^(-8);
s1 = 0.250;
s2 = 0.015;
lamda1 = 0.35;
lamda2 = 22.7;
Tw_1 = 1200;
T_l = 10;
Tw_1 = 1200;
T_l = 10;
lambda_L = 25.12;
betha = 3.543*10^(-3);
v = 144.0*10^(-7);
L = 7;
B = 15;
A = B * L;
g = 9.81;
Pr = 0.7095;
Ra = @(x)Pr*(g*L^3*betha*(x-T_l))/v^2;
alpha_k = @(x)((0.825+(0.387*Ra(x)^1/6)/(1+(0.492/Pr)^9/16)^8/27)^2)*lambda_L/L;
func = @(x) (eps * c_s * x^4) + (alpha_k(x) + 1/(s1/lamda1+s2/lamda2)) * x - ((1/(s1/lamda1+s2/lamda2)) * Tw_1) + (alpha_k(x) * T_l);
xsol = newton_raphson(func,x0)
alpha_k = alpha_k(xsol)
alpha_ges = alpha_k + (eps *c_s*(xsol.^4-T_l.^4)/(xsol-T_l))
Q_ges = (1/((s1/lamda1+s2/lamda2)+1/alpha_ges))*A*(Tw_1-T_l)
(1/(s1/lamda1+s2/lamda2))*(Tw_1-xsol) % heat conduction
alpha_k * (xsol-T_l) + (eps*c_s*xsol^4) % convection + thermal radiation
end
function xsol = newton_raphson(func,x0)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
x(1) = x0;
maxiter = 200;
tol = 10^(-5);
for i = 1:maxiter
diff_xi = (func(x(i)+1e-6)-func(x(i)))*1e6;
if diff_xi < tol
fprintf('Pitfall hast occured a better initial guess\n');
return;
end
x(i+1) = x(i) - func(x(i))/diff_xi;
abs_error(i+1) = abs((x(i+1)-x(i))/x(i+1))*100;
if abs(x(i+1) - x(i)) < tol
fprintf('The Root has converged at x = %.10f\n', x(i+1));
else
fprintf('Iteration no: %d,current guess x = %.10f, error = %.5f', i, x(i+1), abs_error(i+1));
end
end
xsol = x(end);
end
Unfortunately, I cannot calculate alpha_k and Ra in relation to x in this code. x is iterated using Newton's method and the result is then recalculated in alpha_k and Ra and x. The whole thing takes place until the x-value remains constant.
The result is correct if x no longer changes and these two equations have the same value
(1/(s1/lamda1+s2/lamda2))*(Tw_1-xsol) % heat conduction
alpha_k * (xsol-T_l) + (eps*c_s*xsol^4) % convection + thermal radiation
0 Commenti
Risposte (1)
Saarthak Gupta
il 24 Dic 2023
Modificato: Saarthak Gupta
il 24 Dic 2023
Hi Aryo,
It seems like you are facing an issue while implementing the Newton-Raphson method correctly.
I suspect that you are getting inaccurate results for “alpha_k” and “Ra” where the Newton-Raphson method is converging. This might be due to the existence of multiple solutions. The function in question has multiple roots, specifically, 10 and -10. If you initialize "x0" with a negative number, for instance, -100, the Newton-Raphson method will converge to the root -10 after a sufficient number of iterations.
Refer to the following code:
x0 = 80;
eps = 0.4;
c_s = 5.67*10^(-8);
s1 = 0.250;
s2 = 0.015;
lamda1 = 0.35;
lamda2 = 22.7;
Tw_1 = 1200;
T_l = 10;
Tw_1 = 1200;
T_l = 10;
lambda_L = 25.12;
betha = 3.543*10^(-3);
v = 144.0*10^(-7);
L = 7;
B = 15;
A = B * L;
g = 9.81;
Pr = 0.7095;
Ra = @(x)Pr*(g*L^3*betha*(x-T_l))/v^2;
alpha_k = @(x)((0.825+(0.387*Ra(x)^1/6)/(1+(0.492/Pr)^9/16)^8/27)^2)*lambda_L/L;
func = @(x) (eps * c_s * x^4) + (alpha_k(x) + 1/(s1/lamda1+s2/lamda2)) * x - ((1/(s1/lamda1+s2/lamda2)) * Tw_1) + (alpha_k(x) * T_l);
% calculate root of func using fzero
x_true = fzero(func,x0,optimset("Display","iter"));
% initialize x0 to a negative value
x = -100;
x_old = 1000;
iter = 0;
while abs(x_old-x) > 1e-10 && iter <= 20 % x ~= 0
x_old = x;
dfunc = (func(x+1e-6)-func(x))*1e6; % derivative of func
x = x - func(x)/dfunc;
iter = iter + 1;
fprintf('Iteration %d: x=%.18f, err=%.18f\n', iter, x, x_true-x);
pause(1);
end
ak = alpha_k(x_true)
alpha_ges = ak + (eps *c_s*(x_true.^4-T_l.^4)/(x_true-T_l))
Q_ges = (1/((s1/lamda1+s2/lamda2)+1/alpha_ges))*A*(Tw_1-T_l)
(1/(s1/lamda1+s2/lamda2))*(Tw_1-x_true) % heat conduction
ak * (x_true-T_l) + (eps*c_s*x_true^4) % convection + thermal radiation
I have used the "fzero" function rather than the Newton-Raphson method you implemented. "fzero" incorporates a mix of bisection, secant, and inverse quadratic interpolation techniques. To verify, consider comparing your outcomes with those obtained using this method.
Refer to the following MATLAB documentation for further reference:
Hope this helps!
Best regards,
Saarthak
0 Commenti
Vedere anche
Categorie
Scopri di più su Calculus in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!