Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
matlab的中点规则_Matlab_Numerical Methods_Differential Equations_Runge Kutta - Fatal编程技术网

matlab的中点规则

matlab的中点规则,matlab,numerical-methods,differential-equations,runge-kutta,Matlab,Numerical Methods,Differential Equations,Runge Kutta,您好,我被要求为中点规则创建一个matlab代码。我拥有的是eulers方法的代码,所以我必须做一些修改,但我正在努力做到这一点,我有以下几点 function H = heun(f,a,b,ya,M) h = (b-a)/M; T = zeros(1,M+1); Y = zeros(1,M+1); T = a:h:b; Y(1) = ya; for j = 1 : M k1=feval(f,T(j),Y(j)); k2=feval(f,T(j+1),Y(j)+h*k1); Y(j+1)=Y(j

您好,我被要求为中点规则创建一个matlab代码。我拥有的是eulers方法的代码,所以我必须做一些修改,但我正在努力做到这一点,我有以下几点

function H = heun(f,a,b,ya,M)
h = (b-a)/M;
T = zeros(1,M+1);
Y = zeros(1,M+1);
T = a:h:b;
Y(1) = ya;
for j = 1 : M
k1=feval(f,T(j),Y(j));
k2=feval(f,T(j+1),Y(j)+h*k1);
Y(j+1)=Y(j)+(h/2)*(k1+k2);
end
H = [T' Y'];
function f = dif1(t,y)
f=(t-y)/2;
是的,我的函数是y'=(t-y)/2,我在命令窗口中定义了间隔和其他东西


如果有可能将for循环转换为中点规则,我认为这是一条可行之路,任何帮助都将不胜感激。

下面是我在MATLAB中实现的Euler方法,用于求解一对耦合的一阶微分方程。它解决了由以下内容表示的谐振子:

y1(t+h)=y1(t)+h*y2(t)

y2(t+h)=y2(t)+h*(-A/M y1(t)-B/M y1(t)/| y1(t)|)

%使用Euler方法进行积分

while(有可能吗?是的。但是我假设你还有其他问题,但是你需要表达出来让我们知道它们是什么!这个问题目前是“请做我的作业,这里有一些相关代码”。请尝试自己实现正确的代码,并告诉我们您在哪里遇到了问题。您尝试使用什么数学,具体问题是什么,包括a和预期输出。我是matlab新手,我不是要求您解决我的整个问题,我是在寻求帮助,因为我昨天花了5个小时来理解和实现上述内容代码..好的,我现在试着比较我的代码。对于euler方法,我将代码从wi+1=wi+hf(ti,wi)转换成Y(j+1)=Y(j)+h*feval(f,t(j),Y(j));…..现在对于中点,它显示yn+1=yn+hf(tn+h/2,yn+h/2f(tn,yn))我可以像用欧拉方法一样翻译它吗?你已经正确地识别出的是Heun的方法,或者显式梯形规则,或者修正的欧拉方法。显式中点方法有时也称为RK2或改进的欧拉方法。请澄清你到底想要什么。如果没有修正,我会假设“中点法”是指隐式变量,它仍然是一种稍有不同的方法。
% Do the integration using the Euler Method
    while(T<=T1)
        % Update the position of the pt mass using current velocity
        Y1(i+1) = Y1(i) + H*Y2(i);

        % Update the velocity of the pt mass checking if we are turning 
        % within the C/A band
        if ( (abs(Y2(i) < th) && abs(Y1(i)) < C/A) )
            Y2(i+1) = Y2(i);
        else
            Y2(i+1) = Y2(i) + H * ( ((-A/M)*Y1(i)) - (B/M)*sign(Y2(i)) );
        end

        % Incriment the time by H 
        T = T + H;
        % Increase the looping index variable
        i = i + 1;
    end
% Do the integration using the Improved Euler Method
    % Ki_j = K^i_j
    while(T<=T1)
        % Calculate K^i_j's

        K1_1 = Y2(i);

        % Must check if we are turning within C/A
        if ( (abs(Y2(i) < th) && abs(Y1(i)) < C/A) )
            K1_2 = 0;
        else
            K1_2 = (-A/M)*Y1(i) - (B/M)*sign(Y2(i));
        end

        K2_1 = Y2(i)+H*K1_2;

        % Checking if we are turning within C/A
        if ( (abs(Y2(i) < th) && abs(Y1(i)) < C/A) )
            K2_2 = 0;
        else
            K2_2 = (-A/M)*(Y1(i) + H*K1_1) - (B/M)*sign(Y2(i)+ H*K1_2);
        end


        % Update the position and velocity
        Y1(i+1) = Y1(i)+ (H/2)*(K1_1+K2_1);
        Y2(i+1) = Y2(i) + (H/2)*(K1_2+K2_2);

        % Incriment the time by H 
        T = T + H;
        % Increase the looping index variable
        i = i + 1;
    end