• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

matlab实现不动点迭代、牛顿法、割线法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

不动点迭代

function xc = fpi( g, x0, tol )
    x(1) = x0;
    i = 1;
    while 1
        x(i + 1) = g(x(i));
        if(abs(x(i+1) - x(i)) < tol)
            break
        end
        i = i + 1;
    end
    xc = x(i+1);
end

牛顿法:

function xk = funNewton(f, x0, max_steps, tol)
    syms x
    symbol_f = f(x);
    dif_f = matlabFunction(diff(symbol_f));
    clear x
    x = x0;
    for k = 1:max_steps
       xk = x;
       disp([\'the \', num2str(k), \' time is \', num2str(x)])
       %xk to save the last time value of x
       x = x - f(x) / dif_f(x);
       %newton solve 
       if(abs(xk - x) < tol)
       %decide whether to break out
            break;
       end
    end
end

割线法:

function xc = CutLine( f, x0, x1, tol )
    x(1) = x0;
    x(2) = x1;
    i = 2;
    while 1
        x(i + 1) = x(i) - (f(x(i)) * (x(i) - x(i - 1))) / (f(x(i)) - f(x(i - 1)));
        if(abs(x(i + 1) - x(i)) < tol)
            break;
        end
        i = i + 1;
    end
    xc = x(i + 1);
end

Stewart平台运动学问题求解:

function out = Stewart( theta )
    % set the parameter
    x1 = 4; 
    x2 = 0; 
    y2 = 4; 
    L1 = 2;
    L2 = sqrt(2);
    L3 = sqrt(2);
    gamma = pi / 2;
    p1 = sqrt(5);
    p2 = sqrt(5);
    p3 = sqrt(5);
    % calculate the answer
    A2 = L3 * cos(theta) - x1;
    B2 = L3 * sin(theta);
    A3 = L2 * cos(theta + gamma) - x2;
    B3 = L2 * sin(theta + gamma) - y2;
    
    N1 = B3 * (p2 ^ 2 - p1 ^ 2 - A2 ^ 2 - B2 ^ 2) - B2 * (p3 ^ 2 - p1 ^ 2 - A3 ^ 2 - B3 ^ 2);
    N2 =  -A3 * (p2 ^ 2 - p1 ^ 2 - A2 ^ 2 - B2 ^ 2) + A2 * (p3 ^ 2 - p1 ^ 2 - A3 ^ 2 - B3 ^ 2);
    D = 2 * (A2 * B3 - B2 * A3);
    
    out = N1 ^ 2 + N2 ^ 2 - p1 ^ 2 * D ^ 2;

end

test our function at theta = - pi / 4 and theta = pi / 4

clear all
clc

format short

disp(\'f(- pi / 4) is \')
out1 = Stewart(- pi / 4)

disp(\'--------------\')

disp(\'f(pi / 4) is \')
out2 = Stewart(pi / 4)

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
二分法解方程MATLAB发布时间:2022-07-18
下一篇:
matlab 命令行窗口如何抑制绘图和输出发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap