ODE Runge Kutta MATLAB错误 [英] ODE Runge Kutta MATLAB error

查看:85
本文介绍了ODE Runge Kutta MATLAB错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图创建一个Runge Kutta函数,这是我的代码:

so I'm trying to create a Runge Kutta function and this is my code :

function [t,U] = RK(f, n, eta, interv)
h = (interv(2)-interv(1))/n;
t = interv(1):h:interv(2);

v(1) = eta(1);
w(1) = eta(2);
for i=1:n
    k1 = f([v(i),w(i)]);
    k2 = f([v(i),w(i)]+h*k1/2); %f(t(i)+h/2, u(:,i)+h*k1/2);
    k3 = f([v(i),w(i)]+h*k2/2);
    k4 = f([v(i),w(i)]+h*k3);

    v(i+1) = v(i) + h*(k1(1)+2*k2(1)+2*k3(1)+k4(1))/6;
    w(i+1) = w(i) + h*(k1(2)+2*k2(2)+2*k3(2)+k4(2))/6;
end
U = [v;w];
end

其中U是2行和n + 1列的矩阵,这是我尝试执行此函数时遇到的问题,例如:

Where U is a matrix of 2 lines and n+1 columns, here is the problem when I try to execute this function, for example :

RK(sin, 10, [0,1], [5,15])

我收到错误not enough input arguments,但是当我尝试将代码作为脚本执行并用sin代替f时,一切正常,我得到了U矩阵 有人能告诉我解决方案吗?

I get the error not enough input arguments but when I try to execute the code as a script and replacing f by sin every thing works and i get the U matrix Can someon tell what is the solution ?

推荐答案

您正在将sin用作函数句柄.只需添加@符号,即可设置.

You are using the sin as a function handle. Just add the @ symbol and you're set.

RK(@sin, 10, [0,1], [5,15])

这篇关于ODE Runge Kutta MATLAB错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆