其他分享
首页 > 其他分享> > [数学]midpoint 法

[数学]midpoint 法

作者:互联网

原理思想

中点法是龙格-库塔方法的二阶的一种形式 了解龙格-库塔的思想和求解:龙格-库塔方法RK.

公式

\[k1 = f(x_i,y_i)\]
\[k2 = f(x_i+0.5h,y_i+0.5h)\]
\[y_{i+1} = y_i+hk2\]

MATLAB 代码

fun = @(x,y) (x+y);
myans = midpoint_method(fun,0,2,1,0.25);
hold on;

% 准确值
xx = 0:0.25:1;
yy = 3*exp(xx)-xx-1;
plot(xx,yy,'r');

legend('近似值','point','准确值');
% midpoint 
function re = midpoint_method(f,x0,y0,xn,h)
    n = round((xn-x0)/h);
    k1 = f(x0,y0);
    x =x0;
    y = y0;
    xx = [];
    yy = [];
    
    xa(1)=x0;
    ya(1)=y0;
    for i=1:n
        k2 = f(x+h/2,y+(h/2)*k1);
        y = y+h*k2;
        x = x0+h*i;
        k1 = f(x,y);
        xa(i+1) = x;
        ya(i+1) = y;
    end
    re = y;
    fprintf('%f\n',re);
    plot(xa,ya);
    hold on;
    scatter(xa,ya,'b*');
    
end

Result:

标签:ya,xa,midpoint,k1,xx,数学,y0,x0
来源: https://www.cnblogs.com/tailiang/p/12255210.html