Problems with the application of Newton's method
2 次查看(过去 30 天)
显示 更早的评论
function [] = newton_raphson(func, diff, x0)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
x = x0;
maxiter = 200;
tol = 10^(-5);
eps = 0.4;
c_s = 5.67*10^(-8);
alpha_k = 4;
s1 = 0.250;
s2 = 0.015;
lamda1 = 0.35;
lamda2 = 22.7;
Tw_1 = 1200;
T_l = 10;
func = @(x) eps * c_s * x^4 + (alpha_k + 1/(s1/lamda1+s2/lamda2)) * x - ((1/(s1/lamda1+s2/lamda2)) * Tw_1 + alpha_k * T_l);
diff = @(x) 4 * eps * c_s * x^3 + (alpha_k + 1/(s1/lamda1+s2/lamda2));
newton_raphson(func, diff, 200)
for i = 1:maxiter
if
diff(x(i)) < tol
fprintf('Pitfall hast occured a better initial guess\n');
return;
end
x(i+1) = x(i) - func(x(i))/diff(x(i));
abs_error(i+1) = abs((x(i+1)-x(i))/x(i+1))*100;
if
abs(x(i+1) - x(ix)) < 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
end
Can someone help me please. Unfortunately, I'm not quite fit in Matlab and have recently started working with functions. I don't know what's wrong with this code. Unfortunately I don't get a result. It had to come out with an X value of around 290.
Thanks a lot
0 个评论
采纳的回答
Torsten
2021-12-29
编辑:Torsten
2021-12-29
function main
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
x0 = 20;
maxiter = 200;
tol = 10^(-5);
eps = 0.4;
c_s = 5.67*10^(-8);
alpha_k = 4;
s1 = 0.250;
s2 = 0.015;
lamda1 = 0.35;
lamda2 = 22.7;
Tw_1 = 1200;
T_l = 10;
func = @(x) eps * c_s * x^4 + (alpha_k + 1/(s1/lamda1+s2/lamda2)) * x - ((1/(s1/lamda1+s2/lamda2)) * Tw_1 + alpha_k * T_l);
diff = @(x) 4 * eps * c_s * x^3 + (alpha_k + 1/(s1/lamda1+s2/lamda2));
xsol = newton_raphson(func, diff, x0)
end
function xsol = newton_raphson(func, diff, x0)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
x(1) = x0;
maxiter = 200;
tol = 10^(-5);
for i = 1:maxiter
if diff(x(i)) < tol
fprintf('Pitfall hast occured a better initial guess\n');
return;
end
x(i+1) = x(i) - func(x(i))/diff(x(i));
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
9 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!