syms t; p = 2; f = matlabFunction(sqrt(1/(2*p))*exp(-0.5*t^2)); f_fun = @(x) integral(@(t) f(t), 0.5, x); g_fun = @(x) f_fun(x) - 0.45; w = [5/9, 8/9, 5/9]; x_nodes = [-sqrt(3/5), 0, sqrt(3/5)]; x0 = 0.5; tol = 1e-6; df_fun = matlabFunction(diff(f(t), t)); g1_fun = @(x) f_fun(x0) + w(1)*f_fun(x0 + 0.5*(x-x0)*x_nodes(1)) + w(2)*f_fun(x) + w(3)*f_fun(x0 + 0.5*(x-x0)*x_nodes(3)); g2_fun = @(x) x - g1_fun(x)/integral(@(t) f(t)/df_fun(t), 0, x); n = 0; xn = x0; while true n = n + 1; gx = g_fun(xn); gpx = diff(g_fun(t)); gpx = subs(gpx, t, xn); fprintf('迭代次数:%d, xn = %.6f, g(xn) = %.6f, g''(xn) = %.6f\n', n, xn, gx, gpx); xn1 = g2_fun(xn); if abs(xn1 - xn) < tol break; end xn = xn1; end fprintf('方程的根为:%.6f,迭代次数:%d\n', xn1, n); root = fzero(g_fun, x0); fprintf('使用fzero函数检验,方程的根为:%.6f\n', root);修改代码用matlabR2015bb
时间: 2024-04-28 15:22:33 浏览: 121
syms t; p = 2; f = matlabFunction(sqrt(1/(2*p))*exp(-0.5*t^2)); f_fun = @(x) integral(@(t) f(t), 0.5, x); g_fun = @(x) f_fun(x) - 0.45; w = [5/9, 8/9, 5/9]; x_nodes = [-sqrt(3/5), 0, sqrt(3/5)]; x0 = 0.5; tol = 1e-6; df_fun = matlabFunction(diff(f(t), t)); g1_fun = @(x) f_fun(x0) + w(1)*f_fun(x0 + 0.5*(x-x0)*x_nodes(1)) + w(2)*f_fun(x) + w(3)*f_fun(x0 + 0.5*(x-x0)*x_nodes(3)); g2_fun = @(x) x - g1_fun(x)/integral(@(t) f(t)/df_fun(t), 0, x); n = 0; xn = x0; while true n = n + 1; gx = g_fun(xn); gpx = diff(g_fun(t)); gpx = subs(gpx, t, xn); fprintf('迭代次数:%d, xn = %.6f, g(xn) = %.6f, g''(xn) = %.6f\n', n, xn, gx, gpx); xn1 = g2_fun(xn); if abs(xn1 - xn) < tol break; end xn = xn1; end fprintf('方程的根为:%.6f,迭代次数:%d\n', xn1, n); root = fzero(g_fun, x0); fprintf('使用fzero函数检验,方程的根为:%.6f\n', root);
阅读全文