Error in ODE45 when using ...................... >> [t,x] = ode45(@odefcn,tspan,[0 0]);
3 ビュー (過去 30 日間)
古いコメントを表示
Md Muzakkir Quamar
2020 年 10 月 15 日
コメント済み: Steven Lord
2020 年 10 月 15 日
function dxdt = odefcn(t,x,a,b,c,u)
dxdt = zeros(2,1);
dxdt(1) = a*x(1)+2*x(2)+u;
dxdt(2) = c*x(1)+ b*x(2)-2*u;
end
clc;clear all; close all;
a=-1; b=-2; c=0; u=3;
tspan = [0 20];
[t,x] = ode45(@odefcn,tspan,[0 0]);
figure(1)
plot(t,x(:,1),'r',t,x(:,2),'b')
i am getting an error (attached screenshot)
while i am not getting error when i am using
a=-5; b=-3; c=1; u=3;
tspan = [0 20];
[t,x] = ode45(@(t,x) odefcn(t,x,a,b,c,u),tspan,[0 0]);
figure(2)
plot(t,x(:,1),'r',t,x(:,2),'b')
can anyone please explain
0 件のコメント
採用された回答
Walter Roberson
2020 年 10 月 15 日
編集済み: Walter Roberson
2020 年 10 月 15 日
[t,x] = ode45(@odefcn,tspan,[0 0]);
That is valid syntax.
However, your function is relying on the variables a, b, c, and u. MATLAB never automatically passes variables from the calling environment into a function. If you define
x = 42;
y = f();
and you have defined
function result = f(x)
result = x.^2;
end
then when you call f without passing any argument into it, MATLAB does not look and see that the internal parameter name for the first argument is x and then look into the script and see there is an x there and use the caller's x as the value for x in the function. That never happens for named parameters. (There are cases where MATLAB will look for a variable in an outer environment, but that is never done for named parameters.)
So... at the time that your function odefcn first needs to use a, b, c, or u, the function would fail, because you would not have passed the values into the function.
The approach you used with the anonymous function
@(t,x) odefcn(t, x, a, b, c, u)
is the recommended approach. http://www.mathworks.com/help/matlab/math/parameterizing-functions.html
The approach Alan suggested, of instead passing extra parameters, has not been documented since about MATLAB 5.2 or so, and has some subtle internal incompatibilities so it is not completely reliable.
その他の回答 (1 件)
Alan Stevens
2020 年 10 月 15 日
The first approach needs to be structured as follows:
a=-1; b=-2; c=0; u=3;
tspan = [0 20];
[t,x] = ode45(@odefcn,tspan,[0 0],[],a, b, c, u);
figure(1)
plot(t,x(:,1),'r',t,x(:,2),'b')
function dxdt = odefcn(t,x,a,b,c,u)
dxdt = zeros(2,1);
dxdt(1) = a*x(1)+2*x(2)+u;
dxdt(2) = c*x(1)+ b*x(2)-2*u;
end
4 件のコメント
Steven Lord
2020 年 10 月 15 日
That screen shot of the documentation does not show the syntax that Walter stated has not been documented for 15 years, where the additional parameters are passed after the options structure. I second the recommendation not to use this syntax for new code, and if possible to migrate (very) old code to use one of the recommended parameterization techniques.
参考
カテゴリ
Help Center および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!