Matlab

Matlab Saves

版本:Matlab 2020b

binary_search.m:

function [x, iteration] = binary_search(f, l, r)
    iteration = [];
% __SPECIAL_JUDGE_BEGIN__
    if f(l) * f(r) > 0, x = '__BAD_INPUT__'; return; end
    if f(l) == 0 & f(r) == 0, x = [l r]; return;
    elseif f(l) == 0, x = l; return;
    elseif f(r) == 0, x = r; return;
    end
% __SPECIAL_JUDGE_END__
    i = 2;
    m = (l + r) / 2;
    iteration(1) = m;
    while abs(f(m)) >= 1e-7
        if f(m) * f(l) < 0, r = m; m = (l + r) / 2;
        else l = m; m = (l + r) / 2;
        iteration(i) = m;
        i = i + 1;
    end
    x = m;
end

绕x轴旋转示例:

x = -1: 0.01: 1;
t = (0: 0.01: 2 * pi)';
val = x - x .* sin(3 * x);
y = cos(t) * val;
z = sin(t) * val;
[x, t] = meshgrid(x, t);
mesh(x, y, z);

Logistic 迭代:

function f = logistic(a, x0, n)
    if nargin == 2, n = 300; end
    x = 0: 0.01: 1;
    y = a * x .*(1 - x);
    plot(x, x, 'r', x, y, 'r');
    hold on
    clear x y
    x(1) = x0;
    y(1) = a * x(1) * (1 - x(1));
    x(2) = y(1);
    plot([x(1) x(1) x(2)], [0 y(1) y(1)]);
    for i = 2: n
        y(i) = a * x(i) * (1 - x(i));
        x(i + 1) = y(i);
        plot([x(i) x(i) x(i + 1)], [y(i - 1) y(i) y(i)]);
    end
    hold off
end

Newton 迭代:

function x = newton_iterate(f, x0, e)
    syms t
    y = f(t);
    df = matlabFunction(diff(y));
    if nargin < 3, e = 1e-4; end
    x  = x0;
    x0 = x + 2 * e;
    while abs(x0 - x) > e
        x0 = x;
        x  = x0 - f(x0) / df(x0);
    end
end

订阅评论
提醒
guest
0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请发表评论。x
()
x