-2

我写了下面的代码,它工作了几个星期没有任何问题。突然它停止工作并出现以下错误:

"warning: lsode: passing function body as a string is obsolete; please use anonymous functions
warning: called from
heun at line 23 column 2
error: 'f' undefined near line 1 column 42
error: lsode: evaluation of user-supplied function failed
error: called from
__lsode_fcn__C__ at line 1 column 40
heun at line 23 column 2"

代码是:

function yn=euler(t0,y0,tend,f,n)
t0=input('Gib hier t0 ein:');
y0=input('Gib hier y0 ein:');
tend=input('Bis zu welchem Wert von t möchtest du y annährn?');
n=input('Gib hier die Anzahl der Schritte ein(n):');
fstrich=input('Wie lautet die Differentialgleichung?', 's');
f=inline('fstrich');
dt=(tend-t0)/n;
t(1)=t0;
y(1)=y0;
for i=1:n
t(i+1)=t(i)+dt;
y(i+1)=y(i)+f(t(i),y(i))*dt;


rotdim([flip(t) ; flip(y)])
scatter(t,y,'*')
hold on
f=inline('fstrich');
t=t0:0.001:tend;
y=lsode('f',y0,t);
plot(t,y)

有没有人看到错误或我可以改变的东西?

4

1 回答 1

1

显然你更新了八度。

f=inline('fstrich'); % this is discouraged
y=lsode('f',y0,t); % and this is not valid anymore. 

相反,创建一个匿名函数

f=str2func(fstrich);
y=lsode(f,y0,t);
于 2020-09-13T17:19:30.127 回答