Ode45 not working in matlab 2021b

6 ビュー (過去 30 日間)
Marcus Rost 2022 年 1 月 24 日
コメント済み: Steven Lord 2022 年 1 月 24 日
I'm workin on a project for university, and I got a problem:
I'm getting an error with ode45, and I got no clue how to fix it.
Could you lend me a hand?
Here's the code:
function pendul(~)
figure
disp('')
disp('DAMPED PENDULUM MOTION DESCRIBED BY')
disp('theta"(t)+0.2*theta''(t)+sin(theta) = 0')
zdot=inline('[z(2);-0.2*z(2)-sin(z(1))]','t','z');
ops=odeset('reltol',1e-5,'abstol',1e-5);
if nargin==0
while 1, close, disp(' ')
disp('Select the angular velocity at the lowest')
disp('point. Values of 2.42 or greater push the')
disp( 'the pendulum over the top. Input zero to stop.')
w0=input('w0 = ? > ');
if isempty(w0) || w0==0
disp(' '), disp('All Done'), disp(' '), return
end
disp(' ')
t=input(['Input a vector of time values ', '(Try 0:.1:30) > ? ']);
disp(' ')
titl=input('Input a title for the graphs : ','s');
disp(' '), disp('Input 1 to leave images of all positions shown')
trac=input( 'in the animation, otherwise input 0 > ? ');
theta0=0; z0=[theta0;w0];
[t,th]=ode45(zdot,t,z0,ops);
animpen(t,th(:,1),titl,.05,trac)
end
else
tmax=30; n=351; t=linspace(0,tmax,n);
disp(' ')
disp('Press return to see two examples'), pause
w0=2.42; W0=num2str(w0);
[t,th]=ode45(zdot,t,[0;w0],ops);
titl=['PUSHED OVER THE TOP FOR W0 = ',W0];
animpen(t,th(:,1), titl,.05), pause(2)
w0=2.41; W0=num2str(w0);
[t,th]=ode45(zdot,t,[0;w0],ops);
titl=['NEARLY PUSHED OVER THE TOP FOR W0 = ',W0];
animpen(t,th(:,1),titl,.05)
close, disp(' '), disp('All Done'), disp(' ')
end
And I'm getting this error:
Error using inline/horzcat
Too many output arguments.
Error in odefcncleanup (line 15)
[~,oldFcnFun] = evalc(['@' oldFcn]);
Error in ode45 (line 265)
odeFcn_main = odefcncleanup(FcnUsed,odeFcn,odeArgs);
Error in pendul (line 50)
[t,th]=ode45(zdot,t,z0,ops);
Any help would be much apreciated :D
1 件のコメント表示非表示 なし
Steven Lord 2022 年 1 月 24 日
Stop using the inline function. Its use has been discouraged for pretty close to two decades now. It remains for backwards compatibility. As its documentation page suggests, use function handles instead. [Anonymous functions are a type of function handle.]

サインインしてコメントする。

回答 (1 件)

VBBV 2022 年 1 月 24 日

zdot=inline('[z(2);-0.2*z(2)-sin(z(1))]','t','z')
Check your zdot function. There is no variable with t defined in it.
6 件のコメント表示非表示 5 件の古いコメント
Torsten 2022 年 1 月 24 日
Please show the modified code.
And in a first attempt don't read w0 and t from input, but prescribe them as
w0 = 3.0;
t = 0:0.1:10
e.g.

サインインしてコメントする。

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by