简体   繁体   English

octave 中使用 theta 方法求解 ode 的代码无法运行(找不到错误)

[英]the code in octave using theta method to solve an ode doesn't run ( can't find the error)

Using the theta method seen in the picture below使用下图所示的 theta 方法

( https://i.stack.imgur.com/MJx0d.png ) ( https://i.stack.imgur.com/MJx0d.png )

for theta in [0.1].对于 [0.1] 中的 theta。

I'm trying to solve some ODE seen in the code我正在尝试解决代码中看到的一些 ODE

` `

function [tnodes,y] = theta_method(f, t_interval, N, y0, theta)

global glob_h glob_yold glob_tnew glob_f glob_theta;

y= y0;
yold= y0(:); %initial conditions
t0 = t_interval(1);
tN = t_interval(2);

tnodes = linspace(t0, tN, N+1); %the nodes of the partition

glob_h = (tN- t0)/N; %mesh-size
glob_f = f;
glob_theta=theta;
for tnew = tnodes(2:end)
  glob_tnew = tnew;
  glob_yold = yold;
  ynew = fsolve(@(x)F(x), yold);
  y =[y; ynew.'];
  yold = ynew;
end
endfunction

function rhs = F(x)

  global glob_h glob_yold glob_tnew glob_f glob_theta;

  rhs = x - glob_h*glob_theta*feval(glob_f, glob_tnew, x)-glob_h*(1.0 - glob_theta)*feval(glob_f, t0, glob_yold) - glob_yold;

Endfunction

` `

And the plot:和 plot:

` `

PLOT
hold off
N=20;
t_interval = [0 1];
y0 = 1;
theta = 0.5;
[t,y]=theta_method(@model_f, t_interval, N, y0, theta)
uexact = model_exact(t)
plot(t,y,'linewidth', 1, 'g*'); % plot approx vs t
hold on
plot(t,uexact,'linewidth', 1, 'r'); % plot exact solution vs t
legend('BE approx','exact solution')
title('Solution of y''=\lambda y')
xlabel('time')

MODEL EXACT
function rhs = model_exact(t)
  lambda = -1;
  rhs = exp(lambda*t);
endfunction
MODEL F

function rhs = model_f(t,y)
  lambda = -1;
  rhs = lambda*y;
endfunction

` `

the program says the the error is `程序说错误是`

error: 't0' undefined near line 28, column 99
error: called from
    theta_method>F at line 28 column 7
    theta_method>@<anonymous> at line 18 column 21
    fsolve at line 246 column 8
    theta_method at line 18 column 8
    a at line 6 column 6

` `

but i can't understand why what is wrong.但我不明白为什么出了什么问题。 I have define t0 and i think there isn't any syntax error.我已经定义了 t0,我认为没有任何语法错误。

It would be simpler to avoid the need for global variables.避免需要全局变量会更简单。

told = tnodes(1)
for tnew = tnodes(2:end)
  fold = f(told,yold);
  yhalf = yold+(1-theta)*h*fold;
  F = @(x) yhalf+theta*h*f(tnew,x)-x;
  ynew = fsolve(F, yold+h*fold);
  y =[y; ynew.'];
  told = tnew;
  yold = ynew;
end

It would be advisable to scale the error tolerances of fsolve with h or h^2 .建议使用hh^2缩放fsolve的误差容限。

See SIR model using fsolve and Euler 3BDF for a similar construction for a different implicit method.请参阅SIR model 使用 fsolve 和 Euler 3BDF了解不同隐式方法的类似构造。 Another method with setting the options for the error tolerances, Matlab code for $y_{n+2} - y_n = h\left[(1/3)f_{n+2} + (4/3)f_{n+1} + (1/3)f_n\right]$另一种设置误差容限选项的方法, Matlab 代码 $y_{n+2} - y_n = h\left[(1/3)f_{n+2} + (4/3)f_{n+1 } + (1/3)f_n\right]$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM