Matlab:其他颂歌正在发挥作用,除了ode45我需要 - 为什么?

时间:2018-06-01 22:48:43

标签: matlab ode45

我有以下脚本:

onInputChanged(searchStr: string): void {
    this.options = [];
    if (this.subscription) {
      this.subscription.unsubscribe();
    }
    this.subscription = this.yourService.getFilteredData(searchStr).subscribe((result) => {
        this.options = result;
      });
    }

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

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); ode113等,则会生成图,但是当我将其更改为ode23时会出现错误:

ode45

我可以改变什么才能使其完美运作?

1 个答案:

答案 0 :(得分:1)

你在运行什么版本的Matlab?这适合我。我明白了enter image description here

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

版本 ans =

'9.2.0.556344 (R2017a)'