Ode45 not working in matlab 2021b
2 views (last 30 days)
Show older comments
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 Comment
Steven Lord
on 24 Jan 2022
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.]
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!