matlab实现二分法解方程

matlab实现二分法解方程_第1张图片
matlab实现二分法解方程_第2张图片

function [x] = halfSolve(a, b, f ,tol)  % tol 是 tolerance 的缩写,表示绝对误差
    c = (a + b) / 2; k = 1;
    m = 1 + round((log(b - a) - log(2 * tol)) / log(2)); % <1>
    while k <= m + 10
        if (f(c) == 0)
            x = c;
            break;
        elseif f(a) * f(c) < 0
            b = (a + b) / 2;
        else
            a = (a + b) / 2;
        end
        c = (a + b) / 2; k = k + 1;
    end
    x = c; % 这里加分号是为了不再命令行中输出
    k % 不加分号就会在控制台输出
    c
end


clc;clear all;
v_y = 10;
R = 565.98;      %光源轨迹半径

syms lamda
fun = @(lamda)(lamda + (R/v_y) * sin(lamda) - pi);
a = 0;
b = pi;
tol = 1.0e-15;         %控制误差精度
result = halfSolve(a, b,fun,tol);

%% 求交点
syms lamda
eqn1 =  lamda + (R/v_y) * sin(lamda) - pi  == 0;
solx1 = vpasolve(eqn1 , lamda,[0,pi])



参考链接:
https://www.cnblogs.com/fanlumaster/p/14551258.html

你可能感兴趣的:(matlab操作小技巧,开发语言)