0

我有以下脚本:

h=[0.01 0.05 0.1 0.5 1];
func = @(t, y) -2*y+t*sin(t);
opts = odeset('Reltol',1e-13,'AbsTol',1e-14,'Stats','on');
f1_f2_matrix = [];
sinus=@(t) t*(sin(t));
error2nd = zeros(length(h),1);
errorInf = zeros(length(h),1);
error2ndEuler = zeros(length(h),1);
errorInfEuler = zeros(length(h),1);


for i=1:length(h)
t=0:h(i):10;
euler = zeros(length(t),1);
fun_builtin = zeros(length(t),1);
t_builtin = zeros(length(t),1);
results = zeros(length(t),1);


%matrix for A=-2 and given h=0.01
matrix=[ 1-1/2*h(i), (1/2-sqrt(3)/3)*h(i); 
        (1/2+sqrt(3)/3)*h(i), 1-1/2*h(i)];


    for n=2:length(t)
    B = [-2*results(n-1) + sinus(t(n-1)+(1/2-sqrt(3)/6)*h(i));
         -2*results(n-1) + sinus(t(n-1) + (1/2+sqrt(3)/6)*h(i))];

%system of equations solution

    f1_f2_matrix = matrix\B; 

    results(n) = (results(n-1) + h(i)*0.5*(f1_f2_matrix(1) + f1_f2_matrix(2))); 

%euler
    euler(n) = euler(n-1)+h(i)*func(t(n-1),euler(n-1));

   end

   %ode45
[t_builtin,fun_builtin] = ode45(func, t, 0, opts);

%y'=-2y+tsin(t) errors
error2nd(i)=norm(fun_builtin-results)/norm(fun_builtin); %root mean square error of my function
errorInf(i)=norm((fun_builtin-results), Inf)/ norm((fun_builtin), Inf); %maximum error of my function

error2ndEuler(i)=norm(euler-results)/norm(fun_builtin); %root mean square error of my function (euler)
errorInfEuler(i)=norm((euler-results), Inf)/ norm((fun_builtin), Inf); 
%maximum error of my function (euler)

end

figure 
semilogy(h, error2nd, h, errorInf,h, error2ndEuler, h, errorInfEuler)
title([{('Dependence of root mean square and maximum errors')}; {('from h-step and method of solving differential equation')}])
legend('root mean square error: Gauss-Legendre of 4th order','maximum error: Gauss-Legendre of 4th order method','root mean square error: Euler method', 'maximum error: Euler method', 'Location', 'southeast');
xlabel('step');
ylabel('error'); 

请考虑上述代码块中的以下行:

%ode45
[t_builtin,fun_builtin] = ode45(func, t, 0, opts);

如果我将其更改为ode113ode23,则会生成该图形,但是当我将其更改ode45为时出现错误:

Error using ode45
Too many input arguments.

Error in task1 (line 41)
    [t_builtin,fun_builtin] = ode45(func, t, 0, opts);

我可以进行哪些更改以使其完美运行?

4

1 回答 1

1

你运行的是什么版本的 Matlab?这对我有用。我明白了。在此处输入图像描述

1845 successful steps
5 failed attempts
11101 function evaluations
1845 successful steps
5 failed attempts
11101 function evaluations
1844 successful steps
4 failed attempts
11089 function evaluations
1845 successful steps
8 failed attempts
11119 function evaluations
1845 successful steps
9 failed attempts
11125 function evaluations
>> testodes
1845 successful steps
5 failed attempts
11101 function evaluations
1845 successful steps
5 failed attempts
11101 function evaluations
1844 successful steps
4 failed attempts
11089 function evaluations
1845 successful steps
8 failed attempts
11119 function evaluations
1845 successful steps
9 failed attempts
11125 function evaluations

版本答案 =

'9.2.0.556344 (R2017a)'
于 2018-06-02T01:30:52.183 回答